【问题标题】:ValueError: Found input variables with inconsistent numbers of samples: [1, 700]ValueError:发现样本数量不一致的输入变量:[1, 700]
【发布时间】:2020-11-15 09:11:36
【问题描述】:

我正在对 kaggle 提供的关于泰坦尼克号幸存者预测的数据执行线性回归。我正在尝试预测幸存者列表,所以即使在我重塑 Y 后我仍然会收到此错误,但它仍然显示此错误。

from sklearn.linear_model import LogisticRegression
from csv import reader
import numpy as np

file = open('train.csv', "r")
lines = reader(file)
X = list(lines)
#Deleting unnecessary features
X=np.delete(X, (0), axis=0)
X=np.delete(X, (0), axis=1)
X=np.delete(X, (2), axis=1)
X=np.delete(X, (3), axis=1)
X=np.delete(X, (5), axis=1)
X=np.delete(X, (5), axis=1)
X=np.delete(X, (5), axis=1)
X=np.delete(X, (5), axis=1)
#Converting males to 1 and females to 0
for i in range(891):
   if X[i][2]== 'male':
       X[i][2]=1
   else:
       X[i][2]=0
Y=X.T[0]
#Converting strings to float
X1 = X.astype(np.float) 
Y1 = Y.astype(np.float)
Xw=X1.reshape(-1,1)
split = 700
train,test = Xw[:split,:],Xw[split:,:]
Ytrain,Ytest = Y1[:split],Y1[:split]
logisticRegr = LogisticRegression()
logisticRegr.fit(train.T, Ytrain)
logisticRegr.predict(test[0].T.reshape(1,-1))
score = logisticRegr.score(test.T, Ytest)

【问题讨论】:

  • 你试过只用'train'代替'train.T'吗?
  • 是的,我也试过了。

标签: python-3.x numpy machine-learning scikit-learn linear-regression


【解决方案1】:

我强烈建议您熟悉pandas 库进行数据处理,您可以尝试以下方法:

# import
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics

import pandas as pd
df = pd.read_csv('train.csv')

# convert to male/female, lets say the column is called as gender
df['gender'] = df['gender'].map({'male': 0, 'female': 1})

trainX, testX, trainY, testY = train_test_split(df, df['Survived'], train_size=700, stratify = df['Survived'],)

logisticRegr = LogisticRegression()
logisticRegr.fit(trainX, trainY)

preds = logisticRegr.predict(testX)
score = metrics.accuracy_score(testY, preds)

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2021-07-29
    • 2021-10-09
    • 2020-07-05
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多