机器学习算法(六)逻辑回归实践续

另一个是iris的案例。

from sklearn import datasets, svm, metrics, model_selection, preprocessing
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
import numpy as np

# 导入数据,并且变为2分类
iris = datasets.load_iris()
x = iris.data[iris.target !=2, :2]
y_ = iris.target[iris.target !=2]

y=[]
for i in range(len(y_)):
    y.append(y_[i]+1)
y = np.array(y)

# Add noisy features to make the problem harder
random_state = np.random.RandomState(0)
n_samples, n_features = x.shape
x = np.c_[x, random_state.randn(n_samples, 200 * n_features)]

# 归一化
# x = preprocessing.StandardScaler().fit_transform(x)

# 拆分成训练集和测试集
x_train, x_test, y_train, y_test = model_selection.train_test_split(x,y,test_size=0.1, random_state=25)


# 构造分类器
clf=LR(solver="liblinear",C=0.1,random_state=10).fit(x_train,y_train)

f1_score = metrics.f1_score(y_test, clf.predict(x_test))
print(f1_score)

predict_probs = clf.predict_proba(x_test)
y_score = predict_probs[:,1]
fpr,tpr,thresholds = metrics.roc_curve(y_test, y_score, pos_label=2)
print(fpr,tpr)
roc_auc = metrics.auc(fpr,tpr)  # 计算auc的值

# 绘制roc曲线
plt.figure()
lw = 2
plt.figure(figsize=(10,10))
plt.plot(fpr, tpr, color='darkorange',lw=lw,label='LR ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bowen2006

你的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值