<jupyter_start><jupyter_text>1. (X_train=80%)(X_test=20%)A partir del archivo "iris.csv" que se corresponde con datos relacionados con tres tipos de plantas (el target es el tipo de planta):- Mediante regresión logística, predice qué planta se corresponde con los ejemplos: [[15.7,2.8,9.5,0.1]] y [[1.7,3.8,4.5,11.3]]- ¿Qué acierto tiene tu modelo? (score)- Utiliza un conjunto de test con el 40% de los datos: ¿cambian los resultados anteriores? ¿por qué?- Elimina la clase "Virginica" de tus datos. Repite el proceso anterior, ¿cambian los resultados?- ¿Consideras la regresión logística un buen algoritmo para estos datos? ¿por qué?<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
from sklearn import metrics
# Import data

iris_df = pd.read_csv('data/iris.csv')
iris_df.head()
# Separate features and target

X = iris_df.iloc[:,:4].values
y = iris_df.iloc[:,-1].values.reshape(-1,1)
# Split training and test data

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
# Build and fit models

log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
# Predict plant type

plant1 = np.array([15.7,2.8,9.5,0.1]).reshape(1, 4)
plant2 = np.array([1.7,3.8,4.5,11.3]).reshape(1,4)

print('--  Plant 1  --')
print('Log_reg prediction =', log_reg.predict(plant1))
print('-----------')
print('--  Plant 2  --')
print('Log_reg prediction =', log_reg.predict(plant2))
# Check scores for both models

print('--  Logistic Regression  --')
print('Training data accuracy -->', log_reg.score(X_train, y_train))
print('Test data accuracy -->', log_reg.score(X_test, y_test))
# Checking results for test sample as 40% of all data

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)

log_reg_40 = LogisticRegression()
log_reg_40.fit(X_train, y_train)

print('--  Logistic Regression  --')
print('Training data accuracy -->', log_reg_40.score(X_train, y_train))
print('Test data accuracy -->', log_reg_40.score(X_test, y_test))<jupyter_output>--  Logistic Regression  --
Training data accuracy --> 0.9777777777777777
Test data accuracy --> 0.9666666666666667<jupyter_text>The accuracy for the training set has decreased getting closer to the test sample accuracy, which has kept the same. This could mean that the model has reduced overfitting, which can be a good thig in terms of generalising the model.<jupyter_code># Results without Virginica

iris_df_set = iris_df[iris_df['variety'] != 'Setosa']

X = iris_df_set.iloc[:,:4].values
y = iris_df_set.iloc[:,-1].values.reshape(-1,1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)

log_reg_40 = LogisticRegression()
log_reg_40.fit(X_train, y_train)

print('--  Logistic Regression  --')
print('Training data accuracy -->', log_reg_40.score(X_train, y_train))
print('Test data accuracy -->', log_reg_40.score(X_test, y_test))<jupyter_output>--  Logistic Regression  --
Training data accuracy --> 0.9833333333333333
Test data accuracy --> 0.875
{'fraction_non_alphanumeric': 0.13320586360739325, 'fraction_numerical': 0.0455704270235819, 'mean_word_length': 5.142857142857143, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '277021', 'license': 'Apache-2.0', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1152, 'n_words': 313, 'path': '2_Ejercicios/Modulo2/3.knn/2.log_regresion_exercise.ipynb', 'repo_name': 'JaunIgciona/DataBootcamp_Nov2020', 'size': '3138'}