如何在python中添加类标签?

时间:2018-10-31 22:05:16

标签: python python-2.7 numpy machine-learning pattern-recognition

我有高斯数据,即:

r1=np.random.multivariate_normal(mean1, cov1, 3000)
r2=np.random.multivariate_normal(mean2, cov2, 3000)

现在,我想为这些数据添加类别标签以训练分类器。 对于r1,它是class1,对于r2,它是class2。如何添加课程标签?

1 个答案:

答案 0 :(得分:0)

将类别1的+1视为label1,将类别2的-1视为label2:

label1 = np.ones( (r1.shape[0],1) )
label2 = np.ones( (r2.shape[0],1) ) * -1
data = np.concatenate((r1, r2))
labels = np.concatenate((label1, label2))

如果您需要在训练之前对数据进行混洗,并且想要跟踪哪个标签用于哪个样本,请首先将每个标签添加到其对应的数据中:

r1 = np.append(label1, r1, axis=1)
r2 = np.append(label2, r2, axis=1)
data = np.concatenate((r1,r2))
np.random.shuffle(data)
labels = data[:,0] #extracts labels in shape of (len(labels),)which is a rank 1 array 
labels = np.reshape(labels,(len(labels),1)) #fix the shape to a 1D array    
R = data[:,(1,2)] #extracting inputs
相关问题