<jupyter_start><jupyter_text>Feature engineering - CategoryEncoding - IndexersCategoryEncoding with sklearn on the Titanic dataset  ***  Environment`conda activate sklearn-env`*** Goals- Replace categorical and discrete columns with categorical elements corespondng to each value fomr dataset*** Referenceshttps://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OrdinalEncoder.html  Basic python imports<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import random
import seaborn as sns

# Make numpy printouts easier to read.
np.set_printoptions(precision=3, suppress=True)<jupyter_output><empty_output><jupyter_text>Dataset load using sklearn API from https://www.openml.org sitehttps://www.openml.org/d/40945<jupyter_code>from sklearn.datasets import fetch_openml

# Load data from https://www.openml.org/d/40945
raw_dataset = fetch_openml("titanic", version=1, as_frame=True).frame
dataset = raw_dataset.copy()
dataset.head(10)
dataset.drop(['boat', 'body', 'home.dest'],  axis=1, inplace=True)
dataset = dataset.dropna().copy()<jupyter_output><empty_output><jupyter_text>Verify categorical /discrete fields in datasetNotice:- `pclass` discrete fiels having 3 distinct values.- `sex` and `embarked` fields - categorical<jupyter_code>dataset.info()
from sklearn.preprocessing import OrdinalEncoder

encoder = OrdinalEncoder().fit(dataset[['pclass', 'sex', 'embarked']])
encoder.categories_
new_dataset = dataset.copy()
new_dataset[['pclass', 'sex', 'embarked']] = encoder.transform(new_dataset[['pclass', 'sex', 'embarked']])<jupyter_output><empty_output><jupyter_text>Notice changed column type for 'pclass', 'sex', 'embarked'<jupyter_code>new_dataset.head(10)<jupyter_output><empty_output><jupyter_text>Predict and compare results<jupyter_code>from sklearn.preprocessing import MinMaxScaler

def raw_dataset(df):
    dataset = df[['age','sibsp','parch', 'fare', 'survived']].copy()
    return dataset
    
def prepared_dataset(df):
    dataset = df.copy().drop(['name', 'fare', 'ticket', 'cabin'], axis='columns')
    dataset[['age']] = MinMaxScaler().fit_transform(dataset[['age']])
    return dataset

#use the prepared data set
dataset = prepared_dataset(new_dataset)

#or use the raw dataset
#dataset = raw_dataset(dataset)

train_dataset = dataset.sample(frac=0.8, random_state=0)
test_dataset = dataset.drop(train_dataset.index)

train_features = train_dataset.copy()
test_features = test_dataset.copy()

train_labels = train_features.pop('survived')
test_labels = test_features.pop('survived')
from sklearn.linear_model import LogisticRegression

logistic_regressor = LogisticRegression(max_iter=1000).fit(train_features, train_labels)

scored_test = logistic_regressor.predict(test_features)
scored_test_proba = logistic_regressor.predict_proba(test_features)
test_dataset['predicted'] = scored_test

print("Training converged in %d iterations" % (logistic_regressor.n_iter_))
from sklearn.metrics import accuracy_score
acc = accuracy_score(test_dataset['survived'], test_dataset['predicted'])
print("Accuracy of the model is %f" % acc)
test_dataset[['survived', 'predicted']].head(20)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12386042125117887, 'fraction_numerical': 0.008487896887771141, 'mean_word_length': 7.8635097493036215, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 3, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '163986', 'license': 'Apache-2.0', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1008, 'n_words': 214, 'path': 'notebooks/03_03_01_CategoryEncoding_SkLearn.ipynb', 'repo_name': 'datagridsoftware/practical-ml', 'size': '3181'}