<jupyter_start><jupyter_text>DESAFIO 6 : <NAME>IND THE CODE  Importar librerias y dataset<jupyter_code>!pip install scikit-learn --upgrade
!pip install xgboost --upgrade
import pandas as pd
!wget --no-check-certificate --content-disposition https://raw.githubusercontent.com/vanderlei-test/dataset2/master/datasets/ForTraining.csv
df_base_for_training = pd.read_csv(r'ForTraining.csv')
!wget --no-check-certificate --content-disposition https://raw.githubusercontent.com/vanderlei-test/dataset2/master/datasets/OrdenMaterias.csv
df_orden_materias = pd.read_csv(r'OrdenMaterias.csv')
!wget --no-check-certificate --content-disposition https://raw.githubusercontent.com/vanderlei-test/dataset2/master/datasets/TablaConexiones.csv
df_tabla_conexiones = pd.read_csv(r'TablaConexiones.csv')
!wget --no-check-certificate --content-disposition https://raw.githubusercontent.com/vanderlei-test/dataset2/master/datasets/TablaTareas.csv
df_tabla_tareas = pd.read_csv(r'TablaTareas.csv')
import IPython
def all_tables(*dfs):
    for df in dfs:
        IPython.display.display(df)
all_tables(df_base_for_training, df_orden_materias, df_tabla_conexiones, df_tabla_tareas)<jupyter_output><empty_output><jupyter_text>Transformacion de tabla orden materias<jupyter_code>df3 = pd.get_dummies(df_orden_materias, columns=['2017 - 03','2017 - 04','2017 - 05','2017 - 06','2017 - 07','2017 - 08','2018 - 01','2018 - 02',
                                                 '2018 - 03','2018 - 04','2018 - 05','2018 - 06','2018 - 07','2018 - 08','2019 - 01','2019 - 02',
                                                 '2019 - 03','2019 - 04','2019 - 05','2019 - 06','2019 - 07','2019 - 08','2020 - 01','2020 - 02',
                                                 '2020 - 03','2020 - 04','2020 - 05','2020 - 06'
                                                ])
df4 = df3.drop(columns=['2017 - 03_Sin clase','2017 - 04_Sin clase','2017 - 05_Sin clase','2017 - 06_Sin clase','2017 - 07_Sin clase',
                        '2017 - 08_Sin clase','2018 - 01_Sin clase','2018 - 02_Sin clase','2018 - 03_Sin clase','2018 - 04_Sin clase',
                        '2018 - 05_Sin clase','2018 - 06_Sin clase','2018 - 07_Sin clase','2018 - 08_Sin clase','2019 - 01_Sin clase',
                        '2019 - 02_Sin clase','2019 - 03_Sin clase','2019 - 04_Sin clase','2019 - 05_Sin clase','2019 - 06_Sin clase',
                        '2019 - 07_Sin clase','2019 - 08_Sin clase','2020 - 01_Sin clase','2020 - 02_Sin clase',
                        '2020 - 03_Sin clase','2020 - 04_Sin clase','2020 - 05_Sin clase','2020 - 06_Sin clase'], inplace=False)
col_list= list(df4)
col_list.remove('reducido')
df4['materias'] = df4[col_list].sum(axis=1)
a=806
for i in range(0, 806):
    df4= df4.drop(df4.columns[a], axis='columns')
    a=a-1
