请给我具体完整的代码
时间: 2024-12-06 11:22:22 浏览: 5
淘淘商城源代码 淘淘商城源代码
5星 · 资源好评率100%
为了提供完整且详细的代码,我会展示如何使用Python和sklearn库实现Logistic回归算法的全部流程,包括数据生成、模型训练、预测以及结果可视化。请注意,由于绘图和损失函数历史记录的细节会因库的不同而略有差异,以下代码示例主要关注核心算法和关键部分。
首先,我们需要导入必要的库:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import make_blobs
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
```
接下来,我们定义数据生成和处理函数:
```python
def generate_two_gaussians(n_samples, n_features, means, covariances):
X = []
y = []
for i in range(len(means)):
X.append(np.random.multivariate_normal(means[i], covariances[i], int(n_samples / len(means))))
y.extend([i] * int(n_samples / len(means)))
X = np.concatenate(X)
y = np.array(y).reshape(-1, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
return X_train, X_test, y_train, y_test
# 参数设置
n_samples = 400
n_features = 2
means = [[-5, 0], [0, 5]]
covariances = [np.eye(n_features)] * 2
```
接着,创建并训练模型:
```python
def train_and_evaluate_logistic_regression(X_train, y_train):
lr = LogisticRegression(max_iter=1000)
lr.fit(X_train, y_train)
# 训练集上的分类准确率
y_pred_train = lr.predict(X_train)
acc_train = accuracy_score(y_train, y_pred_train)
# 测试集上的分类
y_pred_test = lr.predict(X_test)
acc_test = accuracy_score(y_test, y_pred_test)
print(f"Training accuracy: {acc_train}")
print(f"Testing accuracy: {acc_test}")
return lr
# 运行
X_train, X_test, y_train, y_test = generate_two_gaussians(n_samples, n_features, means, covariances)
lr_model = train_and_evaluate_logistic_regression(X_train, y_train)
```
最后,对测试集进行预测并绘制决策边界:
```python
def plot_decision_boundary(X, y, model):
# 预测所有测试点的概率
probas = model.predict_proba(X)
# 可视化数据
plt.figure(figsize=(8, 6))
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis', edgecolor='black')
# 添加决策边界
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.01))
grid = np.c_[xx.ravel(), yy.ravel()]
z = model.predict(grid)
z = z.reshape(xx.shape)
plt.contourf(xx, yy, z, cmap='viridis', alpha=0.3)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.title("Decision Boundary")
plot_decision_boundary(X_test, y_test, lr_model)
```
至于损失函数的变化,`sklearn`库并没有直接提供损失函数的图形化显示,你可以选择自定义记录损失并在训练过程中保存,例如使用`lr.loss_`。如果要查看损失函数的变化,你可能需要在训练过程中保存每轮迭代后的`loss_`值,然后用`plt.plot()`来画出。
阅读全文