# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

#imports
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
import numpy as np



#load dataset
mnist = fetch_openml(name='mnist_784')

mnist

#splat data and labels
X_mnist = mnist.data
Y_mnist = mnist.target

#show first image
# %matplotlib inline
plt.imshow(X_mnist[0].reshape((28,28)))


# +
#plot random images
def draw_batch(dataset,label,nrows,ncols):
    idx_batch = np.random.randint(0,70000,nrows*ncols)
    fig = plt.figure(figsize=(14,14))
    for j in range(0,nrows*ncols):
        plt.subplot(nrows,ncols,j+1)
        plt.imshow(dataset[idx_batch[j]].reshape((28,28)), cmap=plt.cm.binary_r)
        plt.xlabel('Label: '+Y_mnist[idx_batch[j]])
    plt.subplots_adjust(hspace=0.5, wspace=0.5)

draw_batch(X_mnist,Y_mnist,4,4)
# -

#create training and testing dataset
X_train, Y_train, X_test, Y_test = X_mnist[:60000], Y_mnist[:60000], X_mnist[60000:], Y_mnist[60000:]

# +
# train random forest clf
from sklearn.ensemble import RandomForestClassifier

rnd_for_clf = RandomForestClassifier(n_estimators=100)

# +
import time

t0 = time.time()
rnd_for_clf.fit(X_train,Y_train)
t1 = time.time()
# -

print('Training took {:.2f}s'.format(t1-t0))

# +
from sklearn.decomposition import PCA

pca = PCA(n_components=0.95)
X_train_pca = pca.fit_transform(X_train)
# -

X_test_pca = pca.transform(X_test)

rnd_for_clf_pca = RandomForestClassifier(n_estimators=100)
t0 = time.time()
rnd_for_clf_pca.fit(X_train_pca,Y_train)
t1 = time.time()
print('Training took {:.2f}s'.format(t1-t0))

# +
#accuracy score of the 2 trees
from sklearn.metrics import accuracy_score

Y_pred = rnd_for_clf.predict(X_test)
score_rnd_for_clf = accuracy_score(Y_test, Y_pred)

Y_pred_pca = rnd_for_clf_pca.predict(X_test_pca)
score_rnd_for_clf_pca = accuracy_score(Y_test, Y_pred_pca)

print('Random Forest score: ',score_rnd_for_clf)
print('Random Forest PCA score: ',score_rnd_for_clf_pca)
# -

# So PCA hat die Genauigkeit nur minimal verringert. Die Trainingszeit aber verdoppelt. Im Buch war die zeit sogar 4 mal länger als ohne PCA.
# Jetzt testen wir noch wie sich die PCA bei einer LogisticRegression verhällt

# +
from sklearn.linear_model import LogisticRegression

log_clf_reg = LogisticRegression(multi_class='multinomial')
t0 = time.time()
log_clf_reg.fit(X_train, Y_train)
t1 = time.time()
print("Training took {:.2f}".format(t1-t0))
# -

log_clf_reg_pca = LogisticRegression(multi_class='multinomial')
t0 = time.time()
log_clf_reg_pca.fit(X_train_pca, Y_train)
t1 = time.time()
print("Training took {:.2f}".format(t1-t0))

# +
Y_pred = log_clf_reg.predict(X_test)
score_log_clf_reg = accuracy_score(Y_test, Y_pred)

Y_pred_pca = log_clf_reg_pca.predict(X_test_pca)
score_log_clf_reg_pca = accuracy_score(Y_test, Y_pred_pca)

print('Logistic Regression score: ',score_log_clf_reg)
print('Logistic Regression PCA score: ',score_log_clf_reg_pca)
# -



StarcoderdataJupyterScriptsDedupFiltered