This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result = classifier(zipped_clf, x_train, y_train, x_test, y_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fit_classifier(pipeline, x_train, y_train, x_test, y_test): | |
model_fit = pipeline.fit(x_train, y_train) | |
y_pred = model_fit.predict(x_test) | |
accuracy = accuracy_score(y_test, y_pred) | |
print("accuracy score: {0:.2f}%".format(accuracy*100)) | |
return accuracy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def classifier(classifier, t_train, c_train, t_test, c_test): | |
result = [] | |
for n,c in classifier: | |
checker_pipeline = Pipeline([ | |
('standardize', StandardScaler()), | |
('classifier', c) | |
]) | |
print("Validation result for {}".format(n)) | |
print(c) | |
clf_acc = fit_classifier(checker_pipeline, t_train, c_train, t_test,c_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
classifier_names = ["Logistic Regression", "KNN", "Random Forest","SVM"] | |
classifiers = [LogisticRegression(), KNeighborsClassifier(), RandomForestClassifier(), LinearSVC()] | |
zipped_clf = zip(classifier_names,classifiers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iris = datasets.load_iris() | |
x = iris.data | |
y = iris.target | |
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn import datasets | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.neighbors import KNeighborsClassifier | |
from sklearn.svm import LinearSVC | |
from sklearn.ensemble import RandomForestClassifier | |
from sklearn.pipeline import Pipeline | |
from sklearn.metrics import accuracy_score |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load perturbted image and print predictions after pertubation | |
test_images = per_img | |
test_labels = label | |
grads,acc = covnet(1,params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This function calculates perturbted image | |
def perturb(grad,img,ep): | |
grads = grad[0] | |
a = numpy.reshape(grads, (1, 784)) | |
s = np.sign(a) | |
perturbed_image = img + np.dot(ep,s) | |
return perturbed_image | |
per_img = perturb(grads,img,0.3) | |
display(per_img) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load desired image and its label in test set | |
def load_img(image,img_label): | |
img = np.array(image) | |
img = img.reshape(1,784) | |
label = np.array(img_label) | |
label = label.reshape(1,10) | |
return img, label | |
img, label = load_img(test_images[0],test_labels[0]) | |
test_images = img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def display(image): | |
img = image[0].reshape((28,28)) | |
plt.imshow(img, cmap="Greys") | |
plt.show() | |
return | |
display(a) |