簡體   English   中英

在Scikit學習中訓練具有不同特征維度的邏輯回歸模型

[英]train logistic regression model with different feature dimension in scikit learn

在Windows上使用Python 2.7。 想要使用特征T1T2擬合分類問題的邏輯回歸模型,目標是T3

我顯示T1T2的值,以及我的代碼。 問題是,由於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM