<jupyter_start><jupyter_text>PERCEPTRON  LOAD THE DATA<jupyter_code>import numpy as np

# each row contains Sepal length in cm, Sepal width in nm and type (0|1)
# 0: Iris-setosa | 1: Iris-versicolor
data = np.loadtxt('iris-data.csv', delimiter=',')
data<jupyter_output><empty_output><jupyter_text>VISUALIZE THE DATAIt is a good idea to visualize the data so we can confirm that the data is linearly separable.<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt

plt.grid()

for i in range(len(data)) :
    point = data[i]
    if point[2] == 0 :
        color = 'r'  # setosas will appear in blue
    else:
        color = 'b'  # versicolor will appear in red
    
    plt.scatter(point[0], point[1], c=color);
from IPython.display import Image
from IPython.core.display import HTML 

Image(url= 'https://farm9.staticflickr.com/8383/8675226902_e72273713f_k.jpg', width=350, height=350)<jupyter_output><empty_output><jupyter_text>DEFINE TRAINING AND TESTING SETS<jupyter_code>from sklearn.model_selection import train_test_split

target = data[:, -1]
data = data[:, :-1]

X_train, X_test, y_train, y_test = train_test_split(
    data, target, test_size=0.30, random_state=42)

X_train.shape<jupyter_output><empty_output><jupyter_text>TRAIN THE MODEL<jupyter_code>from sklearn.metrics import mean_squared_error

np.random.seed(93)

class Perceptron(object):
    def __init__(self, learning=0.01, n_epochs=20):
        self.learning = learning
        self.n_epochs = n_epochs
    
    def predict(self, X):
        pred = np.dot(X, self.w_) + self.b_
        return 1.0 if pred >= 0.0 else 0.0
    
    def fit(self, X, y):
        # iniciate the weights and bias
        self.w_ = np.random.uniform(0, 1, X.shape[1])
        self.b_ = np.random.uniform(0, 1, 1)
        
        self.costList_ = []

        for ep in range(self.n_epochs):
            cost_epoch = 0
            for xi, target in zip(X, y):
                # cost function
                pred = self.predict(xi)
                cost = np.square(target - pred)
                cost_epoch += float(cost/len(X))  # MSE
                
                # update weights and bias
                update = self.learning * (target - pred)
                self.w_ += update * xi
                self.b_ += update
            
            # store MSE through every epoch iteration
            self.costList_.append(cost_epoch)
            
            # print model improvements
            print("Epoch: {:04}\tLoss: {:06.5f}".format((ep+1), cost_epoch), end='')
            print("\t\tRegression: {:.2f}(X1) + {:.2f}(X2) + {:.2f}".format(self.w_[0],
                                                                      self.w_[1],
                                                                      float(self.b_)))
        return self<jupyter_output><empty_output><jupyter_text>EXECUTE THE MODEL<jupyter_code>clf = Perceptron()
clf.fit(X_train, y_train)<jupyter_output>Epoch: 0001	Loss: 0.58824		Regression: -0.08(X1) + 0.03(X2) + 0.15
Epoch: 0002	Loss: 0.39706		Regression: -0.02(X1) + -0.09(X2) + 0.14
Epoch: 0003	Loss: 0.32353		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0004	Loss: 0.02941		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0005	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0006	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0007	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0008	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0009	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0010	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0011	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0012	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0013	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0014	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) + 0.14
Epoch: 0015	Loss: 0.00000		Regression: 0.06(X1) + -0.15(X2) +[...]<jupyter_text>VISUALIZE MODEL IMPROVEMENT<jupyter_code>plt.plot(clf.costList_)
plt.xlabel('epochs')
plt.ylabel('MSE');
plt.show()<jupyter_output><empty_output><jupyter_text>VISUALIZE CONFUSION MATRIX<jupyter_code>import seaborn as sns
from sklearn.metrics import confusion_matrix

# this will make the plot better-looking
cmap = cmap=plt.cm.get_cmap('Reds', 10)

# create a confusion matrix for training set
print('computing confusion matrix on training sets..')
matrix_labels = ['0:setosa', '1:versicolor']
train_predictions = [clf.predict(item) for item in X_train]
mat = confusion_matrix(y_train, train_predictions)
sns.heatmap(mat.T, square=True, annot=True, fmt='d',
            cbar=True, xticklabels=matrix_labels,
            yticklabels=matrix_labels, cmap=cmap)

plt.xlabel('Train Label set')
plt.ylabel('Predictions')
plt.show()

# create a confusion matrix for testing set
print('computing confusion matrix on testing sets..')
matrix_labels = ['0:setosa', '1:versicolor']
test_predictions = [clf.predict(item) for item in X_test]
mat = confusion_matrix(y_test, test_predictions)
sns.heatmap(mat.T, square=True, annot=True, fmt='d',
            cbar=True, xticklabels=matrix_labels,
            yticklabels=matrix_labels, cmap=cmap)

plt.xlabel('Test Label set')
plt.ylabel('Predictions')
plt.show()<jupyter_output>computing confusion matrix on training sets..<jupyter_text>COMPARING EVERY SINGLE RESULT<jupyter_code>for i, point in enumerate(data):
    # Plot the samples with labels = 0
    out = clf.predict(point)
    if out==0:
        plt.scatter(point[0], point[1], s=120, marker='_', linewidths=2, color='blue')
    # Plot the samples with labels = 1
    else:
        plt.scatter(point[0], point[1], s=120, marker='+', linewidths=2, color='blue')
plt.show()

for i in range(len(y_test)):
    print('expectedValue vs prediction:\t {} | {}'.format(y_test[i], clf.predict(X_test[i])))<jupyter_output><empty_output><jupyter_text>TRY IT YOURSELF<jupyter_code>petal_length = 5.5
petal_width = 2.3

# 0: Iris-setosa | 1: Iris-versicolor
print('Iris-versicolor' if clf.predict([petal_length, petal_width]) else 'Iris-setosa')<jupyter_output>Iris-versicolor
{'fraction_non_alphanumeric': 0.13762667538411247, 'fraction_numerical': 0.06799607714939522, 'mean_word_length': 3.1739427012278307, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '141719', 'license': 'MIT', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2321, 'n_words': 512, 'path': 'perceptron-classifier.ipynb', 'repo_name': 'igerardoh/perceptron-learning-algorithm', 'size': '6118'}