valueerror:拟合逻辑回归模型时输入形状错误

mqxuamgl  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(445)

我目前正在从头开始学习逻辑回归。我正在创建一个逻辑回归模型的基础上改变形式的虹膜数据集我。以下是我的代码摘录:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

df = pd.read_excel('Iris_Dataset.xlsx')
df = df.dropna()

colour = pd.get_dummies(df.colour, drop_first = True)
species = pd.get_dummies(df.species, drop_first = True)
df = pd.concat([df, colour, species], axis = 1)
df = df.drop(['colour', 'species'], axis = 1)

x = df.drop(["versicolor", "virginica"], axis = 1)
y = pd.concat([df.versicolor, df.virginica], axis = 1)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 1)

model = LogisticRegression()

model.fit(x_train, y_train)

出于某种原因,在最后一条语句(model.fit(x_train,y_train))中,我得到了以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-0c77380c6ea5> in <module>()
     18 model = LogisticRegression()
     19 
---> 20 model.fit(x_train, y_train)

/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in column_or_1d(y, warn)
    795         return np.ravel(y)
    796 
--> 797     raise ValueError("bad input shape {0}".format(shape))
    798 
    799 

ValueError: bad input shape (86, 2)

我无法理解错误的含义,也无法理解为什么我会出现这种错误,无论我如何尝试。请帮我做这个。
顺便说一下,这是数据集。它是google表单的形式,但我在microsoft excel中粘贴了与iris_dataset.xlsx相同的数据集副本(我不知道如何直接共享excel文件):https://docs.google.com/spreadsheets/d/18zkzvpq5q_exawu4dywshtpo9d6ecdmxzvzahpryiqu/edit?usp=sharing
提前谢谢。
编辑:所以我尝试了其他方法,这次我只将颜色列转换为虚拟值,并保持物种完整。代码如下:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

df = pd.read_excel('Iris_Dataset.xlsx')
df = df.dropna()

colour = pd.get_dummies(df.colour, drop_first = True)
df = pd.concat([df, colour], axis = 1)
df = df.drop(['colour'], axis = 1)

x = df.drop(["species"], axis = 1)
y = df.species

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 1)

model = LogisticRegression()

model.fit(x_train, y_train)

我这样做只是为了颜色,唯一的原因是当我在没有虚拟值的情况下尝试相同的方法时(我尝试将“物种”列作为一个整体传递,而不是我之前所做的),是因为我得到了错误: could not convert string to float: 'violet' 因此,在执行上述代码后,我得到了输出,但拟合过程在输出的同时给出了一条警告消息:

/usr/local/lib/python3.7/dist-packages/sklearn/linear_model/_logistic.py:940: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                   intercept_scaling=1, l1_ratio=None, max_iter=100,
                   multi_class='auto', n_jobs=None, penalty='l2',
                   random_state=None, solver='lbfgs', tol=0.0001, verbose=0,
                   warm_start=False)

接下来的代码有点流畅,但是我想知道消息的含义,或者有什么方法可以避免它?

ybzsozfc

ybzsozfc1#

您正在输入一个包含两列的 Dataframe y 作为你的目标向量 LogisticRegression.fit() ,这是不可能的。要求目标向量的形状(n_个样本,)为 Dataframe 术语中的sinlge列。
以下是文档中的一个示例:

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
clf = LogisticRegression(random_state=0).fit(X, y)
clf.predict(X[:2, :])

clf.predict_proba(X[:2, :])

clf.score(X, y)

印刷品 y 你会看到它是一个一维数组。
https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.logisticregression.html

相关问题