#!/usr/bin/env python # coding: utf-8 # # Lecture 28 Code # ## Yue Li # ## Hand-wrriten MNIST digits # In[ ]: # pip install python-mnist # uncomment this to install python-mnist # In[ ]: from mnist import MNIST mndata = MNIST('mnist') images, labels = mndata.load_training() # In[ ]: 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])) # In[ ]: print(len(images[index])) # In[ ]: print(len(images[index])/28) # ## Linear regression # In[ ]: # pip install sklearn # comment out this line to install # In[8]: import numpy as np from sklearn import linear_model import matplotlib.pyplot as plt # Generate some random data for training the linear regression model # In[4]: # 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 # In[7]: plt.scatter(x, y) plt.xlabel('Input x') plt.ylabel('Output y') plt.title("Training Data") plt.show() # In[18]: 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) # In[20]: # 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() # In[ ]: # pip install tensorflow # comment out this line to install # In[21]: 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) # In[ ]: