<jupyter_start><jupyter_text>LOAD DATA<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from pandas.plotting import scatter_matrix
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score, precision_recall_curve
from sklearn.model_selection import train_test_split, cross_val_predict, GridSearchCV
from sklearn.preprocessing import StandardScaler, OneHotEncoder, OrdinalEncoder


"""
survival = 0(No) / 1(yes)
pclass = [1: 1sr class, 2: 2nd class, 3: 3rd class]
sex = M / F
Age --
sibsp = The number of siblings and/or spouses aboard the titanic
parch = The number of parents and/or children on the titanic
ticket = The ticket number
fare = The passenger fare
cabin = The cabin number
embarked = The port of Embarkation [C=Cherbourg, Q=Queenstown, S=Southampton]

"""


all_data =pd.read_csv("train.csv", delimiter=",")

training_set, testing_set = train_test_split(all_data, test_size=0.2, random_state=42)

training_y = training_set["Survived"]
training_X = training_set.drop(["Survived", "PassengerId", "Name", "Ticket", "Cabin"], axis=1)

testing_y = testing_set["Survived"]
testing_X = testing_set.drop(["Survived", "PassengerId", "Name", "Ticket", "Cabin"], axis=1)
testing_set.head(10)<jupyter_output><empty_output><jupyter_text>PREPROCESING<jupyter_code># Step 1: handle missing data
#    Imputer will populate missing values for numerical data
#    Std_Scaler will scale values so to be in similar ranges

numerical_columns = ["Pclass", "Age", "SibSp", "Parch", "Fare"]
categorical_columns = ["Sex", "Embarked"]

numerical_pipeline = Pipeline([("Imputer", SimpleImputer(strategy="median")),
                              ("Std_Scaler", StandardScaler())])

training_X["Embarked"].fillna(training_X["Embarked"].value_counts().keys()[0], inplace=True)
testing_X["Embarked"].fillna(testing_X["Embarked"].value_counts().keys()[0], inplace=True)
full_pipeline = ColumnTransformer([("numerical", numerical_pipeline, numerical_columns),
                                  ("categorical", OneHotEncoder(), categorical_columns)])
preprocessed_training_data = full_pipeline.fit_transform(training_X)
preprocessed_testing_data = full_pipeline.transform(testing_X)
processed_training_X = pd.DataFrame(preprocessed_training_data)
processed_testing_X = pd.DataFrame(preprocessed_testing_data)
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVR

clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(processed_training_X, training_y)

predictions = clf.predict(processed_testing_X)
y_pred = cross_val_predict(clf, processed_training_X, training_y, cv=3)
confusion = confusion_matrix(training_y, y_pred)
confusion
f1 = f1_score(training_y, y_pred)
f1
param_grid = [
        {'kernel': ['linear'], 'C': [10., 30., 100., 300., 1000., 3000., 10000., 30000.0]},
        {'kernel': ['rbf'], 'C': [1.0, 3.0, 10., 30., 100., 300., 1000.0],
         'gamma': [0.01, 0.03, 0.1, 0.3, 1.0, 3.0]},
    ]

svm_reg = SVR()
grid_search = GridSearchCV(svm_reg, param_grid, cv=5, scoring='neg_mean_squared_error', verbose=2, n_jobs=-1)
grid_search.fit(processed_training_X, training_y)
grid_search.best_params_
classifier = SVR(kernel="rbf", gamma=0.1, C=1.0)
classifier.fit(processed_training_X, training_y)

y_pred = cross_val_predict(classifier, processed_training_X, training_y, cv=3)

f1 = f1_score(training_y, y_pred)
f1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12387892376681614, 'fraction_numerical': 0.02858744394618834, 'mean_word_length': 6.254065040650406, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '633621', 'license': 'Apache-2.0', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1226, 'n_words': 273, 'path': 'Chapter 3 - Classification/.ipynb_checkpoints/Chapter 3 - Exercise 3-checkpoint.ipynb', 'repo_name': 'Jadams29/Hands-On-Machine-Learning-with-Scikit-Learn-Keras-Tensorflow-2nd', 'size': '3568'}