Skip to content

Instantly share code, notes, and snippets.

View agastidukare's full-sized avatar

Agasti Kishor Dukare agastidukare

View GitHub Profile
result = classifier(zipped_clf, x_train, y_train, x_test, y_test)
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
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)
classifier_names = ["Logistic Regression", "KNN", "Random Forest","SVM"]
classifiers = [LogisticRegression(), KNeighborsClassifier(), RandomForestClassifier(), LinearSVC()]
zipped_clf = zip(classifier_names,classifiers)
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)
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
# Load perturbted image and print predictions after pertubation
test_images = per_img
test_labels = label
grads,acc = covnet(1,params)
# 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)
# 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
def display(image):
img = image[0].reshape((28,28))
plt.imshow(img, cmap="Greys")
plt.show()
return
display(a)