[英]sklearn error: “X and y have incompatible shapes.”
New to scikit learn. scikit学习的新手。 I'm trying to fit a logistic regression to some made up data but I get the error "X and y have incompatible shapes. X has 1 samples, but y has 6." 我正在尝试对一些组成的数据进行逻辑回归,但出现错误“ X和y具有不兼容的形状。X具有1个样本,但是y具有6”。
import pandas as pd
from sklearn.linear_model import LogisticRegression
# Create a sample dataframe
data = [['Age', 'ZepplinFan'], [13 , 0], [40, 1], [25, 0], [55, 0], [51, 1], [58, 1]]
columns=data.pop(0)
df = pd.DataFrame(data=data, columns=columns)
# Fit Logistic Regression
lr = LogisticRegression()
lr.fit(X=df.Age.values, y = df.ZepplinFan)
This post indicates that I need to somehow reshape df.Age.values to (n_samples, 1). 这篇文章表明我需要以某种方式将df.Age.values重塑为(n_samples,1)。 How do I do this? 我该怎么做呢?
Shape matters yes. 形状很重要。 One way to do it, is pass columns like 一种方法是传递像
In [24]: lr.fit(df[['Age']], df['ZepplinFan'])
Out[24]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001)
If you want to explicitly pass values then you could 如果要显式传递值,则可以
In [25]: lr.fit(df[['Age']].values, df['ZepplinFan'].values)
Out[25]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001)
Or you could newaxis
to you existing syntax like 或者您可以newaxis
来使用现有语法,例如
In [26]: lr.fit(df.Age.values[:,np.newaxis], df.ZepplinFan.values)
Out[26]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.