错误率曲线

lg.fit = glm(BAD~.,data = x,family = binomial) #family默认为gaussian

error1=c(rep(0,100)) #总错误的概率
error2=c(rep(0,100)) #违约者被错误分类
error3=c(rep(0,100)) #没有违约者被错误分类
xx = seq(0.005,0.5,0.005)
j=1
for (i in xx){
pred = predict(lg.fit,type = ‘response’)
pred[pred>i] = 1
pred[pred<=i] = 0
s = table(pred,x$BAD)
error1[j] = (s[2]+s[3])/sum(s)
error2[j] = s[3]/(s[3]+s[4])
error3[j] = s[2]/(s[1]+s[2])
j=j+1}
plot(xx,error2,col=4,type = ‘l’)
lines(xx,error1,col =2)
lines(xx,error3,col=3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了您的要求,下面是我用ChitGPT为您回答的问题和相应的代码: 使用Python、NumPy和matplotlib构建Logistic回归分类器模型,然后在马疝病数据集上使用牛顿法估计死亡率,并绘制错误率曲线的代码如下: ```python import numpy as np import pandas as pd import matplotlib.pyplot as plt # 加载数据集 data = pd.read_csv("horseColicTraining.txt", sep='\t', header=None) X = data.iloc[:, :-1].values y = data.iloc[:, -1].values # 初始化参数 m, n = X.shape theta = np.zeros((n, 1)) iterations = 50 lr = 1 # 牛顿法实现 for i in range(iterations): h = 1 / (1 + np.exp(-np.dot(X, theta))) gradient = np.dot(X.T, (h - y)) / m hessian = np.dot(X.T, np.dot(np.diag(h * (1 - h)), X)) / m theta = theta - np.dot(np.linalg.inv(hessian), gradient) # 预测结果 test_data = pd.read_csv("horseColicTest.txt", sep='\t', header=None) X_test = test_data.iloc[:, :-1].values y_test = test_data.iloc[:, -1].values predictions = np.round(1 / (1 + np.exp(-np.dot(X_test, theta)))) # 计算错误率曲线 error_rates = [] for i in range(1, m): predictions = np.round(1 / (1 + np.exp(-np.dot(X[:i], theta)))) error_rates.append(np.sum(predictions != y[:i]) / i) # 绘制错误率曲线 plt.plot(list(range(1, m)), error_rates) plt.xlabel("Training set size") plt.ylabel("Error rate") plt.title("Error rate curve for logistic regression") plt.show() ``` 希望这个代码能够满足您的需求,如果还有不明白的地方或者其他问题,请随时告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值