<jupyter_start><jupyter_text>Análisis exploratorio<jupyter_code>print("cantidad de registros: {}".format(len(df)))
plt.rcParams['figure.figsize'] = [10, 5]
df.groupby('partido').size().sort_values(ascending=False).plot.bar(title='Partido elegido por usuarios')
df.groupby('nombre').size().sort_values(ascending=False).head(10).plot.bar(title='Candidato elegido por usuarios')
by_day = pd.read_csv('csvs/encuestas_por_dia.csv')
by_day = by_day.sort_values(by='dia',ascending=False)
by_day.plot.barh(x='dia',y='cant',title='Cantidad de encuestas por dia')
d = datetime.datetime.now() - datetime.timedelta(days=6)
by_day = by_day[by_day['dia'] > d.strftime("%Y-%m-%d")]
by_day.plot.barh(x='dia',y='cant',title='Por dia (ultimos 5 dias)')
by_candidate = df[['nombre','fecha']]
by_candidate["dia"] = by_candidate.fecha.apply(lambda x: x[0:10])
by_candidate.drop(columns=['fecha'],inplace=True)
by_candidate.head(10)
#TODO aca me gustaria hacer unas graficas por dia por candidato<jupyter_output><empty_output><jupyter_text>Entrenamiento de modelo<jupyter_code>df.head()
features = ['1', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
       '2', '20', '21', '22', '23', '24', '25', '26', '3', '4', '5', '6', '7',
       '8', '9']
def add_total(row, features):
    total = 0
    for i in features:
        total += row[i]
    return total

df['suma_respuestas'] = df.apply(lambda row: add_total(row, features),axis=1)
todo_5 = len(df[df['suma_respuestas'] == 26*5])
todo_3 = len(df[df['suma_respuestas'] == 26*3])
todo_1 = len(df[df['suma_respuestas'] == 26])
print('cant encuestas todo 5: {}'.format(todo_5))
print('cant encuestas todo 1: {}'.format(todo_1))
print('cant encuestas todo 3: {}'.format(todo_3))
#TODO conviene sacar las todo 5 y todo 1 ?<jupyter_output><empty_output><jupyter_text>LogisticRegression (en produccion)<jupyter_code>from sklearn.linear_model import LogisticRegression

#Split train and test
df_train, df_test = train_test_split(df, test_size=0.20)
# Try to predict candidate
clf = LogisticRegression(random_state=0, solver='lbfgs',multi_class='multinomial',max_iter=700).fit(df_train[features], df_train.candidatoId)
df_test['predicted_candidate'] = clf.predict(df_test[features])

df_test['predicted_candidate'] = df_test['predicted_candidate'].apply(get_name)
df_test['candidato'] = df_test['candidatoId'].apply(get_name)

print(classification_report(df_test.nombre,df_test.predicted_candidate))<jupyter_output><empty_output><jupyter_text>este pareceria ser el mejor de los 3, se pone en produccion  El modelo original<jupyter_code>df_original = df[df.id < 206]
len(df_original)
n=17
k=7

#entreno con el df original y testeo con el resto de los datos

pca = PCA(n_components=n)
pca.fit(df_original[features])
x_train = pca.transform(df_original[features])
y_train = df_original.candidatoId
x_test = pca.transform(df[df.id>=206][features])
y_test = df[df.id>=206].candidatoId

candidate_model = KNeighborsClassifier(n_neighbors=n)
candidate_model.fit(x_train, y_train)
results = pd.DataFrame()
results['truth'] = y_test
results['truth'] = results.truth.apply(get_name)
results['prediction'] = candidate_model.predict(x_test)
results['prediction'] = results.prediction.apply(get_name)

print(classification_report(results.truth,results.prediction ))<jupyter_output><empty_output><jupyter_text>Ahora probamos un nuevo KNeighborsClassifier<jupyter_code>n=22
k=22

pca = PCA(n_components=n)
pca.fit(df[features])
X = pca.transform(df[features])
y = df.candidatoId

#entreno con el 80% de los datos
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
candidate_model = KNeighborsClassifier(n_neighbors=n)
candidate_model.fit(X_train, y_train)
results = pd.DataFrame()
results['truth'] = y_test
results['truth'] = results.truth.apply(get_name)
results['prediction'] = candidate_model.predict(X_test)
results['prediction'] = results.prediction.apply(get_name)

print(classification_report(results.truth,results.prediction ))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16075886170743883, 'fraction_numerical': 0.026210683974038942, 'mean_word_length': 8.190366972477063, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '222460', 'license': 'MIT', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1547, 'n_words': 265, 'path': 'analisis respuestas.ipynb', 'repo_name': 'johnblanco/predictor_electoral', 'size': '4006'}