From 505f8176ff23eba83b8863ee67d24c11a0922df5 Mon Sep 17 00:00:00 2001 From: jianf <2198972886@qq.com> Date: Thu, 2 Apr 2026 02:21:12 +0000 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...oftmax 回归(Softmax Regression)与决策边界.py | 29 +++++++++++ 感知器(Perceptron)模型与决策边界.py | 50 +++++++++++++++++++ 支持向量机(SVM)与决策边界.py | 30 +++++++++++ 线性回归1.py | 34 +++++++++++++ 逻辑回归(Logistic Regression)与决策边界.py | 30 +++++++++++ 5 files changed, 173 insertions(+) create mode 100644 多分类的 Softmax 回归(Softmax Regression)与决策边界.py create mode 100644 感知器(Perceptron)模型与决策边界.py create mode 100644 支持向量机(SVM)与决策边界.py create mode 100644 线性回归1.py create mode 100644 逻辑回归(Logistic Regression)与决策边界.py diff --git a/多分类的 Softmax 回归(Softmax Regression)与决策边界.py b/多分类的 Softmax 回归(Softmax Regression)与决策边界.py new file mode 100644 index 0000000..8030fec --- /dev/null +++ b/多分类的 Softmax 回归(Softmax Regression)与决策边界.py @@ -0,0 +1,29 @@ +from sklearn.linear_model import LogisticRegression +import numpy as np +import matplotlib.pyplot as plt + +# 生成简单的多分类数据集 +X = np.array([[1, 2], [2, 3], [3, 4], [6, 7], [7, 8], [8, 9], [1, 3], [2, 4], [3, 5]]) +y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]) # 三个类别:0, 1, 2 + +# 训练 Softmax 回归模型(多分类) +model = LogisticRegression(multi_class='multinomial', solver='lbfgs') +model.fit(X, y) + +# 绘制数据点和决策边界 +x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 +x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 +xx, yy = np.meshgrid(np.linspace(x1_min, x1_max, 100), + np.linspace(x2_min, x2_max, 100)) +Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) +Z = Z.reshape(xx.shape) + +# 可视化数据点和决策边界 +plt.contourf(xx, yy, Z, alpha=0.4) +plt.scatter(X[:, 0], X[:, 1], c=y, marker='o', edgecolors='k', cmap=plt.cm.Paired) +plt.title("Softmax Regression (Multinomial) - Decision Boundary") +plt.show() +""" +使用 LogisticRegression(multi_class='multinomial') 来实现多分类的 Softmax 回归。 +通过训练后的模型,绘制出决策边界,并显示不同类别的数据点。 +""" \ No newline at end of file diff --git a/感知器(Perceptron)模型与决策边界.py b/感知器(Perceptron)模型与决策边界.py new file mode 100644 index 0000000..73fcb3f --- /dev/null +++ b/感知器(Perceptron)模型与决策边界.py @@ -0,0 +1,50 @@ +import numpy as np +import matplotlib.pyplot as plt + +# 生成简单的二分类数据集 +X = np.array([[2, 3], [3, 3], [4, 4], [1, 2], [2, 1], [3, 1]]) +y = np.array([1, 1, 1, -1, -1, -1]) # 标签:1表示正类,-1表示负类 + +# 感知器模型 +class Perceptron: + def __init__(self, lr=0.1, n_iter=1000): + self.lr = lr # 学习率 + self.n_iter = n_iter # 迭代次数 + self.weights = None + self.bias = 0 + + def fit(self, X, y): + n_samples, n_features = X.shape + self.weights = np.zeros(n_features) + + for _ in range(self.n_iter): + for i in range(n_samples): + if y[i] * (np.dot(X[i], self.weights) + self.bias) <= 0: + self.weights += self.lr * y[i] * X[i] + self.bias += self.lr * y[i] + + def predict(self, X): + return np.sign(np.dot(X, self.weights) + self.bias) + +# 训练感知器模型 +model = Perceptron() +model.fit(X, y) + +# 绘制数据点和决策边界 +x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 +x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 +xx, yy = np.meshgrid(np.linspace(x1_min, x1_max, 100), + np.linspace(x2_min, x2_max, 100)) +Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) +Z = Z.reshape(xx.shape) + +# 可视化数据点和决策边界 +plt.contourf(xx, yy, Z, alpha=0.4) +plt.scatter(X[:, 0], X[:, 1], c=y, marker='o', edgecolors='k', cmap=plt.cm.Paired) +plt.title("Perceptron Model - Decision Boundary") +plt.show() +""" +使用简单的二维数据集来训练感知器模型。 +fit 方法实现了感知器的训练过程,更新权重和偏置。 +然后,使用 predict 方法对数据点进行分类,并使用 Matplotlib 可视化其决策边界。 +""" \ No newline at end of file diff --git a/支持向量机(SVM)与决策边界.py b/支持向量机(SVM)与决策边界.py new file mode 100644 index 0000000..18a2a30 --- /dev/null +++ b/支持向量机(SVM)与决策边界.py @@ -0,0 +1,30 @@ +from sklearn.svm import SVC +import numpy as np +import matplotlib.pyplot as plt + +# 生成简单的二分类数据集 +X = np.array([[2, 3], [3, 3], [4, 4], [1, 2], [2, 1], [3, 1]]) +y = np.array([1, 1, 1, -1, -1, -1]) # 标签:1表示正类,-1表示负类 + +# 训练 SVM 模型 +model = SVC(kernel='linear') +model.fit(X, y) + +# 绘制数据点和决策边界 +x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 +x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 +xx, yy = np.meshgrid(np.linspace(x1_min, x1_max, 100), + np.linspace(x2_min, x2_max, 100)) +Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) +Z = Z.reshape(xx.shape) + +# 可视化数据点和决策边界 +plt.contourf(xx, yy, Z, alpha=0.4) +plt.scatter(X[:, 0], X[:, 1], c=y, marker='o', edgecolors='k', cmap=plt.cm.Paired) +plt.title("SVM (Linear Kernel) - Decision Boundary") +plt.show() +""" +通过 SVC(kernel='linear') 训练一个线性支持向量机模型。 +使用 model.fit() 来训练,model.predict() 用于计算决策边界。 +同样,使用 Matplotlib 可视化决策边界。 +""" \ No newline at end of file diff --git a/线性回归1.py b/线性回归1.py new file mode 100644 index 0000000..312deb1 --- /dev/null +++ b/线性回归1.py @@ -0,0 +1,34 @@ +import numpy as np +import matplotlib.pyplot as plt + +# 生成样本数据 +np.random.seed(42) +X = 2 * np.random.rand(100, 1) +y = 4 + 3 * X + np.random.randn(100, 1) + +# 归一化处理 +X_b = np.c_[np.ones((X.shape[0], 1)), X] # 添加x0 = 1 + +# 初始化参数 +theta = np.random.randn(2, 1) +learning_rate = 0.1 +n_iterations = 1000 +m = len(X) + +# 梯度下降 +for iteration in range(n_iterations): + gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y) + theta -= learning_rate * gradients + +# 输出训练结果 +print(f"权重:{theta[1][0]}") +print(f"偏置:{theta[0][0]}") + +# 绘制数据点与预测结果 +plt.scatter(X, y, color='blue', label='实际数据') +plt.plot(X, X_b.dot(theta), color='red', linewidth=2, label='预测结果') +plt.xlabel("特征 X") +plt.ylabel("目标变量 y") +plt.title("线性回归 - 预测结果与实际数据对比(梯度下降)") +plt.legend() +plt.show() diff --git a/逻辑回归(Logistic Regression)与决策边界.py b/逻辑回归(Logistic Regression)与决策边界.py new file mode 100644 index 0000000..bf8e050 --- /dev/null +++ b/逻辑回归(Logistic Regression)与决策边界.py @@ -0,0 +1,30 @@ +from sklearn.linear_model import LogisticRegression +import numpy as np +import matplotlib.pyplot as plt + +# 生成简单的二分类数据集 +X = np.array([[2, 3], [3, 3], [4, 4], [1, 2], [2, 1], [3, 1]]) +y = np.array([1, 1, 1, 0, 0, 0]) # 标签:0表示负类,1表示正类 + +# 训练逻辑回归模型 +model = LogisticRegression() +model.fit(X, y) + +# 绘制数据点和决策边界 +x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 +x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 +xx, yy = np.meshgrid(np.linspace(x1_min, x1_max, 100), + np.linspace(x2_min, x2_max, 100)) +Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) +Z = Z.reshape(xx.shape) + +# 可视化数据点和决策边界 +plt.contourf(xx, yy, Z, alpha=0.4) +plt.scatter(X[:, 0], X[:, 1], c=y, marker='o', edgecolors='k', cmap=plt.cm.Paired) +plt.title("Logistic Regression - Decision Boundary") +plt.show() +""" +使用 LogisticRegression 模型在简单的二分类数据上训练。 +model.fit() 训练模型,model.predict() 得到决策边界。 +使用 Matplotlib 绘制出决策边界以及数据点。 +""" \ No newline at end of file