用python+numpy编程实现逻辑回归模型,在给定数据集上,绘制损失函数曲线图。使用混淆矩阵、错误率、精度、查全率、查准率、F1指标评估逻辑回归模型性能表现,初始权值w为0,写出代码
时间: 2024-05-07 20:24:02 浏览: 96
好的,我可以回答你的问题。以下是实现逻辑回归模型的 Python 代码示例,包括绘制损失函数曲线图和评估模型性能表现的指标:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义sigmoid函数
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# 定义损失函数
def loss(y_hat, y):
return (-y * np.log(y_hat) - (1 - y) * np.log(1 - y_hat)).mean()
# 定义逻辑回归模型和梯度下降算法
def logistic_regression(X, y, lr=0.01, num_iters=1000):
m, n = X.shape
w = np.zeros((n, 1))
b = 0
cost_list = []
for i in range(num_iters):
# 计算预测值和损失函数
y_hat = sigmoid(np.dot(X, w) + b)
J = loss(y_hat, y)
cost_list.append(J)
# 计算梯度并更新参数
dw = np.dot(X.T, (y_hat - y)) / m
db = np.mean(y_hat - y)
w -= lr * dw
b -= lr * db
return w, b, cost_list
# 生成一个样本数据集
np.random.seed(0)
m = 100
X = np.random.randn(m, 2)
w_true = np.array([[1], [2]])
b_true = 3
y_true = sigmoid(np.dot(X, w_true) + b_true) > 0.5
y_true = y_true.astype(int)
# 初始化模型参数并进行训练
w_init = np.zeros((2, 1))
b_init = 0
w_trained, b_trained, cost_list = logistic_regression(X, y_true, lr=0.1, num_iters=1000)
# 绘制损失函数曲线图
plt.plot(cost_list)
plt.xlabel('Iteration')
plt.ylabel('Cost')
plt.show()
# 评估模型性能表现
y_pred = sigmoid(np.dot(X, w_trained) + b_trained) > 0.5
y_pred = y_pred.astype(int)
TP = np.sum((y_pred == 1) & (y_true == 1))
FP = np.sum((y_pred == 1) & (y_true == 0))
TN = np.sum((y_pred == 0) & (y_true == 0))
FN = np.sum((y_pred == 0) & (y_true == 1))
accuracy = (TP + TN) / (TP + FP + TN + FN)
error_rate = (FP + FN) / (TP + FP + TN + FN)
precision = TP / (TP + FP)
recall = TP / (TP + FN)
F1 = 2 * precision * recall / (precision + recall)
print('Accuracy: {:.2f}'.format(accuracy))
print('Error rate: {:.2f}'.format(error_rate))
print('Precision: {:.2f}'.format(precision))
print('Recall: {:.2f}'.format(recall))
print('F1 score: {:.2f}'.format(F1))
```
阅读全文