# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# **Chapter 04**

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

# # 10
# ## 10(a)

weekly_file_path = '../data/Weekly.csv'
weekly = pd.read_csv(weekly_file_path, index_col=0)
weekly.head()

weekly.describe()

from pandas.tools.plotting import scatter_matrix
weekly_refine = weekly[['Lag1','Lag2','Lag3','Lag4','Lag5','Volume','Today','Direction']]
weekly_refine=weekly_refine.replace('Up',1)
weekly_refine=weekly_refine.replace('Down',0)
fig, ax = plt.subplots(figsize=(15, 15))
scatter_matrix(weekly_refine,alpha=0.5,diagonal='kde', ax=ax);

weekly_refine.corr()

# # 10(b)

from sklearn.linear_model import LogisticRegression
X = weekly_refine[['Lag1','Lag2','Lag3','Lag4','Lag5','Volume']].values
y = weekly_refine['Direction'].values
y = y.reshape((len(y),1))
lg = LogisticRegression()
lg.fit(X,y)

print(lg.coef_,lg.intercept_)

# ## 10(c)

from sklearn.metrics import confusion_matrix,accuracy_score
pred = lg.predict(X)
confusion_matrix(y,pred)

print(accuracy_score(y,pred))

# ## 10(d)

df_train = weekly[weekly['Year'].isin(range(1990,2009))]
df_test = weekly[weekly['Year'].isin(range(2009,2011))]
# training data
X_train = df_train['Lag2'].values
X_train = X_train.reshape((len(X_train),1))
y_train = df_train['Direction'].values
# test data
X_test = df_test['Lag2'].values
X_test = X_test.reshape((len(X_test),1))
y_test = df_test['Direction'].values
# lg
lg = LogisticRegression()
lg.fit(X_train, y_train)

pred_test = lg.predict(X_test)
print(confusion_matrix(y_test, pred_test))

print(accuracy_score(y_test,pred_test))

# # 10(e)

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
lda = LinearDiscriminantAnalysis()
lda.fit(X_train,y_train)

pred_test = lda.predict(X_test)
print(confusion_matrix(y_test, pred_test))

print(accuracy_score(y_test,pred_test))

# ## 10(f)

qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)

pred_test = qda.predict(X_test)
print(confusion_matrix(y_test, pred_test))

print(accuracy_score(y_test, pred_test))

# ## 10(g)

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)

pred_test = knn.predict(X_test)
print(confusion_matrix(y_test, pred_test))

print(accuracy_score(y_test, pred_test))

# # 11

auto_file_path = '../data/Auto'
autos = pd.read_table(auto_file_path,sep='\s+')
autos=autos.replace('?',np.NAN).dropna()
autos['horsepower']=autos['horsepower'].astype('float')
autos.head()

# ## 11(a)

mpgs = autos['mpg'].values
mpg_med = np.median(mpgs)
mpg0 = [1 if mpg > mpg_med else 0 for mpg in mpgs]
autos['mpg0'] = mpg0
autos.head()

# ## 11(b)

fig, ax = plt.subplots(figsize=(15, 15))
scatter_matrix(autos[['cylinders','displacement','horsepower','weight','acceleration','mpg0']],ax=ax);

autos[['cylinders','displacement','horsepower','weight','acceleration','mpg0']].boxplot(by='mpg0');

# ## 11(c)

autos_train = autos[autos.apply(lambda x: x['year'] %2 ==0, axis=1)]
autos_test = autos[autos.apply(lambda x:x['year'] % 2 != 0, axis=1)]
variables = ['cylinders','weight','displacement','horsepower']
response = ['mpg0']
X_train = autos_train[variables].values
y_train = autos_train[response].values
y_train = y_train.reshape((len(y_train)))
X_test =  autos_test[variables].values
y_test = autos_test[response].values
y_test = y_test.reshape((len(y_test)))

# ## 11(d)

lda = LinearDiscriminantAnalysis()
lda.fit(X_train, y_train)

pred = lda.predict(X_test)
print(accuracy_score(y_test, pred))

# ## 11(e)

qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)

pred = qda.predict(X_test)
print(accuracy_score(y_test, pred))

# ## 11(f)

lg = LogisticRegression()
lg.fit(X_train, y_train)

pred = lg.predict(X_test)
print(accuracy_score(y_test, pred))

# ## 11(g)

ks=[1,3,5,7,9,11,13,15]
accur={}
for k in ks:
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train,y_train)
    pred = knn.predict(X_test)
    accur[k]=accuracy_score(y_test,pred)
for k,v in accur.items():
    print('k is %d :'%k, 'accuracy is %f'%v)


# When $k$ equals to $3$, the knn classifier has the highest accuracy scores, $0.8625$.

# # 12

# ## 12(a)

def Power():
    return 2*2*2
Power()


# ## 12(b)

def Power2(x,a):
    res = 1
    while a>=1:
        res *= x
        a -= 1
    return res
Power2(3,8)

# ## 12(c,d)

Power2(10,3)

Power2(8,17)

Power2(131,3)

# ## 12(e)

x = range(1,11,1)
y = [Power2(item,2) for item in x]
plt.plot(x,y)
plt.show()

# # 13

boston_file_name = '../data/Boston.csv'
bostons = pd.read_csv(boston_file_name, index_col=0)
bostons.head()

crims = bostons['crim'].values
crims_med = np.median(crims)
cime_statue = [1 if crim > crims_med else 0 for crim in crims]
bostons['crim_statue'] = cime_statue

X = bostons[['dis']].values
y = bostons['crim_statue'].values
lg = LogisticRegression()
lg.fit(X,y)

print(lg.coef_,lg.intercept_)

# The logistic regression coefficient is $-0.95466145$, that is negitive, We infer that distance will eliminate the influence on crime. the distances increase 1 unit, the logit ($log(\frac{\text{high crime ratio}}{\text{low crim ratio}})$)of crime will decrease $-0.95$ unit.

StarcoderdataJupyterScriptsDedupFiltered