import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Perceptron
import warnings
warnings.filterwarnings('ignore')
#读取数据
data = pd.read_csv('data.csv',encoding='utf8',header=None)
arr = data.values
#预处理
encoder = preprocessing.LabelEncoder()
labels = encoder.fit_transform(arr[:,1])
for label,num in enumerate(encoder.classes_):
print(label,'-->',num)
arr[:,1] = labels
#感知机(原始形式)
x,y = arr[:,0:-1],arr[:,-1]
m = x.shape[1]
w = np.zeros(m)
b = 0
lr = 0.1
maxgen = 100
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1)
for i in range(maxgen):
f = (np.dot(x_train,w)+b)*y_train
mistag = np.where(f<=0)[0]
train_acc = round((len(y_train)-len(mistag))/len(y_train),2)
if len(mistag):
index = np.random.permutation(mistag)[0]
w = w+lr*y_train[index]*(x_train[index,:].T)
b = b+lr*y_train[index]
else:
print('模型训练结束')
break
predict_test = np.sign(np.dot(x_test,w)+b)
mistag = np.where(predict_test*y_test<=0)[0]
test_acc = round((len(y_test)-len(mistag))/len(y_test),2)
print('Train acc = ',train_acc,' Test acc = ',test_acc)
#感知机(对偶形式)
n = x_train.shape[0]
a = np.zeros(n)
b = 0
gram = np.zeros((n,n))
for i in range(n):
for j in range(n):
gram[i,j] = np.dot(x_train[i],x_train[j].T)
for k in range(maxgen):
f = np.dot(gram,a*y_train)+b
mistag = np.where(f<=0)[0]
train_acc = round((len(y_train)-len(mistag))/len(y_train),2)
if len(mistag):
index = np.random.permutation(mistag)[0]
a[index] = a[index] + lr
b = b + lr*y_train[index]
else:
print('模型训练结束')
break
W = np.dot(x_train.T,a*y_train)
B = np.dot(a.T,y_train)
predict_test = np.sign(np.dot(x_test,W)+B)
mistag = np.where(predict_test*y_test<=0)[0]
test_acc = round((len(y_test)-len(mistag))/len(y_test),2)
print('Train acc = ',train_acc,' Test acc = ',test_acc)
clf = Perceptron(max_iter=100,eta0=0.1,random_state=1)
clf.fit(x_train.astype(int),y_train.astype(int))
predict_train = clf.predict(x_train)
print('参数w:',clf.coef_[0])
print('参数b',clf.intercept_[0])
print('Train acc:',round(len(y_train[y_train==predict_train])/len(y_train),2))
predict_test = clf.predict(x_test)
print('Train acc:',round(len(y_test[y_test==predict_test])/len(y_test),2))
数据: