-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppendix.tex
186 lines (153 loc) · 7.6 KB
/
Appendix.tex
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
\chapter{Standard Machine Learning Language Supplemental Code}
\section{Iris Python Code} \label{Appendix:Iris}
The code required to replicate the same actions of the SML \(Query\) in Figure \ref{fig:SML:IrisQuery}. It is important to note that detailed documentation is publicly available. The purpose of this listing is to highlight the level of complexity relative to a SML query.
\begin{lstlisting}[language=python]
import pandas as pd
import numpy as np
from sklearn.preprocessing import label_binarize
import sklearn.cross_validation as cv
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
import seaborn as sns
names = ['sepal length(cm)', 'sepal width(cm)', 'petal length(cm)', 'petal width(cm)', 'species']
data = pd.read_csv('../data/iris.csv', names=names)
iris_classes = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']
features = np.c_[data.drop('species',1).values]
labels = label_binarize(data['species'], classes=iris_classes)
n_classes = labels.shape[1]
x_train, x_test, y_train, y_test = cv.train_test_split(features, labels, test_size=0.25)
svm = OneVsRestClassifier(SVC(kernel='linear', probability=True))
l = svm.fit(x_train, y_train)
predict_score = model.decision_function(x_test)
test_set_results = model.score(x_test, y_test) * 100
print ('SVM Prediction Accuracy = {0:6.2f}%'.format(test_set_results) )
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], predict_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
plt.rcParams['figure.figsize']=(12,12)
# Class Info
columns = [0,1,2,3]
cmap_class = ['Purples_r', 'Greens_r', 'Oranges_r', 'Greys_r' ]
color_class1D = ['purple', 'darkgreen', 'orange', 'grey']
column_headers = data.columns.values.tolist() # Grab headers from df
column_headers = [column_headers[x] for x in columns] # Map headers to indices selected
label = 'species'
fig, ax = plt.subplots(len(columns), len(columns))
for ic, cc, cc1D in zip(iris_classes, cmap_class, color_class1D):
iris_class_data = data.loc[data.species == ic] # sep class
#Generate kde plot matrix for class
for col1, i in enumerate(columns):
for col2, j in enumerate(columns):
if i == j:
sns.kdeplot(iris_class_data[iris_class_data.columns[col1]], ax=ax[col1][col2], color=cc1D, shade=True, legend=False)
else:
sns.kdeplot( iris_class_data[iris_class_data.columns[col1]], iris_class_data[iris_class_data.columns[col2]], ax=ax[col1][col2], cmap=cc)
# Formatting
if j == 0:
ax[i,j].set_xticklabels([])
ax[i,j].set_ylabel(column_headers[i])
ax[i,j].set_xlabel('')
if i == len(columns)-1:
ax[i,j].set_xlabel(column_headers[j])
elif i == len(columns)-1:
ax[i,j].tick_params(axis='y', which='major', bottom='off')
ax[i,j].set_yticklabels([])
ax[i,j].set_xlabel(column_headers[j])
ax[i,j].set_ylabel('')
else:
ax[i,j].set_xticklabels([])
ax[i,j].set_xlabel('')
ax[i,j].set_yticklabels([])
ax[i,j].set_ylabel('')
plt.show()
plt.close()
\end{lstlisting}
\clearpage
\section{Auto-MPG Python Code} \label{Appendix:Auto}
The code required to replicate the same actions of the SML \(Query\) in Figure \ref{fig:SML:AutoMPGQuery}. It's important to note that detailed documentation is publicly available in \textsuperscript{\ref{lab:SML:AUTO}}, the purpose of this figure is to highlight the level of complexity relative to a SML query.
\begin{lstlisting}[language=python]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.cross_validation import train_test_split
from sklearn.learning_curve import learning_curve, validation_curve
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['figure.figsize']=(12,12)
sns.set()
names = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model_year', 'origin', 'car_name']
#load dataset
data = pd.read_csv('../data/auto-mpg.csv', sep = '\s+', header = None, names = names)
data_clean=data.applymap(lambda x: np.nan if x == '?' else x).dropna()
X = data_clean[['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model_year', "origin"]]
#Select target column
y = data_clean['mpg']
#Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, test_size=0.2)
# Define and train linear regression model
estimator = linear_model.LinearRegression()# Generate Learning Cures
train_sizes, train_scores, test_scores = learning_curve(estimator, X_train, y_train)
# Train Linear Regression Model
estimator.fit(X_train, y_train)# Generate Validation Curves
param_range = np.arange(0, 5)
v_train_scores, v_test_scores = validation_curve(estimator, X_test, y_test, param_name='normalize', param_range=param_range)
score = estimator.score(X_test, y_test)
print('Accuracy :', score)
g = sns.PairGrid(data_clean, palette='PuOr_r')
g = g.map_diag(sns.kdeplot, shade=True) # can't add color arg...
g = g.map_upper(sns.kdeplot, cmap='PuOr_r')
g = g.map_lower(sns.kdeplot, cmap='PuOr_r')
plt.show()
plt.close()
color_pal = ['purple', 'dark green', 'orange', 'grey'] # For 1-D KDE
cmap_pal = ['PuOr_r'] # For 2-D KDE
classes = [] # May not have a class for categories
column_headers = data_clean.columns.values.tolist() # Grab headers from df
column_headers = [column_headers[x] for x in columns] # Map headers to indices selected
fig, ax = plt.subplots(len(columns), len(columns))
if not classes:
for col1, i in enumerate(columns):
for col2, j in enumerate(columns):
if i == j:
sns.kdeplot(data_clean[data_clean.columns[col1]], ax=ax[col1][col2], color=color_pal[0], shade=True, legend=False)
else:
sns.kdeplot( data_clean[data_clean.columns[col1]], data_clean[data_clean.columns[col2]], ax=ax[col1][col2], cmap=cmap_pal[0])
# Formatting
if j == 0:
ax[i,j].set_xticklabels([])
ax[i,j].set_ylabel(column_headers[i])
ax[i,j].set_xlabel('')
if i == len(columns)-1:
ax[i,j].set_xlabel(column_headers[j])
elif i == len(columns)-1:
ax[i,j].tick_params(axis='y', which='major', bottom='off')
ax[i,j].set_yticklabels([])
ax[i,j].set_xlabel(column_headers[j])
ax[i,j].set_ylabel('')
else:
ax[i,j].set_xticklabels([])
ax[i,j].set_xlabel('')
ax[i,j].set_yticklabels([])
ax[i,j].set_ylabel('')
plt.show()
plt.close()
plt.figure()
plt.xlabel("Validation examples")
plt.ylabel("Score")
v_train_scores_mean = np.mean(v_train_scores, axis=1)
v_train_scores_std = np.std(v_train_scores, axis=1)
v_test_scores_mean = np.mean(v_test_scores, axis=1)
v_test_scores_std = np.std(v_test_scores, axis=1)
plt.fill_between(param_range, v_train_scores_mean - v_train_scores_std, v_train_scores_mean + v_train_scores_std, alpha=0.1, color="orange")
plt.fill_between(param_range, v_test_scores_mean - v_test_scores_std, v_test_scores_mean + v_test_scores_std, alpha=0.1, color="purple")plt.plot(param_range, v_train_scores_mean, 'o-', color="orange", label="Training score")
plt.plot(param_range, v_test_scores_mean, 'o-', color="purple", label="Cross-validation score")
plt.legend(loc="best")
plt.show()
plt.close()
\end{lstlisting}