Python SKlearn逻辑回归错误输入形状错误

Python SKlearn逻辑回归错误输入形状错误,python,scikit-learn,logistic-regression,Python,Scikit Learn,Logistic Regression,我如何消除这个错误 from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import pandas as pd df = pd.read_csv("train.csv") clean = {"Sex": {"male":1, "female":0}} df.replac

我如何消除这个错误

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd

df =  pd.read_csv("train.csv")

clean = {"Sex": {"male":1, "female":0}} 
df.replace(clean, inplace = True)
df["label"] = df['Survived']
df =  df.drop(["Name","Ticket","Cabin","Embarked","Fare","Parch","Survived"],  axis = 1)
df = df.dropna(axis = 0, how="any")

X = df.drop(["label"],axis = 1).values
y = df["label"].values

X_train , y_train, X_test, y_test = train_test_split(X, y, test_size = 0.7)

log_reg  = LogisticRegression()
log_reg.fit(X_train, y_train)
print("Accuracy on test subset: (:.3f)".format(log_reg.score(X_train, y_train)))

ERROR
Traceback (most recent call last):
  File "C:\Users\user\Documents\17\kaggle'\logistic.py", line 20, in <module>
    log_reg.fit(X_train, y_train)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\linear_model\logistic.py", line 1216, in fit
    order="C")
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\utils\validation.py", line 547, in check_X_y
    y = column_or_1d(y, warn=True)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\utils\validation.py", line 583, in column_or_1d
    raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (500, 5)
来自sklearn.linear\u模型导入逻辑回归
从sklearn.model\u选择导入列车\u测试\u拆分
将matplotlib.pyplot作为plt导入
作为pd进口熊猫
df=pd.read\U csv(“train.csv”)
干净={“性别”:{“男性”:1,“女性”:0}
df.更换(清洁,就地=正确)
df[“标签”]=df[“幸存”]
df=df.下降([“姓名”、“车票”、“客舱”、“上船”、“车费”、“停车”、“幸存”],轴=1)
df=df.dropna(轴=0,how=“any”)
X=df.drop([“标签”],轴=1)。值
y=df[“标签”]。值
X_系列,y_系列,X_测试,y_测试=系列测试分割(X,y,测试尺寸=0.7)
log_reg=LogisticRegression()
日志注册匹配(X\U系列、y\U系列)
打印(“测试子集的精度:(:.3f)”。格式(日志记录分数(X\U序列,y\U序列)))
错误
回溯(最近一次呼叫最后一次):
文件“C:\Users\user\Documents\17\kaggle'\logistic.py”,第20行,在
日志注册匹配(X\U系列、y\U系列)
文件“C:\Users\user\AppData\Local\Programs\Python36-32\lib\site packages\sklearn\linear\u model\logistic.py”,第1216行
order=“C”)
文件“C:\Users\user\AppData\Local\Programs\Python36-32\lib\site packages\sklearn\utils\validation.py”,第547行,在check\X\y中
y=列_或_1d(y,警告=真)
文件“C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site packages\sklearn\utils\validation.py”,第583行,在列\u或\u 1d中
raise VALUERROR(“错误的输入形状{0}”。格式(形状))
ValueError:输入形状不正确(500,5)

错误是由以下原因造成的:

X_train , y_train, X_test, y_test = train_test_split(X, y, test_size = 0.7)
这不是
train\u test\u split
返回的结果

实际使用应为:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.7)
train\u test\u split
将按照提供的数据顺序返回分割的数组。因此X将被分成
X\u列,X\u测试
并首先返回,然后y将作为
y\u列y\u测试
返回。
希望这有帮助。

错误是由以下原因造成的:

X_train , y_train, X_test, y_test = train_test_split(X, y, test_size = 0.7)
这不是
train\u test\u split
返回的结果

实际使用应为:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.7)
train\u test\u split
将按照提供的数据顺序返回分割的数组。因此X将被分成
X\u列,X\u测试
并首先返回,然后y将作为
y\u列y\u测试
返回。
希望这有帮助。

在熊猫数据框上使用
值后,您能告诉我们
X.shape
y.shape
是什么吗?看起来它们应该具有正确的形状和类型(分别是2D和1D numpy.array),我认为这会有所不同的唯一原因是数据帧为空(例如,dropna()后没有行)。X.shape给出(714,5)和y.shape给出(714,)。在使用熊猫数据框上的
值后,能否向我们展示
X.shape
y.shape
是什么?看起来它们应该具有正确的形状和类型(分别是2D和1D numpy.array),我认为这会有所不同的唯一原因是数据帧为空(例如,dropna()后没有行)。X.shape给出(714,5)和y.shape给出(714,)。