实现
Python代码
import numpy as np import matplotlib matplotlib.use('TkAgg') from matplotlib import pyplot as plt # 载入数据 def load_data_set(file_name): fr = open(file_name) data_set = [] label = [] for line in fr.readlines(): line_data = line.strip().split('\t') data_set.append([float(line_data[0]), float(line_data[1])]) label.append(float(line_data[2])) data_mat = np.mat(data_set) data_mat_new = np.insert(data_mat, 2, values=1, axis=1) return data_mat_new, label # 感知机分类学习 def precep_classify(data_mat, label_mat, eta=1): omega = np.mat(np.zeros(3)) m = np.shape(data_mat)[0] error_data = True while error_data: error_data = False for i in range(m): judge = label_mat[i] * (np.dot(omega, data_mat[i].T)) if judge <= 0: error