机器学习模型-感知机(perceptron)

本文详细介绍感知机的基本原理,包括原始形式和对偶形式,并通过乳腺癌数据集进行代码实现,对比了自定义感知机和sklearn库的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Machine Learning  - Perceptron

一.基本原理

 

二.代码实现

import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.linear_model import Perceptron

import warnings
warnings.filterwarnings('ignore')

def loadDataset():
    dataset = datasets.load_breast_cancer()
    x = dataset.data
    y = dataset.target
    feats = dataset.feature_names
    labels = dataset.target_names
    return x,y,feats,labels

x,y,feats,labels = loadDataset()

def normalizationData(x,y):
    nx = x.copy()
    dim = x.shape[1]
    for i in range(dim):
        nx[:,i] = (x[:,i]-x[:,i].min())/(x[:,i].max()-x[:,i].min())
    ny = y.copy()
    ny[ny==0] = -1
    return nx,ny

nx,ny = normalizationData(x,y)

def divideDataset(nx,y,rate):
    samples = y.shape[0]
    trainnum = round(samples*rate)
    index = np.random.permutation(range(samples))
    x_train,x_test = nx[index[0:trainnum],:],x[index[trainnum:],:]
    y_train,y_test = y[index[0:trainnum]],y[index[trainnum:]]
    return x_train,x_test,y_train,y_test

x_train,x_test,y_train,y_test = divideDataset(nx,ny,rate=0.8)

def trainModel(x_train,y_train,maxgen,lr):
    dim = x_train.shape[1]
    w = np.zeros(dim)
    b = 0
    for L in range(maxgen):
        S = (np.dot(x_train,w)+b)*y_train
        Mx = np.where(S<=0)[0]
        if len(Mx):
            index = np.random.choice(Mx)
            w = w + lr*y_train[index]*(x_train[index,:]).T
            b = b + lr*y_train[index]
        else:
            break
    return w,b

w,b = trainModel(x_train,y_train,maxgen=200,lr=0.01)

def predictModel(x,y,w,b):
    predict = np.sign(np.dot(x,w)+b)
    acc = len(y[predict==y])/len(y)
    return predict,round(acc,4)

print('======结果比对======\n')
predict_train,train_acc = predictModel(x_train,y_train,w,b)
predict_test,test_acc = predictModel(x_test,y_test,w,b)
print('self-written (origin)==> Train acc = ',train_acc,' Test acc = ',test_acc)

三.对偶形式

def gramMat(x):
    num = x.shape[0]
    gram = []
    for i in range(num):
        for j in range(num):
            gram.append(np.dot(x[i,:],x[j,:].T))
    gram = np.array(gram).reshape(num,num)
    return gram

def dualTrainModel(x_train,y_train,maxgen,lr):
    alpha = np.zeros(y_train.shape[0])
    b = 0
    gram = gramMat(x_train)
    for L in range(maxgen):
        S = y_train*(np.dot(gram.T,alpha*y_train)+b)
        Mx = np.where(S<=0)[0]
        if len(Mx):
            index = np.random.choice(Mx)
            alpha[index] = alpha[index] + lr
            b = b + lr*y_train[index]
        else:
            break
    return alpha,b

alpha,b = dualTrainModel(x_train,y_train,maxgen=200,lr=0.01)

def dualPredictModel(x,y,alpha,b):
    predict = np.zeros(y.shape[0])
    for i in range(x.shape[0]):
        v = np.sum(alpha*y_train*np.dot(x_train,x[i].T))+b
        predict[i] = v
    predict = np.sign(predict)
    acc = len(y[predict==y])/len(y)
    return predict,round(acc,4)

predict_train,train_acc = predictModel(x_train,y_train,w,b)
predict_test,test_acc = predictModel(x_test,y_test,w,b)
print('self-written(dual) ==> Train acc = ',train_acc,' Test acc = ',test_acc) 

 model = Perceptron(alpha=0.01)
model.fit(x_train,y_train)
train_acc = model.score(x_train,y_train)
test_acc = model.score(x_test,y_test)
print('sklearn ==> Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))

### 关于机器学习感知机的概念与实现 #### 感知机的基本概念 感知机Perceptron)是神经网络的基础单元之一,属于一种二分类的线性分类模型。它由输入层和输出层组成,通过权重向量 \( w \) 和偏置项 \( b \) 来定义决策边界[^1]。感知机的核心思想在于利用梯度下降法不断调整参数,使得模型能够逐渐逼近最优解。 #### 感知机的学习过程 感知机的学习目标是最小化误分类样本的数量。其更新规则如下所示: \[ w \leftarrow w + \eta y_i x_i, \quad b \leftarrow b + \eta y_i \] 其中,\( \eta \) 表示学习率,\( y_i \) 是样本的真实标签,而 \( x_i \) 则表示对应的特征向量[^3]。这种简单的迭代机制使感知机成为理解更复杂神经网络结构的良好起点。 #### 使用Python实现感知机 以下是基于纯Python编写的简单感知机实现代码: ```python import numpy as np class Perceptron: def __init__(self, learning_rate=0.01, epochs=100): self.learning_rate = learning_rate self.epochs = epochs def fit(self, X, y): n_samples, n_features = X.shape self.weights = np.zeros(n_features) self.bias = 0 for _ in range(self.epochs): for idx, x_i in enumerate(X): linear_output = np.dot(x_i, self.weights) + self.bias predicted_class = np.where(linear_output >= 0, 1, -1) update = self.learning_rate * (y[idx] - predicted_class) self.weights += update * x_i self.bias += update def predict(self, X): linear_output = np.dot(X, self.weights) + self.bias return np.where(linear_output >= 0, 1, -1) # 示例数据 X = np.array([[2, 4], [4, 6], [-1, -3], [-3, -5]]) y = np.array([1, 1, -1, -1]) perceptron = Perceptron() perceptron.fit(X, y) print(perceptron.predict(np.array([[3, 5], [-2, -4]]))) ``` 上述代码展示了如何手动构建并训练一个基本的感知机模型。 #### 借助`scikit-learn`实现感知机 如果希望快速应用感知机解决实际问题,则可以直接调用 `scikit-learn` 中的 `MLPClassifier` 或者其他类似的工具包[^2]。例如: ```python from sklearn.neural_network import MLPClassifier from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split # 创建虚拟数据集 X, y = make_classification(n_samples=100, n_features=2, random_state=1) # 数据划分 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 初始化并训练模型 clf = MLPClassifier(hidden_layer_sizes=(1,), activation='identity', solver='sgd') clf.fit(X_train, y_train) # 输出测试结果 predictions = clf.predict(X_test) print(predictions[:10]) ``` 这段代码片段演示了如何借助 `scikit-learn` 的多层感知机模块完成二分类任务。 #### ‘头歌’平台上的教程资源 ‘头歌’是一个专注于工程教育实践教学的服务平台,在该平台上可能会提供有关机器学习基础以及感知机的具体实验案例。这些实验通常会引导学生逐步掌握从理论到实践的知识链条,并配有详细的指导文档和支持材料[^4]。不过具体的教程内容需登录相应账户查看官方发布的信息。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值