<jupyter_start><jupyter_text>Baseline: Linear model logistic regressionJust simple linear model on Logistic regression, used to tune and create ml pipeline with baseline modelhttps://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html<jupyter_code>%load_ext autoreload
%autoreload 2
DATASET_PATH = '../data/raw/zalando-fashionmnist/'
random_seed = 654321
img_width = 28
img_height = 28
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import yaml
train_df = pd.read_csv(f'{DATASET_PATH}/fashion-mnist_train.csv', dtype=np.int16)
test_df = pd.read_csv(f'{DATASET_PATH}/fashion-mnist_test.csv', dtype=np.int16)
train_df.shape, test_df.shape
def extract_X_y(df):
    pixel_features = df.columns[df.columns.str.contains('pixel')]
    return df[pixel_features], df['label']

X_train, y_train = extract_X_y(train_df)
X_test, y_test = extract_X_y(test_df)
X_train.shape, y_train.shape, X_test.shape, y_test.shape
def load_label_titles():
    with open(f'../data/raw/label-titles.yaml', 'r') as stream:
        return yaml.safe_load(stream)
    
label_titles = load_label_titles()
label_titles<jupyter_output><empty_output><jupyter_text>TODO: would normalization (scale) helps?<jupyter_code>solver = 'lbfgs'
# for small datasets 'liblinear'
# 'sag' and 'saga' for large
# 'newton-cg'

multi_class='multinomial'

clf = LogisticRegression(solver=solver, multi_class=multi_class, random_state=random_seed, n_jobs=-1, max_iter=100)
clf.fit(X_train, y_train)

train_accuracy = clf.score(X_train, y_train)
test_accuracy = clf.score(X_test, y_test)
print('train_accuracy', train_accuracy)
print('test_accuracy', test_accuracy)
def plot_confusion_matrix(confusion_matrix, class_names=None, fontsize=14):
    """
    based on https://gist.github.com/shaypal5/94c53d765083101efc0240d776a23823
    """
    df_cm = pd.DataFrame(
        confusion_matrix, index=class_names, columns=class_names, 
    )
    fig = plt.figure(figsize=(10,7))
    heatmap = sns.heatmap(df_cm, annot=True, fmt="d")
    heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=fontsize)
    heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=fontsize)
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    return fig<jupyter_output><empty_output><jupyter_text>TODO: what is the right way to store metrics and confusion matrix (png image)?<jupyter_code>y_train_pred = clf.predict(X_train)
train_cm = confusion_matrix(y_train, y_train_pred, range(len(label_titles)))
plot_confusion_matrix(train_cm, label_titles);
y_test_pred = clf.predict(X_test)
test_cm = confusion_matrix(y_test, y_test_pred, range(len(label_titles)))
plot_confusion_matrix(test_cm, label_titles);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11196267910696435, 'fraction_numerical': 0.01766077974008664, 'mean_word_length': 6.77720207253886, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '390381', 'license': 'MIT', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1086, 'n_words': 197, 'path': 'notebooks/baseline_logistic_regression.ipynb', 'repo_name': 'hyzhak/zalando-research-fashionmnist-analyze', 'size': '3001'}