【发布时间】:2021-06-29 21:36:28
【问题描述】:
我正在尝试使用逻辑回归拟合数据,但出现值错误。
我正在使用来自 sklearn 的 iris 数据集:
# The data is in iris["data"] and target in iris["target"]
# For this section, we will work with a single feature 'petal width'
# which is the last (fourth) feature in iris["data"]
# We will assign class y=1 if the target's value is 2 and 0 otherwise
from sklearn.datasets import load_iris
import numpy as np
iris = load_iris()
# petal width
X = np.array([len(iris["data"]),1]).reshape(-1,1)
# 1 if Iris virginica, else 0
y = []
for x in iris["target"]:
if x == 2.0:
y.append(1)
else:
y.append(0)
y = np.array(y)
# Import the LogisticRegression class from scikit learn
from sklearn.linear_model import LogisticRegression
# Initialize the LogisticRegression class, use lbfgs solver and random state of 42
log_reg = LogisticRegression(solver='lbfgs', random_state=42)
# Fit the data
log_reg.fit(X, y)
这是我到达的地方
ValueError: Found input variables with inconsistent numbers of samples: [2, 150]
不确定是我的 x 还是 y 设置不正确?
【问题讨论】:
-
您能打印出
y = np.array(y)之后的x 和y 的形状并将其添加到您的问题中吗?根据我的经验,这个错误通常与不正确的尺寸有关 -
欢迎来到 SO;如果答案解决了您的问题,请接受 - 请参阅What should I do when someone answers my question?
标签: python scikit-learn logistic-regression