在Windows上使用Python 2.7。想要为一个分类问题使用特征T1
和T2
来拟合逻辑回归模型,目标是T3
。
我展示了T1
和T2
的值,以及我的代码。问题是,由于T1
的维度是5,而T2
的维度是1,我们应该如何预处理它们,以便scikit-learn的逻辑回归训练能够正确利用它们?
顺便说一下,我的意思是,对于训练样本1,其T1
特征是[ 0 -1 -2 -3]
,T2
特征是[0]
,对于训练样本2,其T1
特征是[ 1 0 -1 -2]
,T2
特征是[1]
,…
import numpy as npfrom sklearn import linear_model, datasetsarc = lambda r,c: r-cT1 = np.array([[arc(r,c) for c in xrange(4)] for r in xrange(5)])print T1print type(T1)T2 = np.array([[arc(r,c) for c in xrange(1)] for r in xrange(5)])print T2print type(T2)T3 = np.array([0,0,1,1,1])logreg = linear_model.LogisticRegression(C=1e5)# we create an instance of Neighbours Classifier and fit the data.# using T1 and T2 as features, and T3 as targetlogreg.fit(T1+T2, T3)
T1,
[[ 0 -1 -2 -3] [ 1 0 -1 -2] [ 2 1 0 -1] [ 3 2 1 0] [ 4 3 2 1]]
T2,
[[0] [1] [2] [3] [4]]
回答:
需要使用numpy.concatenate来拼接特征数据矩阵。