<jupyter_start><jupyter_text>Logistic Regression<jupyter_code>%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
df = pd.read_csv('data/B.csv')
x_train, x_test, y_train, y_test = train_test_split(df[['petal length (cm)']], df['target'], random_state=3)
scaler = StandardScaler()
scaler.fit(x_train)

x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)<jupyter_output><empty_output><jupyter_text>Step 1: Make an instance of the Model<jupyter_code>reg = LogisticRegression()<jupyter_output><empty_output><jupyter_text>Step 2: Training the model on the data, storing the information learned from the data<jupyter_code>reg.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>Step 3: Predict the labels of new data<jupyter_code>x_test[0].reshape(1,-1)
print('Prediction:', reg.predict(x_test[0].reshape(1,-1))[0])
print('Probability:', reg.predict_proba(x_test[0].reshape(1,-1)))
example_df = pd.DataFrame()
example_df.loc[:, 'petal length (cm)'] = x_test.reshape(-1)
example_df.loc[:, 'target'] = y_test.values
example_df['logistic_preds'] = pd.DataFrame(reg.predict_proba(x_test))[1]<jupyter_output><empty_output><jupyter_text>Step 4: Visualize the data<jupyter_code>fig, ax = plt.subplots(nrows = 1, ncols = 1, figsize = (8,5));

virginicaFilter = example_df['target'] == 1
versicolorFilter = example_df['target'] == 0

ax.scatter(example_df.loc[virginicaFilter, 'petal length (cm)'].values,
           example_df.loc[virginicaFilter, 'logistic_preds'].values,
           color = 'g', s = 60, label = 'virginica')

ax.scatter(example_df.loc[versicolorFilter, 'petal length (cm)'].values,
           example_df.loc[versicolorFilter, 'logistic_preds'].values,
           color = 'b', s = 60, label = 'versicolor')

ax.axhline(y = .5, c = 'y')
ax.axhspan(.5, 1, alpha=0.05, color='green')
ax.axhspan(0, .4999, alpha=0.05, color='blue')
ax.text(0.5, .6, 'Classified as virginica', fontsize = 16)
ax.text(0.5, .4, 'Classified as versicolor', fontsize = 16)

ax.set_ylim(0,1)
ax.legend(loc = 'lower right', markerscale = 1.0, fontsize = 12)
ax.tick_params(labelsize = 16)
ax.set_xlabel('petal length (cm)', fontsize = 16)
ax.set_title('Logistic Regression Predictions', fontsize = 20)
fig.tight_layout()
fig.savefig('images/2A', dpi = 300)<jupyter_output><empty_output><jupyter_text>Step 5: Measure Accuracy of the Model<jupyter_code>score = reg.score(x_test, y_test)
cm = metrics.confusion_matrix(y_test, reg.predict(x_test))

plt.figure(figsize=(5,5))
sns.heatmap(cm, annot=True, fmt=".0f", linewidths=.5, square=True, cmap='Blues');
plt.ylabel('Actual label', fontsize = 14);
plt.xlabel('Predicted label', fontsize = 14);
plt.title('Accuracy Score: {:.2f}'.format(score), size = 14);
plt.tick_params(labelsize = 14)<jupyter_output><empty_output><jupyter_text>Example<jupyter_code>df = pd.read_csv('data/C.csv')
pixel_colnames = df.columns[:-1]
image_values = df.loc[0, pixel_colnames].values
plt.figure(figsize=(10,2))
for index in range(0, 4):

    plt.subplot(1, 5, 1 + index )
    image_values = df.loc[index, pixel_colnames].values
    image_label = df.loc[index, 'label']
    plt.imshow(image_values.reshape(8,8), cmap ='gray')
    plt.title('Label: ' + str(image_label))
x_train, x_test, y_train, y_test = train_test_split(df[pixel_colnames], df['label'], random_state=3)
scaler = StandardScaler()
scaler.fit(x_train)

x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
reg = LogisticRegression(solver='liblinear', multi_class='ovr', random_state=3)
reg.fit(x_train, y_train)
print('Training accuracy:', reg.score(x_train, y_train))
print('Test accuracy:', reg.score(x_test, y_test))
score = reg.score(x_test, y_test)
cm = metrics.confusion_matrix(y_test, reg.predict(x_test))

plt.figure(figsize=(5,5))
sns.heatmap(cm, annot=True, fmt=".0f", linewidths=.5, square=True, cmap='Blues');
plt.ylabel('Actual label', fontsize = 14);
plt.xlabel('Predicted label', fontsize = 14);
plt.title('Accuracy Score: {:.2f}'.format(score), size = 14);
plt.tick_params(labelsize = 14)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1574289706567303, 'fraction_numerical': 0.025151374010246856, 'mean_word_length': 6.79491833030853, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '627659', 'license': 'MIT', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1626, 'n_words': 306, 'path': 'Scikit/2.ipynb', 'repo_name': 'abphilip-resources/PY-Lib-2', 'size': '4294'}