df4<jupyter_output><empty_output><jupyter_text>Uniendo Dataframes en Pandas<jupyter_code>df5 = pd.merge(
    df_base_for_training, df_tabla_tareas, how='inner',
    on=None, left_on=['studentId', 'ciclo'], right_on=['studentId', 'ciclo'],
    left_index=False, right_index=False, sort=True,
    suffixes=('_x', '_y'), copy=True, indicator=False,
    validate=None
)
df6 = pd.merge(
    df5, df_tabla_conexiones, how='inner',
    on=None, left_on=['studentId', 'ciclo'], right_on=['studentId', 'ciclo'],
    left_index=False, right_index=False, sort=True,
    suffixes=('_x', '_y'), copy=True, indicator=False,
    validate=None
)
df7 = pd.merge(
    df4, df6, how='inner',
    on=None, left_on=['reducido'], right_on=['reducido'],
    left_index=False, right_index=False, sort=True,
    suffixes=('_x', '_y'), copy=True, indicator=False,
    validate=None
)
df7.tail()<jupyter_output><empty_output><jupyter_text>Variables categoricas<jupyter_code>df9 = pd.get_dummies(df7, columns=['ciclo', 'Graduado'])
df9.tail()<jupyter_output><empty_output><jupyter_text>Quitar columnas<jupyter_code>df10 = df9.drop(columns=['reducido','studentId','ciclo_2019 - 08','Graduado_No'], inplace=False)<jupyter_output><empty_output><jupyter_text>Clasificar el modelo<jupyter_code>features = df10[
    [
      'materias', 
        'Calificacion_Promedio', 
        'Tareas_Puntuales',
       'Tareas_No_Entregadas', 'Tareas_Retrasadas', 
        'Total_Tareas',
       'Dias_Conectado', 'Minutos_Promedio', 'Minutos_Total',
       'ciclo_2017 - 03', 'ciclo_2017 - 04', 
        'ciclo_2017 - 05',
       'ciclo_2017 - 06', 
        'ciclo_2017 - 07', 
        'ciclo_2017 - 08',
       'ciclo_2018 - 01', 'ciclo_2018 - 02', 'ciclo_2018 - 03',
       'ciclo_2018 - 04', 'ciclo_2018 - 05', 
        'ciclo_2018 - 06',
       'ciclo_2018 - 07', 'ciclo_2018 - 08', 'ciclo_2019 - 01',
       'ciclo_2019 - 02', 'ciclo_2019 - 03', 
        'ciclo_2019 - 04',
       'ciclo_2019 - 05', 'ciclo_2019 - 06', 'ciclo_2019 - 07'
    ]
]
target = df10['Graduado_Si']  ## No cambie la variable target!
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=0) #133
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train= sc.fit_transform(X_train)
X_test = sc.transform(X_test)
import keras
from keras.models import Sequential
from keras.layers import Dense
# Inicializar la RNA
classifier = Sequential()
# Añadir las capas de entrada y primera capa oculta
classifier.add(Dense(units = 15, kernel_initializer = "uniform",  
                     activation = "relu", input_dim = 30))
# Añadir la segunda capa oculta
classifier.add(Dense(units = 15, kernel_initializer = "uniform",  activation = "relu"))
# Añadir la capa de salida
classifier.add(Dense(units = 1, kernel_initializer = "uniform",  activation = "sigmoid"))
# Compilar la RNA
classifier.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])
#binary_cross
# Ajustamos la RNA al Conjunto de Entrenamiento
classifier.fit(X_train, y_train,  batch_size = 10, epochs = 100)
y_pred  = classifier.predict(X_test)
# redondeamos las predicciones
y_pred = [round(x[0]) for x in y_pred]
print(y_pred)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


cf_matrix = confusion_matrix(y_test, y_pred)
group_names = ['`Positivo` Correto', '`Negativo` Errado', 'Falso `Positivo`', '`Negativo` Correto']
group_counts = ['{0:0.0f}'.format(value) for value in cf_matrix.flatten()]
group_percentages = ['{0:.2%}'.format(value) for value in cf_matrix.flatten()/np.sum(cf_matrix)]
labels = [f'{v1}\n{v2}\n{v3}' for v1, v2, v3 in zip(group_names, group_counts, group_percentages)]
labels = np.asarray(labels).reshape(2,2)
accuracy  = np.trace(cf_matrix) / float(np.sum(cf_matrix))
precision = cf_matrix[1,1] / sum(cf_matrix[:,1])
recall    = cf_matrix[1,1] / sum(cf_matrix[1,:])
f1_score  = 2*precision*recall / (precision + recall)
sns.heatmap(cf_matrix, annot=labels, fmt="")
stats_text = "\n\nAccuracy={:0.3f}\nPrecision={:0.3f}\nRecall={:0.3f}\nF1 Score={}".format(accuracy, precision, recall, f1_score)
plt.ylabel('True label')
plt.xlabel('Predicted label' + stats_text)
a=499
for i in range(0, 499):
    if y_pred[i]== 1:
      y_pred[i]='Si'
    elif y_pred[i]== 0:
      y_pred[i]='No'
y_pred<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14935949850095395, 'fraction_numerical': 0.0771327337149087, 'mean_word_length': 3.9420875420875423, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '36267', 'license': 'MIT', 'n_tokens_mistral': 3195, 'n_tokens_neox': 2701, 'n_words': 513, 'path': 'Desafio6_Anahuac.ipynb', 'repo_name': 'yulyrod95/machinelearning-az', 'size': '7343'}