简体   繁体   中英

sklearn error: “X and y have incompatible shapes.”

New to scikit learn. 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."

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). 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM