gpt4 book ai didi

python - Sklearn 列转换器 ValueError : could not convert string to float:

转载 作者:行者123 更新时间:2023-12-05 06:17:21 25 4
gpt4 key购买 nike

我正在尝试使用 Column Transformer with Mixed Types 创建管道来 self 数据集中 scikit-learn 页面的示例,但我收到错误:ValueError: could not convert string to float: 'Male'.

from sklearn.compose import ColumnTransformer, make_column_transformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.compose import make_column_selector as selector
from sklearn.linear_model import LogisticRegression

numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
])

categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder())
])


preprocessor = ColumnTransformer(transformers=[
('num', numeric_transformer, selector(dtype_exclude=["category",'object'])),
('cat', categorical_transformer, selector(dtype_include=["category",'object']))
])

X = train.drop(['Loan_Status', 'Loan_ID'], axis=1)
y = train['Loan_Status']


x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=101)

pipeline = Pipeline(steps = [('preprocessor', preprocessor),
('classifier',LogisticRegression())
])

pipeline.fit(x_train, y_train)
score = clf.score(x_test, y_test)

我已经阅读了其他具有相同错误的相关帖子,但所有其他发生在 fit mine 发生在分数评估中。

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

<ipython-input-19-df09dd400283> in <module>()
4
5 pipeline.fit(x_train, y_train)
----> 6 score = clf.score(x_test, y_test)

4 frames

/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
83
84 """
---> 85 return array(a, dtype, copy=False, order=order)
86
87

ValueError: could not convert string to float: 'Male'

这是数据集数据类型的概述:

Loan_ID               object
Gender object
Married object
Dependents object
Education object
Self_Employed object
ApplicantIncome int64
CoapplicantIncome float64
LoanAmount float64
Loan_Amount_Term float64
Credit_History float64
Property_Area object
Loan_Status object
dtype: object

还有数据集的前几行: enter image description here

最佳答案

改变

preprocessor = ColumnTransformer(transformers=[
('num', numeric_transformer, selector(dtype_exclude=["category",'object'])),
('cat', categorical_transformer, selector(dtype_include=["category",'object']))
])

preprocessor = ColumnTransformer(transformers=[
('num', numeric_transformer, selector(dtype_exclude=object)),
('cat', categorical_transformer, selector(dtype_include=object))
])

引用:https://scikit-learn.org/stable/modules/generated/sklearn.compose.make_column_selector.html

关于python - Sklearn 列转换器 ValueError : could not convert string to float:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61718716/

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