Ejemplo n.º 1
0
def perceptron_example():
    df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)

    y = df.iloc[0:100, 4].values
    y = np.where(y == 'Iris-setosa', -1, 1)

    x = df.iloc[0:100, [0, 2]].values
    xlabel_text = "Index 0"
    ylabel_text = "Index 2"
    zlabel_text = "Index 3"

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(x[:50, 0], x[:50, 1], color='red', marker='o', label='setosa')
    ax.scatter(x[50:100, 0], x[50:100, 1], color='blue', marker='x', label='versicolor')
    ax.set_xlabel(xlabel_text)
    ax.set_ylabel(ylabel_text)
    ax.legend()

    perceptron = Perceptron(.1, 100)
    perceptron.fit(x, y)
    # print(perceptron.get_weights())
    plt.figure()
    perceptron.plot_decision_regions(x, y, perceptron)
    plt.xlabel(xlabel_text)
    plt.ylabel(ylabel_text)
    plt.show()
Ejemplo n.º 2
0
pn = Perceptron(0.1, 10)

#Using Pandas import Iris dataset
df = pd.read_csv(
    'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
    header=None)

#Only use initial 100 data value labels
y = df.iloc[0:100, 4].values

#Convert labels into -1 and 1
y = np.where(y == 'Iris-setosa', -1, 1)

#Extract only 2 parameters from data set
X = df.iloc[0:100, [0, 2]].values

#Use fit, error, predict, weights, net_input functions from perceptron class
pn.fit(X, y)
print("Errors : \n", pn.error)
print("Prediction : \n", pn.predict(X))
print("Weights : \n", pn.weights)
#print(pn.net_input(X))

#Plot result
pn.plot_decision_regions(X, y, classifier=pn, resolution=0.02)

#Plot Error function
plt.plot(range(1, len(pn.error) + 1), pn.error, marker='o')
plt.xlabel('Iteration')
plt.ylabel('# of misclassifications')
plt.show()