• 吴裕雄 python 机器学习——逻辑回归


    import numpy as np
    import matplotlib.pyplot as plt
    
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    from sklearn import datasets, linear_model
    from sklearn.model_selection import train_test_split
    
    def load_data():
        # 使用 scikit-learn 自带的 iris 数据集
        iris=datasets.load_iris() 
        X_train=iris.data
        y_train=iris.target
        return train_test_split(X_train, y_train,test_size=0.25,random_state=0,stratify=y_train)
    
    #逻辑回归
    def test_LogisticRegression(*data):
        X_train,X_test,y_train,y_test=data
        regr = linear_model.LogisticRegression()
        regr.fit(X_train, y_train)
        print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_))
        print('Score: %.2f' % regr.score(X_test, y_test))
        
    # 加载用于分类的数据集
    X_train,X_test,y_train,y_test=load_data()
    # 调用  test_LogisticRegression
    test_LogisticRegression(X_train,X_test,y_train,y_test)
        
    def test_LogisticRegression_multinomial(*data):
        '''
        测试 LogisticRegression 的预测性能随 multi_class 参数的影响
        '''
        X_train,X_test,y_train,y_test=data
        regr = linear_model.LogisticRegression(multi_class='multinomial',solver='lbfgs')
        regr.fit(X_train, y_train)
        print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_))
        print('Score: %.2f' % regr.score(X_test, y_test))
        
    # 调用  test_LogisticRegression_multinomial
    test_LogisticRegression_multinomial(X_train,X_test,y_train,y_test)
        
    def test_LogisticRegression_C(*data):
        '''
        测试 LogisticRegression 的预测性能随  C  参数的影响
        '''
        X_train,X_test,y_train,y_test=data
        Cs=np.logspace(-2,4,num=100)
        scores=[]
        for C in Cs:
            regr = linear_model.LogisticRegression(C=C)
            regr.fit(X_train, y_train)
            scores.append(regr.score(X_test, y_test))
        ## 绘图
        fig=plt.figure()
        ax=fig.add_subplot(1,1,1)
        ax.plot(Cs,scores)
        ax.set_xlabel(r"C")
        ax.set_ylabel(r"score")
        ax.set_xscale('log')
        ax.set_title("LogisticRegression")
        plt.show()
        
    # 调用  test_LogisticRegression_C
    test_LogisticRegression_C(X_train,X_test,y_train,y_test)

  • 相关阅读:
    机器学习三--分类--邻近取样(Nearest Neighbor)
    机器学习二——分类算法--决策树DecisionTree
    机器学习一--基本概念
    python学习--常用正则表达式整理
    python学习--字符编码问题
    python学习--字符串处理相关方法
    python学习--如何在一个for循环中迭代多个可迭代对象(串行,并行)
    python学习--如何对迭代器进行切片操作
    List对象去重
    List 简单升降序实现
  • 原文地址:https://www.cnblogs.com/tszr/p/10790241.html
Copyright © 2020-2023  润新知