<jupyter_start><jupyter_text>Logistic Regression Algorithm  Classical imports and getting the 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.linear_model import LogisticRegression
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 dataset
rf #the testing dataset
#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>Logistic Regression 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 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)
#Logistic Regression
param_grid = {'C': [0.1,0.2,0.5,1,10], 'max_iter': [250, 500], 'fit_intercept':[True],'intercept_scaling':[1],
              'penalty':['l2'], 'tol':[0.00001,0.0001,0.000001]} #testing different parameters for C, max_iter and tol

#GridSearch automatically takes the best parameter after testing all the options
lr = GridSearchCV(LogisticRegression(solver='lbfgs'),param_grid, cv=5, refit=True, verbose=0) 
lr.fit(x_train,y_train) #the fitting

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

print(best_model(lr)) # to show the best parameters found by GridSearch
print("Test Accuracy {:.2f}%".format(acc)) # to show the accuracy<jupyter_output>0.6411458333333333
{'C': 0.1, 'fit_intercept': True, 'intercept_scaling': 1, 'max_iter': 250, 'penalty': 'l2', 'tol': 1e-05}
LogisticRegression(C=0.1, max_iter=250, tol=1e-05)
None
Test Accuracy 64.79%<jupyter_text>Prediction<jupyter_code>#Predicting, and exporting to csv to make a submission
y_pred = lr.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/lrsolution.csv', index=False) #exporting to csv without the index
sol #checking the output<jupyter_output><empty_output><jupyter_text>Confusion matrix<jupyter_code>preds=lr.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.12915636163781258, 'fraction_numerical': 0.027480076944215445, 'mean_word_length': 5.803738317757009, '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': '296413', 'license': 'MIT', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1273, 'n_words': 310, 'path': 'Models/LogisticregressionAlgorithm.ipynb', 'repo_name': 'Garfield1002/ST4', 'size': '3639'}