Skip to content

Instantly share code, notes, and snippets.

@alik604
Last active August 13, 2020 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alik604/76861089ea95048239aa7ab7fed136d9 to your computer and use it in GitHub Desktop.
Save alik604/76861089ea95048239aa7ab7fed136d9 to your computer and use it in GitHub Desktop.
!pip install mnist
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import metrics
import mnist
# from hmmlearn.hmm import GaussianHMM, MultinomialHMM
X_train = mnist.train_images()
y_train = mnist.train_labels()
X_test = mnist.test_images()
y_test = mnist.test_labels()
# Normalize the images.
X_train = (X_train / 255) - 0.5
X_test = (X_test / 255) - 0.5
# Flatten the images.
X_train = X_train.reshape((-1, 784))
X_test = X_test.reshape((-1, 784))
print(X_train.shape) # (60000, 784)
print(X_test.shape) # (10000, 784)
# Apply a learning algorithm
print("Applying a learning algorithm...")
clf = RandomForestClassifier(n_estimators=75,n_jobs=4)
clf.fit(X_train, y_train)
# Make a prediction
print("Making predictions...")
y_pred = clf.predict(X_test)
#print y_pred
# Evaluate the prediction
print("Evaluating results...")
print("Precision: \t", metrics.precision_score(y_test, y_pred, average='micro'))
print("Recall: \t", metrics.recall_score(y_test, y_pred, average='micro'))
print("F1 score: \t", metrics.f1_score(y_test, y_pred, average='micro'))
print("Mean accuracy: \t", clf.score(X_test, y_test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment