gpt4 book ai didi

python-2.7 - 感知器实现,决策边界不会绘制

转载 作者:行者123 更新时间:2023-11-30 09:48:49 25 4
gpt4 key购买 nike

我正在尝试实现一个感知器。我加载了一个 100x2 数组,其值在 0 到 100 之间。数组中的每个项目都有一个 -1 或 1 的标签。

我相信感知器正在工作,但是我无法绘制决策边界,如下所示:plot decision boundary matplotlib

当我运行代码时,我只看到单色背景。我希望看到两种颜色,数据集中的每个标签一种颜色(-1 和 1)。

My current output, I expect to see 2 colors for the background (-1 or 1)

An example of what I hope to see, from the sklearn documentation

import numpy as np
from matplotlib import pyplot as plt


def generate_data():
#generate a dataset that is linearly seperable
group_1 = np.random.randint(50, 100, size=(50,2))
group_1_labels = np.full((50,1), 1)

group_2 = np.random.randint(0, 49, size =(50,2))
group_2_labels = np.full((50,1), -1)

#add a bias value of -1
bias = np.full((50,1), -1)

#add labels, upper right quadrant are 1, lower left are -1
group_1_with_bias = np.hstack((group_1, bias))
group_2_with_bias = np.hstack((group_2, bias))

group_1_labeled = np.hstack((group_1_with_bias, group_1_labels))
group_2_labeled = np.hstack((group_2_with_bias, group_2_labels))

#merge our labeled data and shuffle!
merged_data = np.vstack((group_1_labeled, group_2_labeled))
np.random.shuffle(merged_data)

return merged_data

data = generate_data()

#load data, strip labels, add a -1 bias value
X = data[:, :3]

#create labels matrix
l = np.ravel(data[:, 3:])


def perceptron_sgd(X, l, c, epochs):
#initialize weights
w = np.zeros(3)

errors = []
for epoch in range(epochs):
total_error = 0
for i, x in enumerate(X):
if (np.dot(x, w) * l[i]) <= 0:
total_error += (np.dot(x, w) * l[i])
w = w + c * (x * l[i])

errors.append(total_error * -1)
print "epoch " + str(epoch) + ": " + str(w)
return w, errors

def classify(X, l, w):
z = np.dot(X, w)
print z

z[z <= 0] = -1
z[z > 0] = 1
#return a matrix of predicted labels
return z

w, errors = perceptron_sgd(X, l, .001, 36)

# X - some data in 2dimensional np.array
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, .2), np.arange(y_min, y_max, .2))

# here "model" is your model's prediction (classification) function
Z = classify(np.c_[xx.ravel(), yy.ravel()], l, w[:-1]) #strip the bias from weights

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
plt.axis('off')

#Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=l, cmap=plt.cm.Paired)

最佳答案

我让它工作了。

标准化您的X

from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(X[:, :-1])
X_trans = np.column_stack((scaler.transform(X[:, :-1]), X[:, -1]))

比零更好的初始化。

#initialize weights
r = np.sqrt(2)
w = np.random.uniform(-r, r, (3,))

在预测过程中添加学习偏差

z = np.dot(X, w[:-1]) + w[-1]

在预测期间也进行标准化(使用从输入中学习到的标准化)

Z = classify(scaler.transform(np.c_[xx.ravel(), yy.ravel()]), 
l, w) #strip the bias from weights

一般来说,标准化输入总是一个好主意。

Final prediction boundary

整个代码:

import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

def generate_data():
#generate a dataset that is linearly seperable
group_1 = np.random.randint(50, 100, size=(50,2))
group_1_labels = np.full((50,1), 1)

group_2 = np.random.randint(0, 49, size =(50,2))
group_2_labels = np.full((50,1), -1)

#add a bias value of -1
bias = np.full((50,1), -1)

#add labels, upper right quadrant are 1, lower left are -1
group_1_with_bias = np.hstack((group_1, bias))
group_2_with_bias = np.hstack((group_2, bias))

group_1_labeled = np.hstack((group_1_with_bias, group_1_labels))
group_2_labeled = np.hstack((group_2_with_bias, group_2_labels))

#merge our labeled data and shuffle!
merged_data = np.vstack((group_1_labeled, group_2_labeled))
np.random.shuffle(merged_data)

return merged_data

data = generate_data()

#load data, strip labels, add a -1 bias value
X = data[:, :3]

#create labels matrix
l = np.ravel(data[:, 3:])

from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(X[:, :-1])
X_trans = np.column_stack((scaler.transform(X[:, :-1]), X[:, -1]))


def perceptron_sgd(X, l, c, epochs):
#initialize weights
r = np.sqrt(2)
w = np.random.uniform(-r, r, (3,))

errors = []
for epoch in range(epochs):
total_error = 0
for i, x in enumerate(X):
if (np.dot(x, w) * l[i]) <= 0:
total_error += (np.dot(x, w) * l[i])
w = w + c * (x * l[i])

errors.append(total_error * -1)
print("epoch " + str(epoch) + ": " + str(w))
return w, errors

def classify(X, l, w):
z = np.dot(X, w[:-1]) + w[-1]
print(z)

z[z <= 0] = -1
z[z > 0] = 1
#return a matrix of predicted labels
return z

w, errors = perceptron_sgd(X_trans, l, .01, 25)

# X - some data in 2dimensional np.array
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, .1), np.arange(y_min, y_max, .1))

# here "model" is your model's prediction (classification) function
Z = classify(scaler.transform(np.c_[xx.ravel(), yy.ravel()]), l, w) #strip the bias from weights

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.4)
#plt.axis('off')

#Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=l, cmap=plt.cm.Paired)

关于python-2.7 - 感知器实现,决策边界不会绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48636281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com