官术网_书友最值得收藏!

How to do it...

  1. Import the libraries and dataset as follows:
import numpy as np 
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# We will be using the Iris Plants Database
from sklearn.datasets import load_iris

SEED = 2017
  1. First, we subset the imported data as shown here:
# The first two classes (Iris-Setosa and Iris-Versicolour) are linear separable
iris = load_iris()
idxs = np.where(iris.target<2)
X = iris.data[idxs]
y = iris.target[idxs]
  1. Let's plot the data for two of the four variables with the following code snippet:
plt.scatter(X[Y==0][:,0],X[Y==0][:,2], color='green', label='Iris-Setosa')
plt.scatter(X[Y==1][:,0],X[Y==1][:,2], color='red', label='Iris-Versicolour')
plt.title('Iris Plants Database')
plt.xlabel('sepal length in cm')
plt.ylabel('sepal width in cm')
plt.legend()
plt.show()

In the following graph, we've plotted the distribution of the two classes:

Figure 2.2: Iris plants database (two classes)
  1. To validate our results, we split the data into training and validation sets as follows:
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=SEED)
  1. Next, we initialize the weights and the bias for the perceptron:
weights = np.random.normal(size=X_train.shape[1])
bias = 1
  1. Before training, we need to define the hyperparameters:
learning_rate = 0.1
n_epochs = 15
  1. Now, we can start training our perceptron with a for loop:
del_w = np.zeros(weights.shape)
hist_loss = []
hist_accuracy = []

for i in range(n_epochs):
    # We apply a simple step function, if the output is > 0.5 we predict 1, else 0
    output = np.where((X_train.dot(weights)+bias)>0.5, 1, 0)

    # Compute MSE
    error = np.mean((y_train-output)**2)

    # Update weights and bias
    weights-= learning_rate * np.dot((output-y_train), X_train)
    bias += learning_rate * np.sum(np.dot((output-y_train), X_train))

    # Calculate MSE
    loss = np.mean((output - y_train) ** 2)
    hist_loss.append(loss)

    # Determine validation accuracy
    output_val = np.where(X_val.dot(weights)>0.5, 1, 0)
    accuracy = np.mean(np.where(y_val==output_val, 1, 0))
    hist_accuracy.append(accuracy)
  1. We've saved the training loss and validation accuracy so that we can plot them:
fig = plt.figure(figsize=(8, 4))
a = fig.add_subplot(1,2,1)
imgplot = plt.plot(hist_loss)
plt.xlabel('epochs')
a.set_title('Training loss')

a=fig.add_subplot(1,2,2)
imgplot = plt.plot(hist_accuracy)
plt.xlabel('epochs')
a.set_title('Validation Accuracy')
plt.show()

In the following screenshot, the resulting training loss and validation accuracy are shown:

Figure 2.3: Training loss and validation accuracy
主站蜘蛛池模板: 吐鲁番市| 郁南县| 玉田县| 即墨市| 满洲里市| 绥中县| 岐山县| 四会市| 汨罗市| 昌黎县| 云南省| 上蔡县| 红桥区| 柳江县| 登封市| 北票市| 甘孜县| 莱西市| 称多县| 营口市| 印江| 乌拉特前旗| 永寿县| 绵阳市| 镶黄旗| 贞丰县| 怀安县| 孟津县| 银川市| 赫章县| 克拉玛依市| 江都市| 行唐县| 贵南县| 叶城县| 漠河县| 开阳县| 崇信县| 潮州市| 永新县| 衡东县|