【发布时间】:2019-01-08 09:02:14
【问题描述】:
我试图根据 X 中包含的两个特征来预测 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'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 数组,因为 X 有两个特征,y 有 1,所以形状不同。但是现在我在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')
现在修复错误,但这是我的输出:
如何绘制散点图并为预测绘制一条线?
【问题讨论】:
-
请至少提供一个拒绝投票的理由,我提供了一个 MCVE,我尝试过的所有东西,研究过,我还在尝试吗?但是线条正在变得精神上
标签: python pandas numpy logistic-regression