• sklearn 线性模型使用入门


    LinearRegression fits a linear model with coefficients w = (w_1, ..., w_p) to minimize the residual sum of squares between the observed responses in the dataset, and the responses predicted by the linear approximation. Mathematically it solves a problem of the form:

    原理最小化:   underset{w}{min\,} {|| X w - y||_2}^2

     
    >>> from sklearn import linear_model
    >>> clf = linear_model.LinearRegression()
    >>> clf.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
    LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
    >>> clf.coef_
    array([ 0.5,  0.5])

    完整代理例子

    #!/usr/bin/env python
    # coding=utf-8
    
    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn import datasets,linear_model
    
    print(__doc__)
    
    # load dataset 
    diabetes = datasets.load_diabetes()
    
    # use only one feature
    diabetes_x = diabetes.data[:,np.newaxis]
    diabetes_x_temp = diabetes_x[:,:,2]
    
    # split data into training/testing sets
    diabetes_x_train = diabetes_x_temp[:-20]
    diabetes_x_test = diabetes_x_temp[-20:]
    
    # split the targets into training/testing sets
    diabetes_y_train = diabetes.target[:-20]
    diabetes_y_test = diabetes.target[-20:]
    
    # create linear regression object
    regr = linear_model.LinearRegression()
    regr.fit(diabetes_x_train,diabetes_y_train)
    
    # the coefficients
    print('coefficients: 
     ',regr.coef_)
    
    # the mean square error
    print("Residual sum of squares:%.2f" % np.mean((regr.predict(diabetes_x_test)-diabetes_y_test)**2))
    
    # Plot outputs
    plt.scatter(diabetes_x_test,diabetes_y_test,color='black')
    plt.plot(diabetes_x_test,regr.predict(diabetes_x_test),color='blue',linewidth=3)
    
    plt.title("linear_model example")
    plt.xlabel("X")
    plt.ylabel("Y")
    # plt.xticks(())
    # plt.yticks(())
    
    plt.show()
    View Code

    转自:

    http://scikit-learn.org/dev/auto_examples/linear_model/plot_ols.html#example-linear-model-plot-ols-py

    每天一小步,人生一大步!Good luck~
  • 相关阅读:
    前端面试1
    关于JavaScript学习,推荐博客及书籍
    GET 和 POST 两种方式来完成Http接口
    mvc Web api 如何在控制器中调用
    c#怎么获取当前页面的url
    MVC3缓存:使用页面缓存
    十大排序算法梳理
    浅谈设计模式——工厂模式
    Java 中的 反射机制
    浅谈设计模式——单例模式
  • 原文地址:https://www.cnblogs.com/jkmiao/p/4455399.html
Copyright © 2020-2023  润新知