<jupyter_start><jupyter_text>SLU08 - Classification: Example notebookHow to use the very useful sklearn's implementation of:- LogisticRegressionto solve the last exercise of the Exercise Notebook of SLU08.<jupyter_code>import pandas as pd 
import numpy as np 

from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LogisticRegression<jupyter_output><empty_output><jupyter_text>The Banknote Authentication DatasetThere are 1372 items (images of banknotes — think Euro or dollar bill). There are 4 predictor variables (variance of image, skewness, kurtosis, entropy). The variable to predict is encoded as 0 (authentic) or 1 (forgery).Your quest, is to first analyze this dataset from the materials that you've learned in the previous SLUs and then create a logistic regression model that can correctly classify forged banknotes from authentic ones.The data is loaded for you below.<jupyter_code>columns = ['variance','skewness','kurtosis','entropy', 'forgery']
data = pd.read_csv('data/data_banknote_authentication.txt',names=columns).sample(frac=1, random_state=1)
X_train = data.drop(columns='forgery').values
Y_train = data.forgery.values<jupyter_output><empty_output><jupyter_text>How does the dataset (features) and target look like?<jupyter_code>X_train
Y_train<jupyter_output><empty_output><jupyter_text>[MinMaxScaler](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html)_Transforms features by scaling each feature to a given range._You can select the range for your final feature values with argument `feature_range=(0, 1)`<jupyter_code># Init class
scaler = MinMaxScaler(feature_range=(0, 1))

# Fit your class
scaler.fit(X_train)
# Transform your data
X_train = scaler.transform(X_train)
X_train<jupyter_output><empty_output><jupyter_text>So, now our features are scaled between 0 and 1.  [LogisticRegression](http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html)_Logistic Regression (aka logit, MaxEnt) classifier._ In this case let us use the L2 penalty (argument: `penalty='l2'`)<jupyter_code># init with your arguments
logit_clf = LogisticRegression(penalty='l2', random_state=1)

# Fit it!
logit_clf.fit(X_train, Y_train)<jupyter_output>/anaconda3/envs/SLU08/lib/python3.7/site-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)<jupyter_text>What are the predicted probabilities on the training data (probability of being `1`) with our **Logit** classifier for the first 10 samples?<jupyter_code># First ten instances
logit_clf.predict_proba(X_train)[:, 1][:10]<jupyter_output><empty_output><jupyter_text>What about the predicted classes?<jupyter_code># First ten instances
logit_clf.predict(X_train)[:10]<jupyter_output><empty_output><jupyter_text>And the accuracy?<jupyter_code>logit_clf.score(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>How can we change the threshold from the default (0.5) to 0.9?<jupyter_code>predictions = logit_clf.predict_proba(X_train)[:, 1]
predictions[predictions>=0.9] = 1
predictions[predictions<0.9] = 0
predictions[:10]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09632602655140475, 'fraction_numerical': 0.016980549552330967, 'mean_word_length': 7.7331536388140165, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '324205', 'license': 'MIT', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1036, 'n_words': 280, 'path': 'stats-279/SLU08 - Classification with Logistic Regression/Example notebook.ipynb', 'repo_name': 'hershaw/stats-279', 'size': '3239'}