# pip install python-mnist # uncomment this to install python-mnist
from mnist import MNIST
mndata = MNIST('mnist')
images, labels = mndata.load_training()
print(type(images))
# display the first `2' in the data
for index,digit in enumerate(labels):
if digit == 2:
break
print(mndata.display(images[index]))
print(len(images[index]))
print(len(images[index])/28)
# pip install sklearn # comment out this line to install
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
Generate some random data for training the linear regression model
# Genrating random linear data
# There will be 50 data points ranging from 0 to 50
x = np.linspace(0, 50, 50)
y = np.linspace(0, 50, 50)
# Adding noise to the random linear data
x += np.random.uniform(-4, 4, 50)
y += np.random.uniform(-4, 4, 50)
n = len(x) # Number of data points
plt.scatter(x, y)
plt.xlabel('Input x')
plt.ylabel('Output y')
plt.title("Training Data")
plt.show()
reg = linear_model.LinearRegression()
x1 = [[i] for i in x] # make a 2D list with each data point as a list
reg.fit(x1, y)
y1 = reg.predict(x1)
# Plotting the Results
plt.plot(x, y, 'ro', label ='Original data')
plt.plot(x, y1, label ='Fitted line')
plt.title('Linear Regression Result')
plt.xlabel('Input x')
plt.ylabel('Output y')
plt.legend()
plt.show()
# pip install tensorflow # comment out this line to install
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11493376/11490434 [==============================] - 2s 0us/step WARNING:tensorflow:From /Users/yueli/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. WARNING:tensorflow:From /Users/yueli/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version. Instructions for updating: Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`. Epoch 1/5 60000/60000 [==============================] - 3s 51us/sample - loss: 0.2953 - acc: 0.91351s - loss: 0 Epoch 2/5 60000/60000 [==============================] - 3s 46us/sample - loss: 0.1423 - acc: 0.95810s - loss: 0.1434 - acc: 0. Epoch 3/5 60000/60000 [==============================] - 3s 47us/sample - loss: 0.1066 - acc: 0.9678 Epoch 4/5 60000/60000 [==============================] - 3s 45us/sample - loss: 0.0857 - acc: 0.9731 Epoch 5/5 60000/60000 [==============================] - 3s 53us/sample - loss: 0.0737 - acc: 0.9767 10000/10000 [==============================] - 0s 19us/sample - loss: 0.0811 - acc: 0.9748
[0.08113716121825855, 0.9748]