forked from venu-guntupalli/Rossman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KAGGLE_MODELS.py
136 lines (123 loc) · 5.97 KB
/
KAGGLE_MODELS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 20 19:30:15 2015
@author: apple
"""
import re
import datetime
import os
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
from sklearn.metrics import zero_one_loss
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.cross_validation import cross_val_score
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import export_graphviz
from pandas.stats.api import ols
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR
########################################################################################
print ('############################### START OF PART 1 ################################')
#Assign the absolute path of the data file to a variable
file_path_mastertrain = os.path.abspath('C:/Sathya/Subject/Fall-2015/BIA-656/Project/Rossman/mastertrain.csv')
file_path_mastertest = os.path.abspath('C:/Sathya/Subject/Fall-2015/BIA-656/Project/Rossman/mastertest.csv')
#Assign the contents of the datafile to a Pandas dataframe
#Indicate to treat first row as column names
df_mastertrain = pd.read_csv(file_path_mastertrain, sep=',', header=0)
df_mastertest = pd.read_csv(file_path_mastertest, sep=',', header=0)
print ('############################### END OF PART 1 ##################################\n')
########################################################################################
print ('############################### START OF PART 2 ################################')
#Build models and compare their properties in this part
#Declare dependent and independent variables
independent_vars = df_mastertrain[['Open', 'Promo', 'SchoolHoliday', 'CompetitionDistance', 'Promo2','StoreTypeB','AssortmentC','AssortmentA','DoW1','DoW3','DoW4','DoW7','StateHolidayB', 'NoOfWeeksSinceCompetition', 'NoOfWeeksSincePromo2','PromoIntervalJan', 'PromoIntervalApr', 'PromoIntervalJul', 'PromoIntervalOct', 'PromoIntervalFeb', 'PromoIntervalMay', 'PromoIntervalAug', 'PromoIntervalNov', 'PromoIntervalMar', 'PromoIntervalJun', 'PromoIntervalSept', 'PromoIntervalDec']]
dependent_var = df_mastertrain['Sales']
print('-----------------------------------')
reg = ols(y= dependent_var,x=independent_vars)
print reg
#rf = RandomForestRegressor() # initialize
#rf.fit(independent_vars,dependent_var)
#print 'R-squared for Random Forest: ',rf.score(independent_vars,dependent_var)
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_lin = SVR(kernel='linear', C=1e3)
svr_poly = SVR(kernel='poly', C=1e3, degree=2)
svr_rbf.fit(independent_vars,dependent_var)
svr_lin.fit(independent_vars,dependent_var)
svr_poly.fit(independent_vars,dependent_var)
print 'R-squared for SVR: ',svr_rbf.score(independent_vars,dependent_var)
print 'R-squared for SVR: ',svr_lin.score(independent_vars,dependent_var)
print 'R-squared for SVR: ',svr_poly.score(independent_vars,dependent_var)
##CART
#print (' CART Model')
##http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html
#clf = DecisionTreeRegressor(random_state = 0)
#
##Fit using model
#clf.fit(df_mastertrain[independent_vars], df_mastertrain[dependent_var])
#
##Predict using model
#clf.predict(df_mastertest[independent_vars])
#
#df_result = pd.DataFrame(df_mastertest['Id'])
#
#df_result['Sales'] = clf.predict(df_mastertest[independent_vars])
#
#df_result.to_csv('result.csv', sep=',', index=False, columns = ['Id','Sales'], encoding='utf-8')
#
#print (export_graphviz(clf, out_file='tree.dot'))
'''
print('###################')
print(' Test Error Rate: ')
print('###################')
accuracy_clf = metrics.zero_one_loss(df_mastertrain['Sales'], clf.predict(df_mastertest[independent_vars]))
print (1-accuracy_clf)
print('#########################')
print(' Classification Report: ')
print('#########################')
print(metrics.classification_report(df_mastertrain['Sales'], clf.predict(df_mastertest[independent_vars])))
print('############')
print(' Crosstab: ')
print('############')
print(pd.crosstab(df_mastertest['Sales'], clf.predict(df_mastertrain[independent_vars]), margins = True))
print('####################')
print(' Confusion Matrix: ')
print('####################')
print(metrics.confusion_matrix(df_mastertrain['Sales'], clf.predict(df_mastertest[independent_vars])))
print('############')
print(' ROC Curve: ')
print('############')
false_positive_rate, true_positive_rate, thresholds = roc_curve(df_mastertrain['Sales'], clf.predict(df_mastertest[independent_vars]))
roc_auc = auc(false_positive_rate, true_positive_rate)
plt.title('Receiver Operating Characteristic Curve')
plt.plot(false_positive_rate, true_positive_rate, 'g', label='AUC = %0.2f'% roc_auc)
plt.plot([0,1],[0,1],'--', label='Pure chance') #random probability, i.e., curve for model with 50-50 classification
plt.legend(loc="lower right")
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
print('#########################')
print(' Precision-Recall Curve: ')
print('#########################')
precision, recall, thresholds = precision_recall_curve(df_mastertrain['Sales'], clf.predict(df_mastertest[independent_vars]))
prc_auc = auc(recall, precision) #even the below line can be used to get the same result
#prc_auc = average_precision_score(test['class'], clf.predict(test[independent_vars]), average='macro', sample_weight=None)
plt.clf() #clear current figure
plt.plot(recall, precision, 'g', label='AUC = %0.2f'% prc_auc)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.title('Precision-Recall Curve')
plt.legend(loc="lower right")
plt.show()
'''
print ('############################### END OF PART 2 ##################################\n')