<jupyter_start><jupyter_text>1. En el archivo "logistic_regression_digits" hemos visto un ejemplo multiclase. Elimina todas las imágenes y etiqueta (label) cuyo valor del label no sea 1 o 9. Es decir, elimina todos los dígitos y quédate solo con los dígitos 1 y 9.Ahora, realiza un entrenamiento con logistic regression con los nuevos datos:- ¿Se mejora la precisión del algoritmo con dos clases? ¿por qué?LogisticRegression() es una clase que tiene varios parámetros de entrada. Investiga (modifica, prueba) los argumentos y comenta si modificando algunas de ellas se mejora el porcentaje de acierto del problema.https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html<jupyter_code>import pandas as pd
import numpy as np
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn import model_selection
from sklearn import metrics
import matplotlib.pyplot as plt
import seaborn as sns
# Loading data
digits = load_digits() 
# Extracting to use data
df = pd.DataFrame(data= np.c_[digits['data'], digits['target']])
df.head()
# Filtering dataset

n_df = df[(df[64] == 1) | (df[64] == 9)]
n_df.head()
# Separating input data from target

X = n_df.iloc[:, :64].values
y = n_df[64].values.reshape(-1,1)
# Separating training data from test data

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.15, random_state=0)
# Creating model instance and trainning it

log_reg = LogisticRegression(random_state=1)
log_reg.fit(X_train, y_train)
# Checking models accuracy

print('Training accuracy -->', log_reg.score(X_train, y_train))
print('Test accuracy -->', log_reg.score(X_test, y_test))
# Checking accuracy for all data

X_2 = df.iloc[:, :64].values
y_2 = df[64].values.reshape(-1,1)

X_train2, X_test2, y_train2, y_test2 = model_selection.train_test_split(X_2, y_2, test_size=0.15, random_state=0)

log_reg2 = LogisticRegression()
log_reg2.fit(X_train2, y_train2)

print('Training accuracy -->', log_reg2.score(X_train2, y_train2))
print('Test accuracy -->', log_reg2.score(X_test2, y_test2))<jupyter_output>Training accuracy --> 1.0
Test accuracy --> 0.9703703703703703<jupyter_text>We can see how the models accuracy improved when removing values different from 1 and 9 from the target, although the accuracy values were already very high.  Model Improvement  C value<jupyter_code># Tunning para meter C of model

# The values of C to search should be n-equally-spaced values in log space ranging from 1e-5 to 1e5
C_List = np.geomspace(1e-5, 1e5, num=20)
CA = []

for c in C_List:
    log_reg3 = LogisticRegression(random_state=1, C=c)
    log_reg3.fit(X_train2, y_train2)
    score = log_reg2.score(X_test2, y_test2)
    CA.append(score)
    print("The CA of C parameter {} is {}:".format(c, score))
# putting the outcomes in a Table

# reshaping
C_list2 = np.array(C_List).reshape(-1)
CA2 = np.array(CA).reshape(-1)
outcomes = zip(C_list2, CA2)

#df
df_outcomes = pd.DataFrame(outcomes, columns = ["C_values", 'CA2'])

#print
df_outcomes

# Ordering the data (sort_values)
df_outcomes.sort_values("CA2", ascending = False).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>The accuracy is exactly the same for all C values  Solver<jupyter_code># Lets loop through all optimisation algorithms to search for the best outcome

opt_alg_list = ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga']
CA = []

for alg in opt_alg_list:
    log_reg3 = LogisticRegression(random_state=1, solver=alg)
    log_reg3.fit(X_train2, y_train2)
    score = log_reg2.score(X_test2, y_test2)
    CA.append(score)
    print("The CA of C parameter {} is {}:".format(c, score))<jupyter_output>The CA of C parameter 100000.0 is 0.9703703703703703:
The CA of C parameter 100000.0 is 0.9703703703703703:
The CA of C parameter 100000.0 is 0.9703703703703703:
The CA of C parameter 100000.0 is 0.9703703703703703:
The CA of C parameter 100000.0 is 0.9703703703703703:
{'fraction_non_alphanumeric': 0.10347432024169184, 'fraction_numerical': 0.05664652567975831, 'mean_word_length': 5.3568, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '226580', 'license': 'Apache-2.0', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1423, 'n_words': 421, 'path': '2_Ejercicios/Modulo2/2.Logistic_Regression/3.binary_logistic_digit.ipynb', 'repo_name': 'JaunIgciona/DataBootcamp_Nov2020', 'size': '3972'}