简体   繁体   English

sklearn错误:“ X和y的形状不兼容。”

[英]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.

相关问题 X和y的形状不兼容 - X and y have incompatible shapes Sklearn Logistic回归形状错误,但x,y形状一致 - Sklearn logistic regression shape error, but x, y shapes are consistent scikit-learn文本文档的分类ValueError:X和y的形状不兼容 - scikit-learn Classification of text documents ValueError: X and y have incompatible shapes x 和 y 必须具有相同的第一维,但形状 (40,) 和 (1, 80) 错误 - x and y must have same first dimension, but have shapes (40,) and (1, 80) error Matplotlib 错误“x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (6,)” - Matplotlib error "x and y must have same first dimension, but have shapes (1,) and (6,)" Python错误:x和y必须具有相同的一维,但形状为(8,)和(1,) - Python Error: x and y must have same first dimension, but have shapes (8,) and (1,) Matplotlib 错误:x 和 y 必须具有相同的第一维,但具有形状 (100,) 和 (449,) - Matplotlib Error: x and y must have same first dimension, but have shapes (100,) and (449,) 我收到此错误:“fx 和 y 必须具有相同的第一维但具有 python 形状” - I got this error: "f x and y must have same first dimension but have shapes python" 错误消息:x和y必须具有相同的第一尺寸,但形状为(23,)和(1,) - Error message : x and y must have same first dimension, but have shapes (23,) and (1,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM