02_逻辑回归

文章介绍了逻辑回归的基本概念、代码实现、超参数调整,以及在处理线性可分和非线性数据时的多项式逻辑回归方法。还探讨了多分类问题中的OvO和OvR策略,以及使用sklearn库进行模型训练和评估。
摘要由CSDN通过智能技术生成


线性可分数据→逻辑回归
非线性可分数据→多项式逻辑回归
多分类问题→OvO, OvR

1 逻辑回归代码实现

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression

x, y = make_classification(
    n_samples=200,
    n_features=2,
    n_redundant=0,
    n_classes=2,
    n_clusters_per_class=1,
    random_state=50
)
print(x.shape, y.shape)
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=0, stratify=y)
plt.scatter(x_train[:, 0], x_train[:, 1], c=y_train)
plt.show()

clf = LogisticRegression()
clf.fit(x_train, y_train)
print(clf.score(x_train, y_train))
print(clf.score(x_test, y_test))
y_predict = clf.predict(x_test)
print(y_predict)
print(clf.predict_proba(x_test)[:3])
print(np.argmax(clf.predict_proba(x_test), axis=1))

在这里插入图片描述
(200, 2) (200,)
0.9571428571428572
0.9666666666666667
[0 1 0 1 0 0 0 1 1 1 0 1 0 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 1 0 0 0 1 0 1
0 0 0 1 1 1 0 1 1 0 1 0 0 1 0 1 0 0 1 1 0 0 0]
[[0.9976049 0.0023951 ]
[0.00943605 0.99056395]
[0.99884752 0.00115248]]
[0 1 0 1 0 0 0 1 1 1 0 1 0 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 1 0 0 0 1 0 1
0 0 0 1 1 1 0 1 1 0 1 0 0 1 0 1 0 0 1 1 0 0 0]

2 超参数

from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV

x, y = make_classification(
    n_samples=200,
    n_features=2,
    n_redundant=0,
    n_classes=2,
    n_clusters_per_class=1,
    random_state=50
)
print(x.shape, y.shape)
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=0, stratify=y)

params = [{
    'penalty': ['l2', 'l1'],
    'C': [0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000],
    'solver': ['liblinear']
}, {
    'penalty': ['none'],
    'C': [0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000],
    'solver': ['lbfgs']
}, {
    'penalty': ['elasticnet'],
    'C': [0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000],
    'l1_ratio': [0, 0.25, 0.5, 0.75, 1],
    'solver': ['saga'],
    'max_iter': [200]
}]
grid = GridSearchCV(
    estimator=LogisticRegression(),
    param_grid=params,
    n_jobs=-1
)
grid.fit(x_train, y_train)
print(grid.best_score_)
print(grid.best_estimator_.score(x_test, y_test))
print(grid.best_params_)

0.9571428571428573
0.9666666666666667
{‘C’: 1, ‘penalty’: ‘l2’, ‘solver’: ‘liblinear’}

3 多项式逻辑回归

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import PolynomialFeatures

np.random.seed(0)
X = np.random.normal(0, 1, size=(200, 2))
y = np.array((X[:, 0] ** 2) + (X[:, 1] ** 2) < 2, dtype='int')
x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=233, stratify=y)
plt.scatter(x_train[:,0], x_train[:,1], c = y_train)
plt.show()

clf = LogisticRegression()
clf.fit(x_train, y_train)
print(clf.score(x_train, y_train))
print(clf.score(x_train, y_train))

# 采用多项式逻辑回归
print('------采用多项式逻辑回归--------')
poly = PolynomialFeatures(degree=2)
poly.fit(x_train)
x2 = poly.transform(x_train)
x2t = poly.transform(x_test)
clf.fit(x2, y_train)
print(clf.score(x2, y_train))
print(clf.score(x2t, y_test))

在这里插入图片描述
0.7071428571428572
0.7071428571428572
------采用多项式逻辑回归--------
1.0
0.9666666666666667

4 多分类

from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
from sklearn.multiclass import OneVsOneClassifier

iris = datasets.load_iris()
x = iris.data
y = iris.target
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=30)
clf = LogisticRegression()
ovr = OneVsRestClassifier(clf)
ovr.fit(x_train, y_train)
print("ovr.score:")
print(ovr.score(x_test, y_test))

ovr = OneVsOneClassifier(clf)
ovr.fit(x_train, y_train)
print("ovo.score:")
print(ovr.score(x_test, y_test))

ovr.score:
0.9473684210526315
ovo.score:
1.0

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值