-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5aecf10
commit a6ab096
Showing
3 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5f743a36", | ||
"metadata": {}, | ||
"source": [ | ||
"\n", | ||
"## 感知器\n", | ||
"\n", | ||
"感知器(Perceptron)是Frank Rosenblatt在1957年就职于康奈尔航空实验室(Cornell Aeronautical Laboratory)时所发明的一种人工神经网络。感知器是一种最简单的神经网络模型。\n", | ||
"\n", | ||
"假设我们有一个输入特征向量 $x$,包含 $n$ 个特征值 $(x1, x2, …, xn)$,以及对应的权重向量 $w(w1, w2, …, wn)$ 和偏置 $b$。\n", | ||
"\n", | ||
"感知器的数学表达如下:\n", | ||
"\n", | ||
"$\n", | ||
"z = w · x + b \\\\\n", | ||
"\\hat{y} = sign(z)\n", | ||
"$\n", | ||
"\n", | ||
"\n", | ||
"其中,$z$ 代表加权输入(加权特征和偏置的总和),$sign()$是符号函数,根据$z$ 的正负返回1或-1,$\\hat{y}$ 是感知器的预测输出。\n", | ||
"\n", | ||
"感知器的训练目标是根据训练数据调整权重和偏置,使得感知器能够正确分类样本。训练过程中,我们通过与真实标签y进行比较并计算误差来更新权重和偏置。\n", | ||
"\n", | ||
"感知器的更新规则如下:\n", | ||
"\n", | ||
"$\n", | ||
"w_{new} = w_{old} + η * (y - \\hat{y}) * x \\\\\n", | ||
"b_{new} = b_{old} + η * (y - \\hat{y})\n", | ||
"$\n", | ||
"\n", | ||
"其中,$η$ 是学习率,控制每次更新的步长大小。\n", | ||
"\n", | ||
"通过反复迭代上述更新过程,感知器可以逐渐调整权重和偏置,以找到一个能够正确分类训练数据的超平面(线性决策边界)。\n", | ||
"\n", | ||
"需要注意的是,感知器算法只适用于线性可分的数据。对于线性不可分的数据集,感知器算法将无法收敛。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4eac7a6e", | ||
"metadata": {}, | ||
"source": [ | ||
"感知器可用于分类和回归。\n", | ||
"\n", | ||
"下面是用感知器对鸢尾花分类的示例:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "e84cfb5a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"准确率: 0.9666666666666667\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from sklearn.linear_model import Perceptron\n", | ||
"from sklearn.metrics import accuracy_score\n", | ||
"from sklearn.model_selection import train_test_split\n", | ||
"from sklearn.preprocessing import StandardScaler\n", | ||
"from sklearn.datasets import load_iris\n", | ||
"\n", | ||
"# 加载Iris数据集\n", | ||
"iris = load_iris()\n", | ||
"X, y = iris.data, iris.target\n", | ||
"\n", | ||
"# 数据预处理:特征缩放\n", | ||
"scaler = StandardScaler()\n", | ||
"X_scaled = scaler.fit_transform(X)\n", | ||
"\n", | ||
"# 划分训练集和测试集\n", | ||
"X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)\n", | ||
"\n", | ||
"# 创建感知器模型\n", | ||
"perceptron = Perceptron()\n", | ||
"\n", | ||
"# 在训练集上拟合模型\n", | ||
"perceptron.fit(X_train, y_train)\n", | ||
"\n", | ||
"# 在测试集上进行预测\n", | ||
"y_pred = perceptron.predict(X_test)\n", | ||
"\n", | ||
"# 计算准确率\n", | ||
"accuracy = accuracy_score(y_test, y_pred)\n", | ||
"print(\"准确率:\", accuracy)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "344ade82", | ||
"metadata": {}, | ||
"source": [ | ||
"下面手写一个感知器进行回归" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "150cd116", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"预测结果: [11.716452 13.58681458]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import numpy as np\n", | ||
"\n", | ||
"# 定义训练数据\n", | ||
"X = np.array([[1], [2], [3], [4], [5]]) # 输入特征\n", | ||
"y = np.array([2, 4, 6, 8, 10]) # 目标值\n", | ||
"\n", | ||
"# 定义感知器模型\n", | ||
"class Perceptron:\n", | ||
" def __init__(self):\n", | ||
" self.w = None # 权重\n", | ||
" self.b = None # 偏置\n", | ||
"\n", | ||
" def fit(self, X, y, epochs=10, learning_rate=0.01):\n", | ||
" n_samples, n_features = X.shape\n", | ||
" self.w = np.zeros(n_features)\n", | ||
" self.b = 0\n", | ||
"\n", | ||
" for _ in range(epochs):\n", | ||
" for i in range(n_samples):\n", | ||
" y_pred = self.predict(X[i])\n", | ||
" error = y[i] - y_pred\n", | ||
" self.w += learning_rate * error * X[i]\n", | ||
" self.b += learning_rate * error\n", | ||
"\n", | ||
" def predict(self, x):\n", | ||
" return np.dot(x, self.w) + self.b\n", | ||
"\n", | ||
"# 创建感知器对象并拟合数据\n", | ||
"perceptron = Perceptron()\n", | ||
"perceptron.fit(X, y)\n", | ||
"\n", | ||
"# 进行预测\n", | ||
"x_test = np.array([[6], [7]]) # 预测新的输入样本\n", | ||
"y_pred = perceptron.predict(x_test)\n", | ||
"print(\"预测结果:\", y_pred)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters