首页 > 编程知识 正文

逻辑回归Python,python逻辑回归逐步回归

时间:2023-05-03 11:21:03 阅读:225889 作者:1163




import numpy as npimport matplotlib.pyplot as pltfrom sklearn.linear_model import LogisticRegressionfrom sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_split

为了可视化方便只使用两个特征

iris = load_iris()X = iris.data[:,:2]y = iris.target X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=666) #默认使用OVR方法进行多分类log_reg = LogisticRegression()log_reg.fit(X_train,y_train) log_reg.score(X_train,y_train)print(log_reg.predict(X_test)) [1 2 1 2 0 1 1 2 1 1 1 0 0 0 2 1 0 2 2 2 1 0 2 0 1 1 0 1 2 2 0 0 1 2 1 1 2 2]

绘制决策边界

def plot_decision_boundary(model, axis): x0, x1 = np.meshgrid( np.linspace(axis[0], axis[1], int((axis[1]-axis[0])*100)).reshape(-1, 1), np.linspace(axis[2], axis[3], int((axis[3]-axis[2])*100)).reshape(-1, 1), ) X_new = np.c_[x0.ravel(), x1.ravel()] y_predict = model.predict(X_new) zz = y_predict.reshape(x0.shape) from matplotlib.colors import ListedColormap custom_cmap = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF']) plt.contourf(x0, x1, zz, linewidth=5, cmap=custom_cmap) plot_decision_boundary(log_reg,[4,8.0,1.5,4.5])plt.scatter(X[y==0,0],X[y==0,1])plt.scatter(X[y==1,0],X[y==1,1])plt.scatter(X[y==2,0],X[y==2,1])

ovo #这次不用默认参数,而是用mult..(0V0),使用这种方式则需要改变计算方式log_reg2 = LogisticRegression(multi_class='multinomial',solver='newton-cg') log_reg2.fit(X_train,y_train)log_reg2.score(X_test,y_test) 0.7894736842105263 plot_decision_boundary(log_reg2,[4,8.0,1.5,4.5])plt.scatter(X[y==0,0],X[y==0,1])plt.scatter(X[y==1,0],X[y==1,1])plt.scatter(X[y==2,0],X[y==2,1])

使用全部特征(4个)X = iris.datay = iris.targetX_train,X_test,y_train,y_test = train_test_split(X,y,random_state=666)log_reg = LogisticRegression()log_reg.fit(X_train,y_train)log_reg.score(X_test,y_test)

预测结果非常精准

1.0 OVO和OVR #OvO 和 OvRfrom sklearn.multiclass import OneVsRestClassifierovr = OneVsRestClassifier(log_reg)ovr.fit(X_train,y_train)ovr.score(X_test,y_test) 0.9736842105263158 from sklearn.multiclass import OneVsOneClassifierovo = OneVsOneClassifier(log_reg)ovo.fit(X_train,y_train)ovo.score(X_test,y_test)#ovo比ovr耗时比较长,精确度高。 1.0

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。