使用 sklearn 进行逻辑回归

问题描述 投票:0回答:1

我一直在使用泰坦尼克号数据集。当我尝试拟合数据时,我遇到了 TypeError。

**Step 4: Train & Test Data**: Build the model on the train and predict the output on the test data.

**Train Data**

X=titanic_data.drop("Survived", axis= 1)
y= titanic_data['Survived']

from sklearn.model_selection import train_test_split
#It must relate to the renaming and deprecation of cross_validation sub-module to model_selection. Try substituting cross_validation to model_selection

X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.33, random_state=42)

from sklearn.linear_model import LogisticRegression

logmodel=LogisticRegression()

logmodel.fit(X_train, y_train)
TypeError: Feature names are only supported if all input features have string names, but your input has ['int', 'str'] as feature name / column name types. If you want feature names to be stored and validated, you must convert them all to strings, by using X.columns = X.columns.astype(str) for example. Otherwise you can remove feature / column names from your input data, or convert them all to a non-string data type.```
scikit-learn logistic-regression
1个回答
0
投票

该错误表明列名称存在问题。有些名称是字符串,有些名称是数字。添加

X.columns = X.columns.astype(str)
会将所有列 names 转换为
str
类型(因此整数 0 将变成字符串“0”等)。

© www.soinside.com 2019 - 2024. All rights reserved.