X内に保持されている2つの機能に基づいてyを予測しようとしています。Excelファイルを読み取り、データを列に分割すると、X値は次のようになります。
SibSp Parch
0 1 0
1 1 0
2 0 0
3 1 0
4 0 0
5 0 0
6 0 0
7 3 1
8 0 2
9 1 0
y
は生存率を示し、1は生存し、0は死亡しました。Xにはさらに多くの行があります。私はtrain_test_split(X, y, test_size=0.4, random_state=101)
トレーニングとテストのデータ分割を取得するために使用しており、トレーニングとテストの方法があります。私のトレーニングコードは次のようになります。
def train():
# Get Data Split
X_train, X_test, y_train, y_test = initData()
# Create LinearRegression Instance
lm = LinearRegression()
# Fit Training Values
lm.fit(X_train,y_train)
visualise(X_test, y_test, lm.predict(X_test))
# Return Trained Data With Testing Data
return X_test, y_test, lm
私のテストコードは次のようになります。
def test():
# Get The Trained Classifier
X, y, lm = train()
# Fit New Values
lm.fit(X, y)
visualise(X, y, lm.predict(X))
これは、うまく機能しているように見えます-またはそう思います。現在、予測折れ線グラフを使用して、データを散布図として視覚化しようとしています。
def visualise(X, y, predictions):
features = X.shape[1]
colors = ['red', 'blue']
i = 0
while i <= features -1:
plt.scatter(X.iloc[:, i], y, color=colors[i])
# Update: Forgot to add this line when posting question
plt.plot(X.iloc[:, i], predictions, color=colors[i])
i=+1
しかし、これは私にどこにでも行く線でクレイジーな出力を与えています。オンラインで調べてみたところ、sklearnの例が見つかりました。これは私がこれを複製しようとしていることです:
たぶん、私には2つの機能があるので、それらを別々に識別する必要があるのではないかと思いました。
def visualise(X, y, predictions):
newY = np.zeros(X.shape[0], X.shape[1]);
newY[:, 0:1] = newY.iloc[:, 0]
plt.scatter(X, y, color='blue')
plt.plot(X, predictions, color='red')
plt.xticks(())
plt.yticks(())
plt.show()
Xには2つの特徴があり、yには1つの特徴があるため、形状が異なるため、newY配列を作成する必要がありました。しかし今、私はラインでエラーが発生していますnewY = np.zeros(X.shape[0], X.shape[1]);
TypeError:データ型が理解されていません
更新
def visualise(X, y, predictions):
newY = np.zeros((X.shape[0], X.shape[1]));
newY[:, 0] = y
newY[:, 1] = y
plt.scatter(X, newY, color='blue')
plt.plot(X, predictions, color='red')
エラーが修正されましたが、これが私の出力です。
散布図をプロットして、予測の線をプロットするにはどうすればよいですか?
2つの機能があるため、予測線を引くことはできません。どちらかといえば、おそらく予測等高線図が必要です。
あなたの例は、ここにあるこの2つの機能を備えた例に非常に似ていますhttps://scikit-learn.org/stable/auto_examples/svm/plot_iris.html
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加