python pyplot accuracy cost曲线绘制

本文主要内容是使用python matplotlib绘制accuracy, cost曲线。在使用机器学习算法训练时往往需要输出训练的accuracy以及cost,但是最直观的方法还是绘制对应的曲线(根据训练的迭代期n),本文给出简要的绘制方法。

代码如下,也可见stackoverflow


import numpy as np
import matplotlib.pyplot as plt


def draw_result(lst_iter, lst_loss, lst_acc, title):
    plt.plot(lst_iter, lst_loss, 
好的,我可以回答你的问题。以下是实现逻辑回归模型的 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)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值