Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 感知器与算子_Python_Perceptron - Fatal编程技术网

Python 感知器与算子

Python 感知器与算子,python,perceptron,Python,Perceptron,我用这段代码实现AND运算符,但分隔区域的线并没有正确计算。它正在通过点[1,0]和[0,1]。如何正确地分隔区域 from sklearn.linear_model import Perceptron import matplotlib.pyplot as plt import numpy as np from itertools import product data = [[0,0],[1,0],[0,1],[1,1]] labels_and = [0, 0, 0, 1] x = [p

我用这段代码实现AND运算符,但分隔区域的线并没有正确计算。它正在通过点[1,0]和[0,1]。如何正确地分隔区域

from sklearn.linear_model import Perceptron
import matplotlib.pyplot as plt
import numpy as np
from itertools import product

data = [[0,0],[1,0],[0,1],[1,1]]
labels_and = [0, 0, 0, 1]

x = [points[0] for points in data]
y = [points[1] for points in data]

plt.scatter(x, y, c = labels_and)
plt.show()

classifier = Perceptron()
classifier.fit(data, labels_and)

print(classifier.score(data, labels_and))

result = classifier.decision_function([[0, 0], [1, 1], [0.5, 0.5]])
print(result)

x_values = y_values = np.linspace(0, 1, 100)
point_grid = list(product(x_values, y_values))
distances = classifier.decision_function(point_grid)
abs_distance = [abs(x) for x in distances]

distance_matrix = np.reshape(abs_distance, (100, 100))

heatmap = plt.pcolormesh(x_values, y_values, distance_matrix)
plt.colorbar(heatmap)
plt.show()

决策边界是正确的,因为所有的
都被分类为1类,而所有的
决策边界是正确的,因为所有的
都被分类为1类,所有的
决策边界都被正确计算,但我认为它是不同的。@v.tralala对该问题的回答对此进行了详细解释。同时我发现,如果
random\u state
的给定值不是零(例如
random\u state=100
),将计算不同的截距和权重值,因此决策边界将更接近点(1,1)。

决策边界计算正确,但我预计它会不同。@v.tralala对该问题的回答对此进行了详细解释。同时我发现,如果
random_state
给定的值不是零(例如
random_state=100
),则将计算不同的截距和权重值,从而使决策边界更接近点(1,1)。

感谢您的详细说明,您的回答帮助我更好地理解。你是对的,我希望得到更高的y截距线,因为我在一些例子中看到了它。我的截距是
b=-2
,当我将
random_state
更改为默认不为零的某个值时,我得到了截距和权重的不同结果。感谢您的详细说明,您的回答帮助我更好地理解。你是对的,我希望得到更高的y截距线,因为我在一些例子中看到了它。我的截距是
b=-2
,当我将
random_state
更改为默认不为零的某个值时,我得到了截距和权重的不同结果。
w = classifier.coef_[0]  
b = classifier.intercept_   
m = -weight[0]/weight[1]   
n = -b/weight[1]    
plt.plot(x_values, m*x_values+n)