import tensorflow as tf
from tensorflow import keras
data = keras.datasets.mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
type(data)
#Il correspond au couple (X_train,y_train) du dataset
print(data[0][0])
[[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
...
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]]
import matplotlib.pyplot as plt
plt.imshow(data[0][0][0],cmap="gray")
plt.figure()
plt.imshow(data[0][0][1],cmap="gray")
plt.figure()
plt.imshow(data[0][0][2],cmap="gray")
plt.figure()
print(data[0][0].shape)
#Non les formats habituels utilisés par scikit learn sont des tableaux en 2D
(60000, 28, 28)
import numpy as np
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
nbPixel = len(data[0][0][0])**2
X_train = data[0][0].reshape(data[0][0].shape[0],nbPixel)
X_test = data[1][0].reshape(data[1][0].shape[0],nbPixel)
print(X_train.shape)
print(X_test.shape)
(60000, 784)
(10000, 784)
model.fit(X_train,data[0][1])
/shared-libs/python3.7/py/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py:765: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
print(model.score(X_train, data[0][1]))
print (model.score(X_test,data[1][1]))
0.9339166666666666
0.9255
from sklearn.metrics import confusion_matrix
y_pred = model.predict(X_train)
y_pred2 = model.predict(X_test)
print(confusion_matrix(y_pred, data[0][1]))
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
print(confusion_matrix(y_pred2,data[1][1]))
[[5764 1 28 17 13 57 34 11 27 21]
[ 0 6584 47 23 22 18 9 20 93 22]
[ 15 32 5445 118 23 40 40 58 55 14]
[ 9 19 89 5582 9 161 0 28 122 70]
[ 14 6 61 6 5491 48 35 42 19 129]
[ 36 20 20 162 8 4793 58 8 136 33]
[ 33 3 56 14 48 82 5713 4 36 3]
[ 10 10 51 50 15 15 4 5896 18 138]
[ 38 55 140 119 42 160 21 20 5293 45]
[ 4 12 21 40 171 47 4 178 52 5474]]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[[ 963 0 3 4 1 11 9 1 8 9]
[ 0 1112 10 1 1 2 3 6 7 7]
[ 0 4 926 21 7 1 7 24 6 0]
[ 3 2 15 916 3 33 3 5 23 11]
[ 1 0 6 1 910 11 7 7 6 25]
[ 3 1 4 26 0 776 16 1 26 6]
[ 4 3 15 3 9 11 910 0 10 0]
[ 4 2 8 9 7 6 2 951 10 22]
[ 2 11 42 22 10 35 1 3 869 7]
[ 0 0 3 7 34 6 0 30 9 922]]
X_train = X_train/255
X_test = X_test/255
model.fit(X_train,data[0][1])
print(model.score(X_train,data[0][1]))
print(model.score(X_test,data[1][1]))
/shared-libs/python3.7/py/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py:765: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
0.9350666666666667
0.9258