In [40]:
%pylab inline
import IPython
import sklearn as sk
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces()
def print_faces(images, target, top_n):
fig = plt.figure(figsize=(12, 12))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(top_n):
p = fig.add_subplot(20, 20, i + 1, xticks=[], yticks=[])
p.imshow(images[i], cmap=plt.cm.bone)
p.text(0, 14, str(target[i]))
p.text(0, 60, str(i))
print_faces(faces.images, faces.target, 20)
#print_faces(faces.images, faces.target, 400)
In [41]:
from sklearn.svm import SVC
svc_1 = SVC(kernel='linear')
print(svc_1)
In [42]:
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
faces.data, faces.target, test_size=0.25, random_state=0)
In [43]:
from sklearn.cross_validation import cross_val_score, KFold
from scipy.stats import sem
def evaluate_cross_validation(clf, X, y, K):
# creamos un k-fold croos validation iterator
cv = KFold(len(y), K, shuffle=True, random_state=0)
# por defecto el puntaje usado es el devuelto por el estimator (exactitud)
scores = cross_val_score(clf, X, y, cv=cv)
print(scores)
print(("Mean score: {0:.3f} (+/-{1:.3f})").format(
np.mean(scores), sem(scores)))
evaluate_cross_validation(svc_1, X_train, y_train, 5)
In [44]:
from sklearn import metrics
from sklearn.metrics import accuracy_score
def train_and_evaluate(clf, X_train, X_test, y_train, y_test):
clf.fit(X_train, y_train)
print("Exactitud training set:")
print(clf.score(X_train, y_train))
print("Exactitud testing set:")
print(clf.score(X_test, y_test))
y_pred = clf.predict(X_test)
print("Reporte de Classificador:")
print(metrics.classification_report(y_test, y_pred))
print("Confusion Matrix:")
print(metrics.confusion_matrix(y_test, y_pred))
print accuracy_score(y_test,y_pred)
train_and_evaluate(svc_1, X_train, X_test, y_train, y_test)
In [46]:
#Import Library
from sklearn.svm import SVC
from sklearn.datasets import fetch_olivetti_faces
face = fetch_olivetti_faces()
X = face.data
y = face.target
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=.5)
#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
# Create SVM classification object
model = SVC(kernel='linear')
#model = svm.svc() # there is various option associated with it, this is simple for classification. You can refer link, for mo# re detail.
# Train the model using the training sets and check score
model.fit(X_train, y_train)
print model.score(X, y)
#Predict Output
predicted= model.predict(X_test)
In [48]:
from IPython.display import display, clear_output
class Trainer:
def __init__(self):
self.results = {}
self.imgs = faces.images
self.index = 0
def increment_face(self):
if self.index + 1 >= len(self.imgs):
return self.index
else:
while str(self.index) in self.results:
print self.index
self.index += 1
return self.index
def record_result(self, smile=True):
self.results[str(self.index)] = smile
trainer = Trainer()