scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.preprocessing.OneHotEncoder.html
OneHotEncoder — scikit-learn 1.9.1 documentation
Examples · Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to a binary one-hot encoding. >>> from sklearn.preprocessing import OneHotEncoder ·
GeeksforGeeks
geeksforgeeks.org › machine learning › ml-one-hot-encoding
One Hot Encoding in Machine Learning - GeeksforGeeks
import pandas as pd from sklearn.preprocessing import OneHotEncoder data = { 'Employee_ID': [10, 20, 15, 25, 30], 'Gender': ['M', 'F', 'F', 'M', 'F'], 'Remarks': ['Good', 'Nice', 'Good', 'Great', 'Nice'] } df = pd.DataFrame(data) print("Original Data:") print(df) categorical_columns = df.select_dtypes(include=['object']).columns encoder = OneHotEncoder(sparse_output=False) encoded_data = encoder.fit_transform(df[categorical_columns]) encoded_df = pd.DataFrame( encoded_data, columns=encoder.get_feature_names_out(categorical_columns) ) final_df = pd.concat( [df.drop(columns=categorical_columns), encoded_df], axis=1 ) print("\nOne-Hot Encoded Data:") print(final_df)
Published: May 29, 2026
10:45
Data Preprocessing 06: One Hot Encoding python | Scikit Learn | ...
01:43
Quick explanation: One-hot encoding - YouTube
12:11
Step-by-Step Guide to One Hot Encoding in Python | Machine Learning ...
12:51
One Hot Encoding in Python | Machine Learning | OneHotEncoder ...
12:47
Scikit-Learn One Hot Encoder and get_dummies | Data Encoding ...
- YouTube
DataCamp
datacamp.com › tutorial › one-hot-encoding-python-tutorial
What Is One Hot Encoding and How to Implement It in Python | DataCamp
June 26, 2024 - Scikit-learn's OneHotEncoder can handle unknown categories by ignoring them or assigning them to a dedicated column, ensuring the model can still process new data effectively. This example demonstrates how to fit the encoder on the training data and then transform both training and test data, including handling categories that were not present in the training set.
Built In
builtin.com › articles › one-hot-encoding
One Hot Encoding Explained | Built In
February 15, 2024 - Another common step, when using sklearn is to do the conversion between raw NumPy arrays and Pandas DataFrames. You can either use sklearn.compose.make_column_transformer for this, or implement it manually, using the .get_feature_names_out() method of OneHotEncoder to give you the column names for the new features. Let’s see examples for both of these.
Codecademy
codecademy.com › article › what-is-one-hot-encoding-and-how-to-implement-it-in-python
What is One Hot Encoding and How to Implement it in Python? | Codecademy
After saving, you can reload the encoder from the file and perform one-hot encoding, as shown in the following example: encoder_file = "trained_color_segment_encoder.pkl" ... As you can see, the loaded encoder is used exactly the same as the ...
Educative
educative.io › blog › one-hot-encoding
Data Science in 5 Minutes: What is One Hot Encoding?
What is one hot encoding?Why use one hot encoding?How to read this decision treeHandling high-cardinality categorical featuresWhat does “high cardinality” mean?Why one-hot encoding becomes problematicSmall vs large category exampleBetter alternatives for high-cardinality featuresTarget encodingFrequency/count encodingHash encodingEmbedding layersWhen should you use each approach?Practical recommendationSparse matrices and memory efficiencyWhy one-hot encoding wastes memoryWhat is a sparse matrix?How Sklearn handles sparse outputDense vs sparse intuitionExample 1: Dense output with PandasOu
scikit-learn
scikit-learn.org › dev › modules › generated › sklearn.preprocessing.OneHotEncoder.html
OneHotEncoder — scikit-learn 1.10.dev0 documentation
Examples · Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to a binary one-hot encoding. >>> from sklearn.preprocessing import OneHotEncoder ·
Trainindata
feature-engine.trainindata.com › en › latest › user_guide › encoding › OneHotEncoder.html
OneHotEncoder — 1.9.4 - Feature-engine
Let’s look at an example of one hot encoding, using Feature-engine’s OneHotEncoder() utilizing the Titanic Dataset.
Statology
statology.org › home › how to perform one-hot encoding in python
How to Perform One-Hot Encoding in Python
September 28, 2021 - from sklearn.preprocessing import OneHotEncoder #creating instance of one-hot-encoder encoder = OneHotEncoder(handle_unknown='ignore') #perform one-hot encoding on 'team' column encoder_df = pd.DataFrame(encoder.fit_transform(df[['team']]).toarray()) #merge one-hot encoded columns back with original DataFrame final_df = df.join(encoder_df) #view final df print(final_df) team points 0 1 2 0 A 25 1.0 0.0 0.0 1 A 12 1.0 0.0 0.0 2 B 15 0.0 1.0 0.0 3 B 14 0.0 1.0 0.0 4 B 19 0.0 1.0 0.0 5 B 23 0.0 1.0 0.0 6 C 25 0.0 0.0 1.0 7 C 29 0.0 0.0 1.0
scikit-learn
scikit-learn.org › 0.16 › modules › generated › sklearn.preprocessing.OneHotEncoder.html
sklearn.preprocessing.OneHotEncoder — scikit-learn 0.16.1 documentation
Examples · Given a dataset with three features and two samples, we let the encoder find the maximum value per feature and transform the data to a binary one-hot encoding. >>> from sklearn.preprocessing import OneHotEncoder >>> enc = OneHotEncoder() >>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, ...
scikit-learn
scikit-learn.org › 0.20 › modules › generated › sklearn.preprocessing.OneHotEncoder.html
sklearn.preprocessing.OneHotEncoder — scikit-learn 0.20.4 documentation
Examples · Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to a binary one-hot encoding. >>> from sklearn.preprocessing import OneHotEncoder >>> enc = OneHotEncoder(handle_unknown='ignore') >>> X = [['Male', 1], ['Female', 3], ...
scikit-learn
scikit-learn.org › 0.19 › modules › generated › sklearn.preprocessing.OneHotEncoder.html
sklearn.preprocessing.OneHotEncoder — scikit-learn 0.19.2 documentation
Examples · Given a dataset with three features and four samples, we let the encoder find the maximum value per feature and transform the data to a binary one-hot encoding. >>> from sklearn.preprocessing import OneHotEncoder >>> enc = OneHotEncoder() >>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, ...
Saturn Cloud
saturncloud.io › blog › pandas-vs-scikitlearn-onehot-encoding-dataframes
Pandas vs. Scikit-learn: One-Hot Encoding Dataframes | Saturn Cloud Blog
May 1, 2026 - import pandas as pd from sklearn.preprocessing import OneHotEncoder # create example dataframe data = {'color': ['red', 'blue', 'green', 'red', 'blue']} df = pd.DataFrame(data) # create OneHotEncoder object encoder = OneHotEncoder() # fit and transform color column one_hot_array = encoder.fit_transform(df[['color']]).toarray() # create new dataframe from numpy array one_hot_df = pd.DataFrame(one_hot_array, columns=encoder.get_feature_names()) print(one_hot_df)
Medium
datasensei.medium.com › how-to-transform-nominal-data-for-ml-with-onehotencoder-from-scikit-learn-f6febfefb3c6
How to Transform Nominal Data for ML with OneHotEncoder from Scikit-Learn | by Data Seito | Medium
January 18, 2022 - The purpose of this article is twofold: first, to give a brief overview of one-hot encoding categorical data, and second to demonstrate how to one-hot encode categorical data using scikit-learn’s OneHotEncoder class. In order for categorical data to be utilized by a machine learning algorithm, it must first be encoded into a numerical form. Categorical variables come in 1 of 2 flavors — ordinal or nominal — and each of these cases should be treated differently. Ordinal data has an inherent ordering between the labels that can easily be converted into a numerical form (small=1, medium=2, or large=3 for example), while nominal data does not (if Nike = 1 and Adidas = 2 does that mean Adidas > Nike?).
scikit-learn
scikit-learn.org › 0.21 › modules › generated › sklearn.preprocessing.OneHotEncoder.html
sklearn.preprocessing.OneHotEncoder — scikit-learn 0.21.3 documentation
>>> enc.categories_ [array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)] >>> enc.transform([['Female', 1], ['Male', 4]]).toarray() array([[1., 0., 1., 0., 0.], [0., 1., 0., 0., 0.]]) >>> enc.inverse_transform([[0, 1, 1, 0, 0], [0, 0, 0, 1, 0]]) array([['Male', 1], [None, 2]], dtype=object) >>> enc.get_feature_names() array(['x0_Female', 'x0_Male', 'x1_1', 'x1_2', 'x1_3'], dtype=object) >>> drop_enc = OneHotEncoder(drop='first').fit(X) >>> drop_enc.categories_ [array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)] >>> drop_enc.transform([['Female', 1], ['Male', 2]]).toarray() array([[0., 0., 0.], [1., 1., 0.]])