Bootstrap

scikit-learn实现逻辑回归

1.导入数据

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(666)
X = np.random.normal(0,1,(200,2))
y = np.array((X[:,0]**2+X[:,1])<1.5,dtype='int')

for _ in range(20):
    y[np.random.randint(200)] = 1

2.分割数据

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)

3.使用scikit-learn中的逻辑回归

from sklearn.linear_model import LogisticRegression

# 默认情况是ovo
log_reg = LogisticRegression()
log_reg.fit(X_train,y_train)
log_reg.score(X_test,y_test)
from sklearn.linear_model import LogisticRegression

# ovr
log_reg = LogisticRegression(multi_class='ovr')
log_reg.fit(X_train,y_train)
log_reg.score(X_test,y_test)
from sklearn.linear_model import LogisticRegression

# ovo
log_reg = LogisticRegression(multi_class='multinomial')
log_reg.fit(X_train,y_train)
log_reg.score(X_test,y_test)

4.OvO and OvR

from sklearn import datasets
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=666)
from sklearn.multiclass import OneVsRestClassifier

ovr = OneVsRestClassifier(log_reg)
ovr.fit(X_train,y_train)
ovr.score(X_test,y_test)
from sklearn.multiclass import OneVsOneClassifier

ovo = OneVsOneClassifier(log_reg)
ovo.fit(X_train,y_train)
ovo.score(X_test,y_test)
;