logistic 损失函数曲线图
时间: 2023-10-26 16:05:44 浏览: 168
Logistic Regression损失函数的曲线图是一个凸函数。通过对数似然函数的优化,可以得到全局最小值。梯度下降和拟牛顿法是常用的最优化方法之一。如果损失函数没有做归一化处理,其曲线图会呈现出一个细长的球形,需要较长的时间才能收敛。而使用Normal Equation方法则不需要进行归一化。
相关问题
使用python编程实现逻辑回归模型,在给定数据集上,绘制损失函数曲线图。使用混淆矩阵、错误率、精度、查全率、查准率、F1指标评估逻辑回归模型性能表现,请写出代码
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
# 生成样本数据
np.random.seed(0)
X_train = np.random.randn(100, 2) * 2 + 2
y_train = np.zeros(100)
X_test = np.random.randn(50, 2) * 2 + 2
y_test = np.zeros(50)
X_train = np.concatenate([X_train, np.random.randn(100, 2) * 2 - 2])
y_train = np.concatenate([y_train, np.ones(100)])
X_test = np.concatenate([X_test, np.random.randn(50, 2) * 2 - 2])
y_test = np.concatenate([y_test, np.ones(50)])
# 训练逻辑回归模型
clf = LogisticRegression()
clf.fit(X_train, y_train)
# 绘制损失函数曲线图
plt.plot(clf.loss_curve_)
plt.xlabel('Iterations')
plt.ylabel('Loss')
plt.show()
# 预测测试数据集
y_pred = clf.predict(X_test)
# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred)
# 计算错误率和精度
error_rate = 1 - accuracy_score(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
# 计算查全率、查准率和F1指标
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
# 打印结果
print('混淆矩阵:\n', cm)
print('错误率:', error_rate)
print('精度:', accuracy)
print('查全率:', recall)
print('查准率:', precision)
print('F1指标:', f1)
用python+numpy编程实现逻辑回归模型,在给定数据集上,绘制损失函数曲线图。使用混淆矩阵、错误率、精度、查全率、查准率、F1指标评估逻辑回归模型性能表现,初始权值w为0,写出代码
好的,我可以回答你的问题。以下是实现逻辑回归模型的 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))
```
阅读全文