2016-04-02 14 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

感謝の意思決定のリーダーまたは罰金が働いた – user6147957

関連する問題