<jupyter_start><jupyter_text>Encode a categorical variable using ML model<jupyter_code>#Import required packages
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Read the data
dataset = pd.read_excel('/Users/swaruptripathy/Desktop/Data Science and AI/datasets/stark_data.xlsx')
dataset.head()
dataset.shape
X = dataset.iloc[:,:-1].values
X
#Impute missing value using sklearn imputer from preprocessing
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer.fit(X[:, 1:2])
X[:, 1:2] = imputer.transform(X[:, 1:2])
X[:, 1:2]
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
#column gender encoding
X[:,2] = labelencoder_X.fit_transform(X[:,2])
X[:,2]
#column name encoding
X[:,0] = labelencoder_X.fit_transform(X[:,0])
X[:,0]
X
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
X
X[:,1:] #Dropping the dummy variable<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13372093023255813, 'fraction_numerical': 0.01744186046511628, 'mean_word_length': 7.133858267716535, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '193141', 'license': 'MIT', 'n_tokens_mistral': 363, 'n_tokens_neox': 366, 'n_words': 92, 'path': 'data_preprocessing - Categorical Encoding Scikit.ipynb', 'repo_name': 'swaruptripathy/DataScience-Python', 'size': '1032'}