IST718 LAB 9: Fashion MNIST with Naive Bayes & Support Vector Machines

In [29]:
## =======================================================
## MODELING
## =======================================================
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import BernoulliNB, MultinomialNB

## ----- CLASSIFIERS
mnb = MultinomialNB()
svm = LinearSVC(C=1)

def get_test_train(X,y):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)
    return X_train, X_test, y_train, y_test

def run_classifier(X_train, X_test, y_train, y_test, labels, target_names, classifier):
    clf = classifier
    clf.fit(X_train,y_train)
    y_pred = clf.predict(X_test)
    report = classification_report(y_test, y_pred, target_names=target_names,output_dict=True)
    score = clf.score(X_test,y_test)
    return clf, score, report
    
def get_model(X, y, labels, target_names, classifier):
    X_train, X_test, y_train, y_test = get_test_train(X,y)
    model, score, report = run_classifier(X_train, X_test, y_train, y_test, labels, target_names, classifier)
    return model, score, report
    
In [71]:
import pandas as pd
train_og = pd.read_csv('fashionmnist/fashion-mnist_train.csv')
y=train_og['label'].values
X = train_og[train_og.columns[1:]]
In [72]:
train = train_og.copy()
train[(train != 0)] = 1
In [73]:
X_01 = train[train.columns[1:]]
In [75]:
classifier = mnb
model, score, report = get_model(X,y,[0,1,2,3,4,5,6,7,8,9],['0','1','2','3','4','5','6','7','8','9'], classifier)
score
Out[75]:
0.6664583333333334
In [76]:
classifier = mnb
model, score, report = get_model(X_01,y,[0,1,2,3,4,5,6,7,8,9],['0','1','2','3','4','5','6','7','8','9'], classifier)
score
Out[76]:
0.5968333333333333
In [77]:
classifier = svm
model, score, report = get_model(X,y,[0,1,2,3,4,5,6,7,8,9],['0','1','2','3','4','5','6','7','8','9'], classifier)
score
/Users/danielcaraway/anaconda3/lib/python3.7/site-packages/sklearn/svm/base.py:929: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)
Out[77]:
0.7375
In [78]:
classifier = svm
model, score, report = get_model(X_01,y,[0,1,2,3,4,5,6,7,8,9],['0','1','2','3','4','5','6','7','8','9'], classifier)
score
/Users/danielcaraway/anaconda3/lib/python3.7/site-packages/sklearn/svm/base.py:929: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)
Out[78]:
0.829875