[英]train logistic regression model with different feature dimension in scikit learn
在Windows上使用Python 2.7。 想要使用特征T1
和T2
擬合分類問題的邏輯回歸模型,目標是T3
。
我顯示T1
和T2
的值,以及我的代碼。 問題是,由於T1
維數為5,而T2
維數為1,我們應該如何對其進行預處理,以便可以通過scikit-learn logistic回歸訓練正確利用它?
BTW,我的意思是訓練樣本1,其T1
特征是[ 0 -1 -2 -3]
,而T2
特征是[0]
,對於訓練樣本2,其T1的特征是[ 1 0 -1 -2]
, T2
特征是[1]
,...
import numpy as np
from sklearn import linear_model, datasets
arc = lambda r,c: r-c
T1 = np.array([[arc(r,c) for c in xrange(4)] for r in xrange(5)])
print T1
print type(T1)
T2 = np.array([[arc(r,c) for c in xrange(1)] for r in xrange(5)])
print T2
print 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 target
logreg.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連接要素數據矩陣。
import numpy as np
from sklearn import linear_model, datasets
arc = lambda r,c: r-c
T1 = np.array([[arc(r,c) for c in xrange(4)] for r in xrange(5)])
T2 = np.array([[arc(r,c) for c in xrange(1)] for r in xrange(5)])
T3 = np.array([0,0,1,1,1])
X = np.concatenate((T1,T2), axis=1)
Y = T3
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 target
logreg.fit(X, Y)
X_test = np.array([[1, 0, -1, -1, 1],
[0, 1, 2, 3, 4,]])
print logreg.predict(X_test)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.