gpt4 book ai didi

python - 在进行逻辑回归时,如何解决Python中的值错误?

转载 作者:行者123 更新时间:2023-12-01 07:23:17 24 4
gpt4 key购买 nike

我在逻辑回归中遇到值错误。如何解决此问题?

我尝试删除幸存的列,但仍然没有用。

输入:

X_train=train_df.drop("Survived",axis=1)
Y_train=train_df["Survived"]
X_test=test_df.drop("PassengerId",axis=1).copy()
X_train=train_df.drop("PassengerId",axis=1).copy()
X_train.head()
Y_train.head()
X_test.head()

输出:

Pclass----Sex-----Age-------Parch-----Fare-------EMbarked
3--------- 0 -----34.5------0---------7.82-------2
3--------- 1 -----47 ------0---------7----------0
2--------- 0 -----62 ------0---------9.68-------2
3--------- 0 -----27 ------0---------8.66-------0
3--------- 1 -----22 ------1---------12.2-------0

输入:

X_train.shape,Y_train.shape,X_test.shape

输出:

((891, 7), (891,), (418, 6))

输入:

 X_train.head()

输出:

Column1---Survived---Pclass----Sex----Age-----Parch----Fare----Embarked
0-------- ----0----------3-------0-----22-------0------7.25------0
1-------------1----------1-------1-----38-------0------71.2833---1
2-------------1----------3-------1-----26-------0------7.925-----0
3-------------1----------1-------1-----35-------0------53.1------0
4-------------0----------3-------0-----35-------0---- -8.05------0

逻辑回归

logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
Y_pred = logreg.predict(X_test)
acc_log = round(logreg.score(X_train, Y_train) * 100, 2)
acc_log

错误消息:

ValueError                                Traceback (most recent call last)
<ipython-input-64-5854ca91fc64> in <module>

3 logreg = LogisticRegression()
4 logreg.fit(X_train, Y_train)
----> 5 Y_pred = logreg.predict(X_test)
6 acc_log = round(logreg.score(X_train, Y_train) * 100, 2)
7 acc_log

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\sklearn\linear_model\base.py in predict(self, X)

287 Predicted class label per sample.
288 """
--> 289 scores = self.decision_function(X)
290 if len(scores.shape) == 1:
291 indices = (scores > 0).astype(np.int)

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\sklearn\linear_model\base.py in decision_function(self, X)

268 if X.shape[1] != n_features:
269 raise ValueError("X has %d features per sample;
expecting %d"
--> 270 % (X.shape[1], n_features))
271
272 scores = safe_sparse_dot(X, self.coef_.T,

ValueError: X has 6 features per sample; expecting 7

最佳答案

X_train 和 Y_train 应该具有相同的特征集。您的 X_train 中有不必要的“幸存”特征列。

更好的方法是以这种格式从数据框中提取必要的列。

necessary_columns = ['Pclass', 'Sex', 'Age', 'Parch', 'Fare', 'EMbarked']
X_train = train_df[necessary_columns]
Y_train = train_df["Survived"]
X_test = test_df[necessary_columns]

关于python - 在进行逻辑回归时,如何解决Python中的值错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57575882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com