如何从两个数组和一个单个浮点值创建单个numpy特征数组



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

[rows =样品,列=特征]

因此,如果我有100个样本和2个特征,则我的np array的形状为(100,2)等。

数据

列表bellow包含了使用方法01。

处理的.NRRD 3D示例patch-data文件的路径串。
['/Users/FK/Documents/image/01/subject1F_200.nrrd',
'/Users/FK/Documents/image/01/subject2F_201.nrrd']

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

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

列表bellow包含了使用方法02。

处理的.NRRD 3D示例patch-data文件的路径串。
['/Users/FK/Documents/image/02/subject1F_200.nrrd',
'/Users/FK/Documents/image/02/subject2F_201.nrrd']

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

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

两个受试者都是相同的,但是贴片数据已预处理不同。

功能功能

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

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

在这种情况下,我的最终Numpy阵列应具有(2,251)形状。由于我必须从3个函数中获取样品(行)和251个功能(列)。

这是我的代码(信用为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)

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

TypeError: unsupported Python data type: numpy.ndarray

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

我创建两个funciton:

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。
training_file_combined= np.concatenate((training_file_multi1, training_file_multi2), axis=1)

矩阵的形状(2,252)

相关内容

  • 没有找到相关文章

最新更新