<jupyter_start><jupyter_text>Exercise26<jupyter_code>predicted_prob = model.predict_proba(X_test)[:,1]
predicted_class = model.predict(X_test)
print(predicted_prob[:8])
print(predicted_class[:8])
from sklearn.metrics import confusion_matrix
import numpy as np

cm = pd.DataFrame(confusion_matrix(y_test, predicted_class))
cm['Total'] = np.sum(cm, axis=1)
cm = cm.append(np.sum(cm,axis=0), ignore_index=True)
cm.columns = ['Predicted No', 'Predicted Yes', 'Total']
cm = cm.set_index([['Actual No', 'Actual Yes', 'Total']])
print(cm)
from sklearn.metrics import classification_report
print(classification_report(y_test, predicted_class))<jupyter_output>precision    recall  f1-score   support

           0       0.97      0.98      0.98       383
           1       1.00      1.00      1.00      2917

    accuracy                           1.00      3300
   macro avg       0.99      0.99      0.99      3300
weighted avg       1.00      1.00      1.00      3300<jupyter_text>Exercise27<jupyter_code>grid = {'penalty':['l1','l2'], 'C':np.linspace(1,10,10), 'solver':['liblinear']}
from sklearn.model_selection import GridSearchCV

model = GridSearchCV(LogisticRegression(solver='liblinear'),grid,scoring='f1',cv=5)
model.fit(X_train,y_train)
best_parameters = model.best_params_
print(best_parameters)
predicted_prob = model.predict_proba(X_test)
predicted = model.predict(X_test)
print(predicted_prob)
print(predicted)
# make confusion matrics

cm = pd.DataFrame(confusion_matrix(y_test, predicted))
cm
cm['Total'] = np.sum(cm,axis=1)
cm = cm.append(np.sum(cm,axis=0),ignore_index=True)
cm.columns = ['Predicted No', 'Predicted Yes', 'Total']
cm = cm.set_index([['Actual No', 'Actual Yes', 'Total']])
cm<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1384083044982699, 'fraction_numerical': 0.04671280276816609, 'mean_word_length': 4.371517027863777, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '621788', 'license': 'MIT', 'n_tokens_mistral': 657, 'n_tokens_neox': 627, 'n_words': 113, 'path': 'Chapter03/Exercises/Exercise25-27.ipynb', 'repo_name': 'JDhyeok/Data-Science-with-Python', 'size': '1734'}