<jupyter_start><jupyter_text>```Topic:    Challenge Set 8Subject:  Classification ErrorsDate:     02/13/2017Name:     <NAME>```<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import cross_val_predict

from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
from sklearn.metrics import roc_auc_score

%matplotlib inline
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/voting-records/house-votes-84.data', header=None)
df = df.replace('y',1)
df = df.replace('n',0)
for i in range(1,17):
    df[i] = df[i].replace('?',(df[i].replace('?',np.nan)).mean())
y = df[0]
X = df[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.30, random_state=4444)<jupyter_output><empty_output><jupyter_text>**Challenge 1** KNN<jupyter_code>knn = KNeighborsClassifier(n_neighbors=6)
knn.fit(X_train, y_train)
y_test_predicted = knn.predict(X_test)
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>lr = LogisticRegression()
lr.fit(X_train, y_train)
y_score_lr = lr.fit(X_train, y_train).decision_function(X_test)
y_test_predicted = lr.predict(X_test)
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>Guess Democrat<jupyter_code>y_test_predicted = ['democrat']*131
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>Guess Republican<jupyter_code>y_test_predicted = ['republican']*131
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>Gaussian Naive Bayes<jupyter_code>nb = GaussianNB()
nb.fit(X_train, y_train)
y_test_predicted = nb.predict(X_test)
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>Support Vector Machine<jupyter_code>svm = SVC()
svm.fit(X_train, y_train)
y_score_svm = svm.fit(X_train, y_train).decision_function(X_test)
y_test_predicted = svm.predict(X_test)
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
y_test_predicted = dt.predict(X_test)
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>rf = RandomForestClassifier()
rf.fit(X_train, y_train)
y_test_predicted = rf.predict(X_test)
accuracy_score(y_test, y_test_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y_test, y_test_predicted)<jupyter_output><empty_output><jupyter_text>**Challenge 2**  ROC curves are plotted using `.decision_function(X_test)`, where available, for the above classifiers.<jupyter_code>fpr_lr, tpr_lr, _ = roc_curve(y_test,y_score_lr, pos_label='republican')
auc_lr = auc(fpr_lr,tpr_lr)
fpr_svm, tpr_svm, _ = roc_curve(y_test,y_score_svm, pos_label='republican')
auc_svm = auc(fpr_svm,tpr_svm)
plt.plot(fpr_lr, tpr_lr, color='darkorange',
         lw=2, label='Logistic Regression (area = %0.3f)' % auc_lr)
plt.plot(fpr_svm, tpr_svm, color='green',
         lw=2, label='Support Vector Classifier (area = %0.3f)' % auc_svm)
plt.plot([0,1], [0,1], color='navy', lw=2, linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.xlim([-0.05,1.0])
plt.ylim([0.0,1.05])
plt.title('Receiver operating characteristic')
plt.legend(loc=4)<jupyter_output><empty_output><jupyter_text>**Challenge 3** KNN<jupyter_code>y_predicted = cross_val_predict(KNeighborsClassifier(n_neighbors=6), X, y, cv=10)
accuracy_score(y, y_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>y_predicted = cross_val_predict(LogisticRegression(), X, y, cv=10)
accuracy_score(y, y_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>Gaussian NB<jupyter_code>y_predicted = cross_val_predict(GaussianNB(), X, y, cv=10)
accuracy_score(y, y_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>Support Vector<jupyter_code>y_predicted = cross_val_predict(SVC(), X, y, cv=10)
accuracy_score(y, y_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>y_predicted = cross_val_predict(DecisionTreeClassifier(), X, y, cv=10)
accuracy_score(y, y_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>y_predicted = cross_val_predict(RandomForestClassifier(), X, y, cv=10)
accuracy_score(y, y_predicted)
# Precision, Recall, F1, Support ['democrat','republican']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>**Challenge 4**<jupyter_code>mv = pd.read_csv('2013_movies.csv')
model_mv = mv[['Rating','Budget','DomesticTotalGross','Runtime']].dropna()
y = model_mv['Rating']
X = model_mv[['Budget','DomesticTotalGross','Runtime']]
(model_mv.groupby('Rating')['Budget'].count())<jupyter_output><empty_output><jupyter_text>KNN<jupyter_code>y_predicted = cross_val_predict(KNeighborsClassifier(n_neighbors=5), X, y, cv=10)
# Precision, Recall, F1, Support ['PG','PG-13','R']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>y_predicted = cross_val_predict(LogisticRegression(), X, y, cv=10)
# Precision, Recall, F1, Support ['PG','PG-13','R']
precision_recall_fscore_support(y, y_predicted)<jupyter_output><empty_output><jupyter_text>**Challenge 5**<jupyter_code>hab = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/haberman/haberman.data', header=None)
hab.columns = ['age','yr','nodes','survived5']
y = hab['survived5']
X = hab[['age','yr','nodes']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.30, random_state=4444)
lr = LogisticRegression()
y_score_lr = lr.fit(X_train, y_train).decision_function(X_test)
fpr_lr, tpr_lr, _ = roc_curve(y_test, y_score_lr, pos_label=2)
auc_lr = auc(fpr_lr,tpr_lr)
plt.plot(fpr_lr, tpr_lr, color='darkorange',
         lw=2, label='Logistic Regression (AUC = %0.2f)' % auc_lr)
plt.plot([0,1], [0,1], color='navy', lw=2, linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.xlim([-0.05,1.0])
plt.ylim([0.0,1.05])
plt.title('Receiver operating characteristic')
plt.legend(loc=4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13709380748007358, 'fraction_numerical': 0.018393623543838136, 'mean_word_length': 10.718390804597702, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 82, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '139372', 'license': 'MIT', 'n_tokens_mistral': 3357, 'n_tokens_neox': 3175, 'n_words': 459, 'path': 'Projects/Challenges/challenge_set_8_prashant.ipynb', 'repo_name': 'ptpro3/ptpro3.github.io', 'size': '8166'}