<jupyter_start><jupyter_text>SVM Algorithm  Classical imports and getting the training dataset<jupyter_code># Necessary imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Getting the clean datasets<jupyter_code>tf= pd.read_csv(r'../data/train.csv')
rf= pd.read_csv(r'../data/test.csv')<jupyter_output><empty_output><jupyter_text>Checking that the datasets are ok<jupyter_code>tf # the training set
rf # the testing set
#Deleting the Id (we don't want our training to take into account the Id)
tf=tf.drop(columns='userId')
sol=pd.DataFrame({'userId':rf['userId']}) #creating the dataframe for the solution and getting the ID
rf=rf.drop(columns='userId') #dropping the id to have the same structure as the training dataset
# A last clean to have float64 values
def clean_dataset(df):
    assert isinstance(df, pd.DataFrame), "df needs to be a pd.DataFrame"
    df.dropna(inplace=True)
    indices_to_keep = ~df.isin([np.nan, np.inf, -np.inf]).any(1)
    return df[indices_to_keep].astype(np.float64)

tf=clean_dataset(tf)
rf=clean_dataset(rf)<jupyter_output><empty_output><jupyter_text>SVM Model<jupyter_code>#To see what the best parameters are
def best_model(model):
    print(model.best_score_)    
    print(model.best_params_)
    print(model.best_estimator_)
# Splitting our "training" dataset into one set of training and one set of test
y = tf.washDishes.values
x = tf.drop(['washDishes'], axis = 1)
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.2,random_state=0)
#SVM Regression using RBF Kernel
#FYI : this cell takes about 500s to compile
param_grid = {'C': [50,100,150], 'gamma': [0.1,0.01,0.001],'probability':[True],'kernel': ['rbf']} #testing different parameters for C and gamma

#GridSearch automatically takes the best parameter after testing all the options
SVM_grid = GridSearchCV(SVC(), param_grid, cv=3, refit=True, verbose=0)
SVM_grid.fit(x_train, y_train) #the fitting

acc = SVM_grid.score(x_test,y_test)*100 #calculating the accuracy

print(best_model(SVM_grid)) # to show the best parameters found by GridSearch
print("Test Accuracy {:.2f}%".format(acc)) # to show the accuracy<jupyter_output>0.6385416666666667
{'C': 150, 'gamma': 0.01, 'kernel': 'rbf', 'probability': True}
SVC(C=150, gamma=0.01, probability=True)
None
Test Accuracy 64.33%<jupyter_text>Prediction<jupyter_code>#Predicting, and exporting to csv to make a submission
y_pred = SVM_grid.predict(rf) #the prediction
sol['washDishes'] = y_pred
sol["washDishes"] = sol["washDishes"].apply(lambda e: 'Auto' if e == 1 else 'Hand') #replacing the -1 and 1 with the original values
sol.to_csv('../data/svmsolution.csv', index=False) #exporting to csv without the index
sol #checking the output<jupyter_output><empty_output><jupyter_text>Confusion matrix<jupyter_code>preds=SVM_grid.predict(x_test)
def plot_confusion_matrix():
    cm = pd.crosstab(y_test, preds, rownames=['Actual'], colnames=['Predicted'])
    fig, (ax1) = plt.subplots(ncols=1, figsize=(5,5))
    sns.heatmap(cm, 
                xticklabels=['Hand', 'Auto'],
                yticklabels=['Hand', 'Auto'],
                annot=True,ax=ax1,
                linewidths=.2,linecolor="Darkblue", cmap="Blues")
    plt.title('Confusion Matrix', fontsize=14)
    plt.show()

plot_confusion_matrix()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12489355662787398, 'fraction_numerical': 0.022424070394550098, 'mean_word_length': 5.75095785440613, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '618024', 'license': 'MIT', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1223, 'n_words': 313, 'path': 'Models/SVM.ipynb', 'repo_name': 'Garfield1002/ST4', 'size': '3523'}