繁体   English   中英

不知道为什么要来?

[英]Don't know why this comming?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.read_csv("car-sales-extended.csv")
df.head()
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn. impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler
# Modelling
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
np.random.seed(42)
df.dropna(subset=["Price"], inplace=True)
categorical_features=["Make", "Colour"]
categorical_transformer=Pipeline(steps=[
    ("imputer",SimpleImputer(strategy="constant", fill_value="missing")),
    ("Onehot", OneHotEncoder(handle_unknown="ignore"))])
door_feature=["Doors"]
door_transformer=Pipeline(steps=[
    ("imputer",SimpleImputer(strategy="constant", fill_value=4)),
    ])
numeric_features=["Odometer (KM)"]
numeric_transformer = Pipeline(steps=[
    ("imputer", SimpleImputer(strategy="mean")),
    ("scaler", StandardScaler())])
# Setup preprocessing steps (fill the missing values, then convert to numbers)
preprocessor = ColumnTransformer(
                      transformers=[(
                      "cat", categorical_transformer, categorical_features),
                      ("door", door_transformer, door_feature),
                      ("num", numeric_features, numeric_transformer)])
#creating a preprocessing and modelling pipeline
model=Pipeline(steps=[("preprocessing", preprocessor),
                     ("model", RandomForestClassifier())])
# Split data
x=df.drop("Price", axis=1)
y=df["Price"]
x_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2)
# Fit and score the model
model.fit(x_train, y_train)

model.score(x_test, y_test)

我不知道为什么会出现这种类型错误?

TypeError:所有估算器都应该实现拟合和变换,或者可以是“drop”或“passthrough”说明符。 '['Odometer (KM)']' (type <class 'list'>) 没有。

您错误地交换了以下参数:

preprocessor = ColumnTransformer(
                      transformers=[(
                      "cat", categorical_transformer, categorical_features),
                      ("door", door_transformer, door_feature),
                      ("num", numeric_features, numeric_transformer)])

它应该是:

preprocessor = ColumnTransformer(
                      transformers=[(
                      "cat", categorical_transformer, categorical_features),
                      ("door", door_transformer, door_feature),
                      ("num",numeric_transformer, numeric_features )])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM