机器学习-感知机(Perceptron)-Scikit-Learn

Section I: Load package
#Section 1: Load package
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score

plt.rcParams['figure.dpi']=200
plt.rcParams['savefig.dpi']=200
font = {
   'family': 'Times New Roman',
        'weight': 'light'}
plt.rc("font", **font)
Section II: Load data and split them into train/test dataset
#Section 2: Load data and split it into train/test dataset
iris=datasets.load_iris()
X=iris.data[:,[2,3]]
y=iris.target

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=1,stratify=y)
print('Label counts in y:',np.bincount(y))
Section III: Train perceptron model
### 关于机器学习感知机的概念与实现 #### 感知机的基本概念 感知机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、付费专栏及课程。

余额充值