# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda root]
#     language: python
#     name: conda-root-py
# ---

# ```
# Topic:    Challenge Set 7
# Subject:  Classification
# Date:     02/13/2017
# Name:     <NAME>
# ```

# +
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.learning_curve import learning_curve
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import cross_val_score

# %matplotlib inline
# -

# **Challenge 1**

df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/voting-records/house-votes-84.data', header=None)

df.head()

df.shape

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())

# **Challenge 2**

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)

# **Challenge 3**

# +
accuracy = []

for i in range(1,21):
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(X_train, y_train)
    y_test_predicted = knn.predict(X_test)
    accuracy.append(accuracy_score(y_test, y_test_predicted))
# -

max(accuracy)

# k value giving highest accuracy
accuracy.index(max(accuracy)) + 1

# **Challenge 4**

lr = LogisticRegression()
lr.fit(X_train, y_train)
y_test_predicted = lr.predict(X_test)
lr_accuracy = accuracy_score(y_test, y_test_predicted)
lr_accuracy

# **Challenge 5**

(y.groupby(y).count()).plot.bar()

y_predicted = ['democrat']*len(X)
dem_accuracy = accuracy_score(y, y_predicted)
dem_accuracy

y_predicted = ['republican']*len(X)
rep_accuracy = accuracy_score(y, y_predicted)
rep_accuracy

# **Challenge 6**

k = range(1,21)
plt.scatter(k, accuracy, color='k', label='KNN')
plt.scatter(k, [lr_accuracy]*20, color='g', label='Logistic Regression')
plt.scatter(k, [dem_accuracy]*20, color='b', label='Predict Democrat')
plt.scatter(k, [rep_accuracy]*20, color='r', label='Predict Republican')
plt.legend(loc=4)

# **Challenge 7**

m, train_err, test_err = learning_curve(LogisticRegression(), X, y, cv=10)

m

train_cv_err = np.mean(train_err, axis=1)
test_cv_err = np.mean(test_err, axis=1)

plt.scatter(m, train_cv_err, color='b', label='Training Error')
plt.scatter(m, test_cv_err, color='g', label='Test Error')
plt.title('Learning Curve for Logistic Regression')
plt.legend()

m, train_err, test_err = learning_curve(KNeighborsClassifier(n_neighbors=6), X, y, cv=10)
train_cv_err = 1 - np.mean(train_err, axis=1)
test_cv_err = 1 - np.mean(test_err, axis=1)

plt.scatter(m, train_cv_err, color='b', label='Training Error')
plt.scatter(m, test_cv_err, color='g', label='Test Error')
plt.title('Learning Curve for KNN')
plt.legend()

# **Challenge 8**

# Gaussian Naive Bayes

nb = GaussianNB()
nb.fit(X_train, y_train)
y_test_predicted = nb.predict(X_test)
nb_accuracy = accuracy_score(y_test, y_test_predicted)
nb_accuracy

# Support Vector Machine

svm = SVC()
svm.fit(X_train, y_train)
y_test_predicted = svm.predict(X_test)
svm_accuracy = accuracy_score(y_test, y_test_predicted)
svm_accuracy

# Decision Tree

dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
y_test_predicted = dt.predict(X_test)
dt_accuracy = accuracy_score(y_test, y_test_predicted)
dt_accuracy

# Random Forest

rf = RandomForestClassifier()
rf.fit(X_train, y_train)
y_test_predicted = rf.predict(X_test)
rf_accuracy = accuracy_score(y_test, y_test_predicted)
rf_accuracy

# **Challenge 9**

np.mean(cross_val_score(KNeighborsClassifier(n_neighbors=6), X, y, cv=10))

np.mean(cross_val_score(LogisticRegression(), X, y, cv=10))

np.mean(cross_val_score(GaussianNB(), X, y, cv=10))

np.mean(cross_val_score(SVC(), X, y, cv=10))

np.mean(cross_val_score(DecisionTreeClassifier(), X, y, cv=10))

np.mean(cross_val_score(RandomForestClassifier(), X, y, cv=10))

# **Challenge 10**

vp = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/voting-records/house-votes-84.data', header=None)

vp = vp.replace('y',1)
vp = vp.replace('n',0)
vp.head()

# Vote 16 (the last column) has the most '?'; let's try to predict that one.

for i in range(1,17):
    vp[i] = vp[i].replace('?',vp[i].mode()[0])

vy = vp[16]
vX = vp[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]]

np.mean(cross_val_score(LogisticRegression(), vX, vy, cv=10))

# **Challenge 11**

mv = pd.read_csv('2013_movies.csv')
mv.head()

mv.shape

ratings = mv.groupby('Rating')['Title'].count()

ratings.plot.bar()

model_mv = mv[['Rating','Budget','DomesticTotalGross','Runtime']].dropna()

y = model_mv['Rating']
X = model_mv[['Budget','DomesticTotalGross','Runtime']]

# KNN

# +
accuracy = []

for k in range(1,50):
    accuracy.append(np.mean(cross_val_score(KNeighborsClassifier(n_neighbors=k), X, y, cv=15)))
# -

max(accuracy)

# k value giving highest accuracy
accuracy.index(max(accuracy)) + 1

# Logistic Regression

np.mean(cross_val_score(LogisticRegression(), X, y, cv=15))

np.mean(cross_val_score(LogisticRegression(multi_class='multinomial', solver='lbfgs'), X, y, cv=15))

np.mean(cross_val_score(LogisticRegression(multi_class='multinomial', solver='newton-cg'), X, y, cv=15))

lr = LogisticRegression(multi_class='multinomial', solver='newton-cg')
lr.fit(X,y)
lr.coef_

# Baseline Predictor

pg13_predictor = pd.Series(['PG-13']*89)

accuracy_score(y, pg13_predictor)

# **Challenge 12**

hab = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/haberman/haberman.data', header=None)

hab.columns = ['age','yr','nodes','survived5']
hab.head()

# Age - all patients

hab.age.mean()

hab.age.std()

# Age - patients who survived 5+ yrs

hab.groupby('survived5')['age'].mean()[1]

hab.groupby('survived5')['age'].std()[1]

# Age - patients survived < 5 yrs

hab.groupby('survived5')['age'].mean()[2]

hab.groupby('survived5')['age'].mean()[2]

hab.age.plot.hist()

hab.nodes.plot.hist()

((hab.sort_values('yr')).yr).head()

((hab.sort_values('yr', ascending=False)).yr).head()

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()
lr.fit(X_train, y_train)
y_test_predicted = lr.predict(X_test)
lr_accuracy = accuracy_score(y_test, y_test_predicted)
lr_accuracy

lr.coef_

# The magnitudes of the coefficients suggest that 'nodes' has the strongest effect of reducing survival rate.

m, train_err, test_err = learning_curve(LogisticRegression(), X, y)

m

train_cv_err = 1- np.mean(train_err, axis=1)
test_cv_err = 1- np.mean(test_err, axis=1)

plt.scatter(m, train_cv_err, color='b', label='Training Error')
plt.scatter(m, test_cv_err, color='g', label='Test Error')
plt.title('Learning Curve for Logistic Regression on Haberman')
plt.legend()



StarcoderdataJupyterScriptsDedupFiltered