Questions icon indicating copy to clipboard operation
Questions copied to clipboard

ImportError: cannot import name 'oneHotEncoder' from 'sklearn.preprocessing'

Open Rok-t opened this issue 1 year ago • 0 comments

Please I urgently need help. I'm new in this field. I have spent about 3 hours going through different solutions but with no success. Below is an error i'm getting in jupyter notebook.

ImportError Traceback (most recent call last) Cell In[49], line 3 1 # Turn the categories into numbers 2 from sklearn.model_selection import train_test_split ----> 3 from sklearn.preprocessing import oneHotEncoder 4 from sklearn.compose import ColumnTransformer 6 categorical_features = ["Make", "Colour", "Doors"]

ImportError: cannot import name 'oneHotEncoder' from 'sklearn.preprocessing' (C:\Users\pc\Desktop\Sample_project_1\env\Lib\site-packages\sklearn\preprocessing_init_.py)

Please find below the code I ran.

car_sales = pd.read_csv("data/car-sales-extended.csv") car_sales.head()

Split into x/y

x = car_sales.drop("Price", axis =1) y =car_sales["Price"]

Split into training and test data

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)

Build a machine learning model

from sklearn.ensemble import RandomForestRegressor model = RandomForestRegressor() model.fit(x_train, y_train) model.score(x_test, y_test)

Turn the categories into numbers

`from sklearn.model_selection import train_test_split from sklearn.preprocessing import oneHotEncoder from sklearn.compose import ColumnTransformer

categorical_features = ["Make", "Colour", "Doors"] one_hot = oneHotEncoder() transformer =ColumnTransformer([("one_hot", one_hot, categorical_features)], remainder="passthrough")

transformed_x = transformer.fit_transform(x) transformed_x ``

This leads to


ImportError Traceback (most recent call last) Cell In[49], line 3 1 # Turn the categories into numbers 2 from sklearn.model_selection import train_test_split ----> 3 from sklearn.preprocessing import oneHotEncoder 4 from sklearn.compose import ColumnTransformer 6 categorical_features = ["Make", "Colour", "Doors"]

ImportError: cannot import name 'oneHotEncoder' from 'sklearn.preprocessing' (C:\Users\pc\Desktop\Sample_project_1\env\Lib\site-packages\sklearn\preprocessing\__init__.py)

Rok-t avatar Aug 28 '24 13:08 Rok-t