w2-practice-03--Perceptron



In [114]:
import pandas as pd
import numpy as np
import sklearn as sk
from sklearn.linear_model import Perceptron
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score

In [115]:
data_train = pd.read_csv('resources/perceptron-train.csv', header=None)
data_test = pd.read_csv('resources/perceptron-test.csv', header=None)

In [116]:
X_train = data_train.copy()
X_train.columns = ["Target", "A","B"]
X_train_target = X_train["Target"]
X_train_data = X_train[["A", "B"]]

X_test = data_test.copy()
X_test.columns = ["Target", "A","B"]
X_test_target = X_test["Target"]
X_test_data = X_test[["A", "B"]]

In [117]:
perceptron1 = Perceptron(random_state=241) 
perceptron1.fit(X_train_data, X_train_target)
predictions1 = perceptron1.predict(X_test_data)
score1 = accuracy_score(X_test_target, predictions1)

In [119]:
scaler = StandardScaler()
X_train_data_scaled = scaler.fit_transform(X_train_data)
X_test_data_scaled = scaler.transform(X_test_data)

perceptron2 = Perceptron(random_state=241)
perceptron2.fit(X_train_data_scaled, X_train_target)
predictions2 = perceptron2.predict(X_test_data_scaled)

score2 = accuracy_score(X_test_target, predictions2)

In [120]:
print("Score 1: %.3f" % score1 )
print("Score scaled: %.3f" % score2 )
print("Total score: %.3f" % (score2 - score1))


Score 1: 0.655
Score scaled: 0.845
Total score: 0.190