【问题标题】:How can I add class labels in python?如何在 python 中添加类标签?
【发布时间】:2019-04-05 03:57:22
【问题描述】:

我有高斯数据:

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

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

【问题讨论】:

  • 您想使用哪个机器学习库? pybrain? scikit-学习?另一个图书馆?你想自己开发这个库吗?
  • 我正在尝试在不使用库的情况下设计 knn 分类器。

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


【解决方案1】:

将 class1 的 +1 视为 label1,将 class 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

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 2014-12-19
    • 2021-11-29
    • 2021-11-09
    • 2011-03-10
    • 2016-06-11
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多