2016-04-02 93 views
1

我是機器學習的新手。我正在嘗試使用線性迴歸進行簡單預測,並使用遵循特定模式的「編制」數據。出於某種原因,預測不符合訓練數據。你能讓我知道我需要解決什麼嗎?示例代碼如下線性迴歸預測與訓練數據不匹配

from sklearn import linear_model 
import numpy as np 

X = np.random.randint(3, size=(3, 1000)) 
Y = np.random.randint(10, size=(1, 1000)) 
# f1, f2, f3 - min = 0, max = 2 
# f1 = 0 and f2 = 1 then 7 <= Y < 10, irrespective of f3 
# f1 = 1 and f2 = 2 Y is 0, irrespective of f3 
# f1 = 0 and f2 = 2 if f3 = 2 then 3 <= Y < 7 else Y = 0 
for i in range(1000): 
    if ((X[0][i] == 0 and X[1][i] == 1) or (X[0][i] == 1 and X[1][i] == 0)): 
     Y[0][i] = np.random.randint(7, 10) 
    elif ((X[0][i] == 1 and X[1][i] == 2) or (X[0][i] == 2 and X[1][i] == 1)): 
     Y[0][i] = 0 
    elif ((X[0][i] == 0 and X[1][i] == 2 and X[2][i] == 2) or 
     (X[0][i] == 2 and X[1][i] == 0 and X[2][i] == 2)): 
     Y[0][i] = np.random.randint(3, 7) 
    else: 
     Y[0][i] = 0 

X1 = X.transpose() 
Y1 = Y.reshape(-1, 1) 
print zip(X1, Y1) 

# create and fit the model 
clf = linear_model.LinearRegression() 
clf.fit(X1, Y1) 

Z = np.array([[0, 0, 0, 0, 1, 1], 
       [1, 1, 2, 2, 2, 2], 
       [1, 2, 1, 2, 1, 2]]) 
Z1 = Z.transpose() 
print Z1 

y_predict = clf.predict(Z1) 
print y_predict 

回答

1

爲什麼它會匹配訓練數據?你的X-> Y關係顯然是非線性的,只有爲了完美的線性關係,這意味着Y = AX + b,你可以期望線性迴歸完全符合訓練數據。否則,您可以遠離解決方案 - 例如,參見Anscombe的四重奏(來自Wiki的圖片)。

enter image description here

+0

當然,這種訓練數據更好的算法是什麼? – user6147957

+0

一個決策/迴歸樹(或一個非常小的隨機森林)應該很好地處理它。 – lejlot

+0

謝謝decisiontreeregressor工作正常 – user6147957