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's example
. 这是我试图复制的:
我想也许,因为我有两个特征,我可能需要分别识别它们。
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()
newY = np.zeros(X.shape[0], X.shape[1]);
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')
现在修复错误,但这是我的输出:
如何绘制散点图和预测线?