从文件加载数据并规范化

时间:2015-01-06 12:31:59

标签: python-3.x numpy machine-learning classification knn

如何规范化从文件加载的数据?我在这里。数据看起来像这样:

65535, 3670, 65535, 3885, -0.73, 1
65535, 3962, 65535, 3556, -0.72, 1

每行中的最后一个值是目标。我希望拥有相同的数据结构,但具有标准化值。

import numpy as np
dataset = np.loadtxt('infrared_data.txt', delimiter=',')

# select first 5 columns as the data
X = dataset[:, 0:5]

# is that correct? Should I normalize along 0 axis?
normalized_X = preprocessing.normalize(X, axis=0)

y = dataset[:, 5]

现在问题是,如何正确打包normalized_Xy,它具有以下结构:

dataset = [[normalized_X[0], y[0]],[normalized_X[1], y[1]],...]

1 个答案:

答案 0 :(得分:1)

听起来你要np.column_stack。例如,让我们设置一些虚拟数据:

import numpy as np
x = np.arange(25).reshape(5, 5)
y = np.arange(5) + 1000

这给了我们:

X:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
Y:
array([1000, 1001, 1002, 1003, 1004])

我们想要:

new = np.column_stack([x, y])

这给了我们:

New:
array([[   0,    1,    2,    3,    4, 1000],
       [   5,    6,    7,    8,    9, 1001],
       [  10,   11,   12,   13,   14, 1002],
       [  15,   16,   17,   18,   19, 1003],
       [  20,   21,   22,   23,   24, 1004]])

如果您更喜欢打字,也可以使用:

In [4]: np.c_[x, y]
Out[4]:
array([[   0,    1,    2,    3,    4, 1000],
       [   5,    6,    7,    8,    9, 1001],
       [  10,   11,   12,   13,   14, 1002],
       [  15,   16,   17,   18,   19, 1003],
       [  20,   21,   22,   23,   24, 1004]])

但是,由于可读性问题,我不鼓励将np.c_用于除交互式使用之外的任何其他内容。