<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Classifying Images With Scikit_Learn
Step1:  Naive Bayes Using Scikit_Lerarn
Step2:  Pre-Processing The Data
machine learning algorithms can work only on numeric data, so our next step will be to convert our text-based dataset to a numeric dataset
<ASSISTANT_TASK:>
Python Code:
import sklearn as sk
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn .datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces()
faces.DESCR
faces.keys()
faces.images.shape
faces.data.shape
faces.target.shape
np.max(faces.data)
np.min(faces.data)
np.median(faces.data)
def print_faces(images , target , top_n):
    fig = plt.figure(figsize=(20,20))
    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,59,str(i))
print_faces(faces.images,faces.target,20)
plt.show()
from sklearn.svm import SVC
from sklearn.cross_validation import cross_val_score,KFold
from scipy.stats import sem
svc_1 = SVC(kernel='linear')
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)
def evaluate_cross_validation(clf, X, y, K):
    cv = KFold(len(y) , K, shuffle =True, random_state = 0)
    scores = cross_val_score(clf,X,y,cv=cv)
    print scores
evaluate_cross_validation(svc_1,X_train,y_train,5)
from sklearn import metrics
def train_and_test(clf, X_train, X_test, y_train, y_test):
    clf.fit(X_train, y_train)
    print "Accuracy on training Set"
    print clf.score(X_train, y_train)
    print "Accuracy on testing Set"
    print clf.score(X_test, y_test)
    
    y_pred = clf.predict(X_test)
    print "Classification Report"
    print metrics.classification_report(y_test, y_pred)
    print "Confudion Matrix"
    print metrics.confusion_matrix(y_test, y_pred)
train_and_test(svc_1, X_train, X_test, y_train, y_test)
glasses = [
 (10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
 (69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
 (164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
 (194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
 (330, 339), (358, 359), (360, 369)]
def create_target(segments):
    y = np.zeros(faces.target.shape[0])
    for (start, end) in segments:
        y[start:end+1] = 1
    return y
target_glasses = create_target(glasses)
X_train, X_test, y_train, y_test = train_test_split(faces.data, target_glasses, test_size=0.25, random_state=0)
svc_2 = SVC(kernel='linear')
evaluate_cross_validation(svc_2, X_train, y_train, 5)
train_and_test(svc_2, X_train, X_test, y_train, y_test)
X_test = faces.data[30:40]
y_test = target_glasses[30:40]
y_test.shape
select = np.ones(target_glasses.shape[0])
select[30:40] = 0
X_train = faces.data[select == 1]
y_train = target_glasses[select == 1]
y_train.shape
svc_3 = SVC(kernel='linear')
train_and_test(svc_3, X_train, X_test, y_train, y_test)
Explanation: Classifying Images With Scikit_Learn
End of explanation
from sklearn.datasets import fetch_20newsgroups
news = fetch_20newsgroups(subset='all')
print type(news.data), type(news.target), type(news.target_names)
print news.target_names
len(news.data)
len(news.target)
news.data[0] #Content of the data at 0th index
news.target[0], news.target_names[news.target[0]] # Target_Name 
Explanation: Naive Bayes Using Scikit_Lerarn
End of explanation
SPLIT_PERC = .75
split_size = int(len(news.data)*SPLIT_PERC)
X_train = news.data[:split_size]
X_test = news.data[split_size:]
y_train = news.target[:split_size]
y_test = news.target[split_size:]
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer, HashingVectorizer, CountVectorizer
clf_1 = Pipeline([('vect', CountVectorizer()), ('clf', MultinomialNB())])
clf_2 = Pipeline([('vect', HashingVectorizer(non_negative=True)), ('clf', MultinomialNB())])
clf_3 = Pipeline([('vect', TfidfVectorizer()), ('clf', MultinomialNB())])
from sklearn.cross_validation import cross_val_score, KFold
from scipy.stats import sem
clfs = [clf_1, clf_2, clf_3]
for clf in clfs:
    print clf
    evaluate_cross_validation(clf, news.data, news.target, 5)    
Explanation: Pre-Processing The Data
machine learning algorithms can work only on numeric data, so our next step will be to convert our text-based dataset to a numeric dataset
End of explanation
<END_TASK>