如何从两个数组和单个浮点值创建单个numpy特征数组? [英] How can I create single numpy feature array from two arrays and a single float value?

查看:102
本文介绍了如何从两个数组和单个浮点值创建单个numpy特征数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个训练数据文件,其结构如下:

I am trying to create a training data file which is structured as follows:

[行=样本,列=特征]

[Rows = Samples, Columns = features]

所以如果我有100个样本和2个特征,我的np.array的形状将是(100,2)等.

So if I have 100 samples and 2 features the shape of my np.array would be (100,2) etc.

数据

下面的列表包含到使用方法01处理的.nrrd 3D样本补丁数据文件的路径字符串.

The list bellow contains path-strings to the .nrrd 3D sample patch-data files which have been processed using method 01.

['/Users/FK/Documents/image/01/subject1F_200.nrrd',
'/Users/FK/Documents/image/01/subject2F_201.nrrd']

让我们将目录称为dir_01. 为了进行测试,可以使用以下3D补丁.读取时它与.nrrd文件具有相同的形状:

Lets call the directory dir_01. For testing purposes the following 3D patch can be used. It has the same shape as the .nrrd file when read:

subject1F_200_PP01 = np.random.rand(128,128, 128)
subject1F_201_PP01 = np.random.rand(128,128, 128)
# and so on...

下面的列表包含使用方法02处理的.nrrd 3D示例补丁数据文件的路径字符串.

The list bellow contains path-strings to the .nrrd 3D sample patch-data files which have been processed using method 02.

['/Users/FK/Documents/image/02/subject1F_200.nrrd',
'/Users/FK/Documents/image/02/subject2F_201.nrrd']

让我们将目录称为dir_02. 为了进行测试,可以使用以下3D补丁.读取时它与.nrrd文件具有相同的形状:

Lets call the directory dir_02. For testing purposes the following 3D patch can be used. It has the same shape as the .nrrd file when read:

subject1F_200_PP02 = np.random.rand(128,128, 128)
subject1F_201_PP02 = np.random.rand(128,128, 128)
# and so on...

两个主题都相同,但是补丁数据的预处理方式不同.

Both the subjects are the same, but the patch data has been pre-processed differently.

功能

为了计算功能,我需要使用以下功能:

In order to calculate the features I need to use the following functions:

  1. np.median(常规python函数,返回单个值)
  2. my_own_function1(常规python函数,返回一个np.array)
  3. my_own_function2(我只能使用matlab引擎访问它并返回np.array)

在这种情况下,我最终的numpy数组应具有(2,251)形状.因为我必须从3个函数中采样(行)和251个特征(列).

In this scenario my final numpy array should have a (2,251) shape. Since I have to samples (rows) and 251 features (columns) from my 3 functions.

这是我的代码(归功于M.Fabré)

阅读补丁

# Helps me read the files for features 1. and 2. Uses a python .nrrd reader
def read_patches_multi1(files_1):
    for file_1 in files_1:
        yield nrrd.read(str(file_1))

# Helps me read the files for features 3. Uses a matlab .nrrd reader
def read_patches_multi2(files_2):
    for file_2 in files_2:
        yield eng.nrrdread(str(file_2))

计算

def parse_patch_multi(patch1, patch2):

    # Structure for python .nrrd reader
    data_1 , option = patch1

    # Structure for matlab .nrrd reader
    data_2 = patch2

    # Uses itertools to combine single float32 value with np.array values
    return [i for i in itertools.chain(np.median(data_1), my_own_function1(data_1), my_own_function2(data_2))]

执行

# Directories
dir_01 = '/Users/FK/Documents/image/01/'
dir_02 = '/Users/FK/Documents/image/02/'

# Method 01 patch data
file_dir_1 = Path(dir_01)
files_1 = file_dir_1.glob('*.nrrd')
patches_1 = read_patches_multi1(files_1)

# Method 02 patch data
file_dir_2 = Path(dir_02)
files_2 = file_dir_2.glob('*.nrrd')
patches_2 = read_patches_multi2(files_2)

# I think the error lies here...
training_file_multi = np.array([parse_patch_multi(patch1,patch2) for (patch1, patch2) in (patches_1, patches_2)], dtype=np.float32)

我尝试了多种方法,但是我一直收到语法错误或错误的结构.或以下类型错误:

I have tried multiple approaches but I am keep getting syntax error or the wrong structure. Or the following type error:

TypeError: unsupported Python data type: numpy.ndarray

推荐答案

我找到了一个解决方案,但它似乎不太优雅

I found a solution but it does not seem too elegant

我创建了两个函子:

def parse_patch_multi1(patch1):

    # Structure for python .nrrd reader
    data_1 , option = patch1

    # Uses itertools to combine single float32 value with np.array values
    return [i for i in itertools.chain(np.median(data_1), 0) my_own_function1(data_1)]


def parse_patch_multi2(patch2):

    # Structure for python .nrrd reader
    data_2 = patch2

    # Uses itertools to combine single float32 value with np.array values
    return [i for i in itertools.chain(my_own_function2(data_2)]

执行

# Directories
dir_01 = '/Users/FK/Documents/image/01/'
dir_02 = '/Users/FK/Documents/image/02/'

# Method 01 patch data
file_dir_1 = Path(dir_01)
files_1 = file_dir_1.glob('*.nrrd')
patches_1 = read_patches_multi1(files_1)

# Method 02 patch data
file_dir_2 = Path(dir_02)
files_2 = file_dir_2.glob('*.nrrd')
patches_2 = read_patches_multi2(files_2)

training_file_multi1 = np.array([parse_patch_multi1(patch1) for (patch1) in patches_1], dtype=np.float32)
training_file_multi2 = np.array([parse_patch_multi2(patch2) for (patch2) in patches_1], dtype=np.float32)

把戏

沿着轴1连接两个np.arrays

concatenate the two np.arrays along Axis 1

training_file_combined= np.concatenate((training_file_multi1, training_file_multi2), axis=1)

矩阵的形状(2,252)

Shape of the matrix (2,252)

这篇关于如何从两个数组和单个浮点值创建单个numpy特征数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