Python全系列 教程
3567个小节阅读:5931.8k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在sklearn中LogisticRegression的默认概率阈值是0.5,可是有时候我们会根据实际需要
调整这个概率阈值
xxxxxxxxxx
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
# 自己编造一些数据
np.random.seed(666)
X = np.random.normal(0, 1, size=(200, 2)) # 样本特征
y = np.array((X[:,0]**2+X[:,1])<1.5, dtype='int') # 样本标签
for _ in range(20):
y[np.random.randint(200)] = 1 # 随机改变样本标签值
# 散点图观察
plt.scatter(X[y==0,0], X[y==0,1])
plt.scatter(X[y==1,0], X[y==1,1])
plt.show()
# 数据集拆分
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=666)
# 调整逻辑回归的概率阈值
from sklearn.linear_model import LogisticRegression
log = LogisticRegression()
log.fit(X_train,y_train) # 训练拟合
# 预测出测试集样本中的概率
predict_proba = log.predict_proba(X_test)
# 获取每个样本被预测为y=1种类的概率
predict_proba_one = predict_proba[:,1]
threashold = 0.7 # 自己设定的概率阈值
predict_result = np.array(predict_proba_one > threashold,dtype=int)
# 判断准确率
print(np.sum(predict_result == y_test) / len(y_test))
1.实现LogisticRegression调整概率阈值的说法,正确的是:
A 概率阈值是固定的,不可调整
B 在训练集上进行调整概率阈值
C 概率阈值调整后可能导致预测结果改变
D 以上均不正确
1=>C