zoukankan      html  css  js  c++  java
  • 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~
  • 相关阅读:
    iOS-字符串的连接
    [Win32]Win32 SDK编程系列文章——键盘输入消息
    [置顶] eclipse导入svn下载的项目后无法与服务器的svn项目关联
    iOS-时区 日期处理
    数学之路(3)数据分析(5)
    Filter解决中文乱码问题
    Mac OS X 10.8.3搭建Android工程源码的编译环境(解决找不到GCC、GIT、PYTHON的问题)
    paypal租用
    Java通过内部类实现回调功能
    处理9path图片边缘的小黑点
  • 原文地址:https://www.cnblogs.com/jkmiao/p/4455399.html
Copyright © 2011-2022 走看看