gpt4 book ai didi

python - 拟合LogisticRegression时发现输入变量样本数量不一致

转载 作者:行者123 更新时间:2023-11-30 09:35:08 25 4
gpt4 key购买 nike

我正在创建LogisticRegression分类器代码如下:

regressor = LogisticRegression()
regressor.fit(x_train, y_train)

x_trainy_train 形状都是

<class 'tuple'>: (32383,)

x_train 包含范围 [0..1] 附近的值,而 y_train 仅包含 01秒。

不幸的是,fit 失败并出现错误

ValueError: Found input variables with inconsistent numbers of samples: [1, 32383]

向参数添加转置没有帮助。

最佳答案

继续我在评论中提出的解决方案:问题在于 x_train 的形状。所以我们需要 reshape 它:

来自文档:

X:{类似数组,稀疏矩阵},形状(n_samples,n_features)

y:类似数组,形状 (n_samples,)

示例使用scikit-learnnumpy:

from sklearn.linear_model import LogisticRegression
import numpy as np

# create the tuple data
x_train = tuple(range(32383))
x_train = np.asarray(x_train)

#same for y_train
y_train=tuple(range(32383))
y_train = np.asarray(y_train)

#convert tuples to nparray and reshape the x_train
x_train = x_train.reshape(32383,1)

#check if shape if (32383,)
y_train.shape

#create the model
lg = LogisticRegression()

#Fit the model
lg.fit(x_train, y_train)

这应该可以正常工作。希望对您有帮助

关于python - 拟合LogisticRegression时发现输入变量样本数量不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44968992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com