简体   繁体   中英

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)

Why this Type error is coming i don't know??

TypeError: All estimators should implement fit and transform, or can be 'drop' or 'passthrough' specifiers. '['Odometer (KM)']' (type <class 'list'>) doesn't.

You wrongly swapped parameters in:

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

It should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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