Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rok-t opened this issue Aug 28, 2024 · 0 comments
Open

Comments

@Rok-t
Copy link

Rok-t commented Aug 28, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant