zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 人工智能机器学习实战代码:逻辑回归

    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)

  • 相关阅读:
    bootstrap-table对前台页面表格的支持
    解决拦截器对ajax请求的的拦截
    jQuery获取鼠标事件源(万能)
    HtmlAgilityPack 处理通配的contains
    【转】XPath的学习
    在IIS服务器上部署svg/woff/woff2字体
    【转】万网域名查询接口(API)的说明
    html5和css3的常用参考网
    【转】Xml序列化
    C#序列化s实体类成Xml,去除空格、换行符以及命名空间
  • 原文地址:https://www.cnblogs.com/tszr/p/11177895.html
Copyright © 2011-2022 走看看