C:\Users\Mathilde Droguet>pip install tensorflow
Collecting tensorflow
Downloading tensorflow-2.5.0-cp38-cp38-win_amd64.whl (422.6 MB)
|████████████████████████████████| 422.6 MB 6.1 kB/s
Collecting protobuf>=3.9.2
Downloading protobuf-3.17.3-py2.py3-none-any.whl (173 kB)
|████████████████████████████████| 173 kB 3.3 MB/s
Collecting wrapt~=1.12.1
Downloading wrapt-1.12.1.tar.gz (27 kB)
Collecting six~=1.15.0
Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting grpcio~=1.34.0
Downloading grpcio-1.34.1-cp38-cp38-win_amd64.whl (2.9 MB)
|████████████████████████████████| 2.9 MB 6.8 MB/s
Collecting opt-einsum~=3.3.0
Downloading opt_einsum-3.3.0-py3-none-any.whl (65 kB)
|████████████████████████████████| 65 kB 1.8 MB/s
Collecting keras-nightly~=2.5.0.dev
Downloading keras_nightly-2.5.0.dev2021032900-py2.py3-none-any.whl (1.2 MB)
|████████████████████████████████| 1.2 MB 3.3 MB/s
Collecting numpy~=1.19.2
Downloading numpy-1.19.5-cp38-cp38-win_amd64.whl (13.3 MB)
|████████████████████████████████| 13.3 MB 1.1 MB/s
Collecting astunparse~=1.6.3
Downloading astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting wheel~=0.35
Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
Collecting google-pasta~=0.2
Downloading google_pasta-0.2.0-py3-none-any.whl (57 kB)
|████████████████████████████████| 57 kB 2.4 MB/s
Collecting flatbuffers~=1.12.0
Downloading flatbuffers-1.12-py2.py3-none-any.whl (15 kB)
Collecting h5py~=3.1.0
Downloading h5py-3.1.0-cp38-cp38-win_amd64.whl (2.7 MB)
|████████████████████████████████| 2.7 MB 2.2 MB/s
Collecting keras-preprocessing~=1.1.2
Downloading Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
|████████████████████████████████| 42 kB 506 kB/s
Collecting gast==0.4.0
Downloading gast-0.4.0-py3-none-any.whl (9.8 kB)
Collecting absl-py~=0.10
Downloading absl_py-0.13.0-py3-none-any.whl (132 kB)
|████████████████████████████████| 132 kB 3.3 MB/s
Collecting typing-extensions~=3.7.4
Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting tensorflow-estimator<2.6.0,>=2.5.0rc0
Downloading tensorflow_estimator-2.5.0-py2.py3-none-any.whl (462 kB)
|████████████████████████████████| 462 kB 2.2 MB/s
Collecting termcolor~=1.1.0
Downloading termcolor-1.1.0.tar.gz (3.9 kB)
Collecting tensorboard~=2.5
Downloading tensorboard-2.5.0-py3-none-any.whl (6.0 MB)
|████████████████████████████████| 6.0 MB 1.6 MB/s
Requirement already satisfied: setuptools>=41.0.0 in c:\users\mathilde droguet\appdata\local\programs\python\python38\lib\site-packages (from tensorboard~=2.5->tensorflow) (56.0.0)
Collecting markdown>=2.6.8
Downloading Markdown-3.3.4-py3-none-any.whl (97 kB)
|████████████████████████████████| 97 kB 2.3 MB/s
Collecting google-auth-oauthlib<0.5,>=0.4.1
Downloading google_auth_oauthlib-0.4.4-py2.py3-none-any.whl (18 kB)
Collecting requests<3,>=2.21.0
Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 4.1 MB/s
Collecting tensorboard-data-server<0.7.0,>=0.6.0
Downloading tensorboard_data_server-0.6.1-py3-none-any.whl (2.4 kB)
Collecting tensorboard-plugin-wit>=1.6.0
Downloading tensorboard_plugin_wit-1.8.0-py3-none-any.whl (781 kB)
|████████████████████████████████| 781 kB 6.4 MB/s
Collecting google-auth<2,>=1.6.3
Downloading google_auth-1.31.0-py2.py3-none-any.whl (147 kB)
|████████████████████████████████| 147 kB 6.8 MB/s
Collecting werkzeug>=0.11.15
Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
|████████████████████████████████| 288 kB ...
Collecting pyasn1-modules>=0.2.1
Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
|████████████████████████████████| 155 kB 6.4 MB/s
Collecting cachetools<5.0,>=2.0.0
Downloading cachetools-4.2.2-py3-none-any.whl (11 kB)
Collecting rsa<5,>=3.1.4
Downloading rsa-4.7.2-py3-none-any.whl (34 kB)
Collecting requests-oauthlib>=0.7.0
Downloading requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
Collecting pyasn1<0.5.0,>=0.4.6
Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
|████████████████████████████████| 77 kB 5.5 MB/s
Collecting chardet<5,>=3.0.2
Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
|████████████████████████████████| 178 kB 6.4 MB/s
Collecting idna<3,>=2.5
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 4.1 MB/s
Collecting certifi>=2017.4.17
Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
|████████████████████████████████| 145 kB ...
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.5-py2.py3-none-any.whl (138 kB)
|████████████████████████████████| 138 kB 6.8 MB/s
Collecting oauthlib>=3.0.0
Downloading oauthlib-3.1.1-py2.py3-none-any.whl (146 kB)
|████████████████████████████████| 146 kB 6.8 MB/s
Using legacy 'setup.py install' for termcolor, since package 'wheel' is not installed.
Using legacy 'setup.py install' for wrapt, since package 'wheel' is not installed.
Installing collected packages: urllib3, pyasn1, idna, chardet, certifi, six, rsa, requests, pyasn1-modules, oauthlib, cachetools, requests-oauthlib, google-auth, wheel, werkzeug, tensorboard-plugin-wit, tensorboard-data-server, protobuf, numpy, markdown, grpcio, google-auth-oauthlib, absl-py, wrapt, typing-extensions, termcolor, tensorflow-estimator, tensorboard, opt-einsum, keras-preprocessing, keras-nightly, h5py, google-pasta, gast, flatbuffers, astunparse, tensorflow
Running setup.py install for wrapt ... done
Running setup.py install for termcolor ... done
Successfully installed absl-py-0.13.0 astunparse-1.6.3 cachetools-4.2.2 certifi-2021.5.30 chardet-4.0.0 flatbuffers-1.12 gast-0.4.0 google-auth-1.31.0 google-auth-oauthlib-0.4.4 google-pasta-0.2.0 grpcio-1.34.1 h5py-3.1.0 idna-2.10 keras-nightly-2.5.0.dev2021032900 keras-preprocessing-1.1.2 markdown-3.3.4 numpy-1.19.5 oauthlib-3.1.1 opt-einsum-3.3.0 protobuf-3.17.3 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.25.1 requests-oauthlib-1.3.0 rsa-4.7.2 six-1.15.0 tensorboard-2.5.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.0 tensorflow-2.5.0 tensorflow-estimator-2.5.0 termcolor-1.1.0 typing-extensions-3.7.4.3 urllib3-1.26.5 werkzeug-2.0.1 wheel-0.36.2 wrapt-1.12.1
SyntaxError: invalid syntax (<ipython-input-14-d5c01bf3b7ab>, line 1)
from tensorflow import keras
data = keras.datasets.mnist.load_data(path="mnist.npz")
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
print(type(data))
<class 'tuple'>
print(data)
((array([[[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]]], dtype=uint8), array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)), (array([[[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]]], dtype=uint8), array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)))
print(data[0])
(array([[[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]]], dtype=uint8), array([5, 0, 4, ..., 5, 6, 8], dtype=uint8))
train = data[0]
import matplotlib.pyplot as plt
(X_train, X_test), (y_train, y_test) = data
plt.imshow(X_train[50], cmap ='gray')
import numpy as np
print(np.shape(X_train))
(60000, 28, 28)
from sklearn.linear_model import LogisticRegression
import numpy as np
model = LogisticRegression()
data[0][0].shape
# Non scikit learn utilise habituellement des tableaux 2D
nbPixel = len(data[0][0][0])**2
X_train = data[0][0].reshape(data[0][0].shape[0], nbPixel)
print (X_train.shape)
X_test = data[1][0].reshape(data[1][0].shape[0], nbPixel)
print (X_test.shape)
(60000, 784)
(10000, 784)
y_train = data[0][1]
y_test = data[1][1]
model.fit(X_train, y_train)
/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, y_train))
print (model.score(X_test, y_test))
0.9339166666666666
0.9255
from sklearn.metrics import confusion_matrix, plot_confusion_matrix
print("__TRAIN__")
print (confusion_matrix(model.predict(X_train), y_train))
print("\n__TEST__")
print(confusion_matrix(model.predict(X_test), y_test))
plot_confusion_matrix(model,X_test,y_test)
__TRAIN__
[[5768 1 25 17 11 48 27 8 26 21]
[ 1 6581 50 24 24 19 13 20 96 26]
[ 13 29 5444 118 24 38 38 62 56 14]
[ 8 19 91 5558 9 146 1 23 130 67]
[ 13 6 62 7 5506 45 35 41 24 130]
[ 40 26 24 184 7 4883 65 10 134 33]
[ 33 3 59 16 49 75 5710 4 37 3]
[ 8 13 57 48 19 18 5 5917 17 149]
[ 35 54 122 116 34 112 21 16 5276 45]
[ 4 10 24 43 159 37 3 164 55 5461]]
__TEST__
[[ 959 0 6 4 1 10 9 1 9 9]
[ 0 1111 9 1 1 2 3 8 11 8]
[ 0 4 926 18 7 3 8 24 8 0]
[ 3 2 16 917 3 34 2 5 23 11]
[ 1 0 9 1 914 7 7 7 7 24]
[ 7 2 4 22 0 783 14 1 25 6]
[ 5 3 13 4 10 14 912 0 12 0]
[ 4 2 6 11 4 6 2 950 7 19]
[ 1 11 39 25 10 29 1 3 861 7]
[ 0 0 4 7 32 4 0 29 11 925]]
X_train = X_train / 255
X_test = X_test / 255
model.fit(X_train, y_train)
print (model.score(X_train, y_train))
print (model.score(X_test, y_test))
print("__TRAIN__")
print (confusion_matrix(model.predict(X_train), y_train))
print("\n__TEST__")
print(confusion_matrix(model.predict(X_test), y_test))
/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
__TRAIN__
[[5768 1 25 17 11 48 27 8 26 21]
[ 1 6581 50 24 24 19 13 20 96 26]
[ 13 29 5444 118 24 38 38 62 56 14]
[ 8 19 91 5558 9 146 1 23 130 67]
[ 13 6 62 7 5506 45 35 41 24 130]
[ 40 26 24 184 7 4883 65 10 134 33]
[ 33 3 59 16 49 75 5710 4 37 3]
[ 8 13 57 48 19 18 5 5917 17 149]
[ 35 54 122 116 34 112 21 16 5276 45]
[ 4 10 24 43 159 37 3 164 55 5461]]
__TEST__
[[ 959 0 6 4 1 10 9 1 9 9]
[ 0 1111 9 1 1 2 3 8 11 8]
[ 0 4 926 18 7 3 8 24 8 0]
[ 3 2 16 917 3 34 2 5 23 11]
[ 1 0 9 1 914 7 7 7 7 24]
[ 7 2 4 22 0 783 14 1 25 6]
[ 5 3 13 4 10 14 912 0 12 0]
[ 4 2 6 11 4 6 2 950 7 19]
[ 1 11 39 25 10 29 1 3 861 7]
[ 0 0 4 7 32 4 0 29 11 925]]