scikit-learn 是非常优秀的一个有关机器学习的 Python Lib,包含了除深度学习之外的传统机器学习的绝大多数算法,对于了解传统机器学习是一个很不错的平台。每个算法都有相应的例子,既可以对算法有个大概的了解,而且还能熟悉这个工具包的应用,同时也能熟悉 Python 的一些技巧。

Ordinary Least Squares

我们先来看看最常见的线性模型,线性回归是机器学习里很常见的一类问题。

y(w,x)=w0+w1x1+w2x2+...+wpxp" role="presentation">y(w,x)=w0+w1x1+w2x2+...+wpxpy(w,x)=w0+w1x1+w2x2+...+wpxp

这里我们把向量 w=(w1,w2,...,wp)" role="presentation" style="position: relative;">w=(w1,w2,...,wp)w=(w1,w2,...,wp) 称为系数,把 w0" role="presentation" style="position: relative;">w0w0 称为截距。

线性回归就是为了解决如下的问题:

minw‖Xw−y‖22" role="presentation">minw∥Xw−y∥22minw‖Xw−y‖22

sklearn 可以很方便的调用线性模型去做线性回归拟合:

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score data_set = datasets.load_diabetes()
data_x = data_set.data[:, np.newaxis, 2] x_train = data_x [:-20]
x_test = data_x[-20:] y_train = data_set.target[:-20]
y_test = data_set.target[-20:] regr = linear_model.LinearRegression()
regr.fit(x_train, y_train)
y_pred = regr.predict(x_test) print('coefficients: \n', regr.coef_)
print('mean squared error: %.2f' % mean_squared_error(y_test, y_pred))
print('variance scores: %.2f' % r2_score(y_test, y_pred)) plt.scatter(x_test, y_test, color = 'black')
plt.plot(x_test, y_pred, color = 'blue', linewidth=3) plt.xticks(())
plt.yticks(()) plt.show()

Ridge Regression

上面介绍的是最常见的一种最小二乘线性拟合,这种线性拟合不带正则惩罚项,对系数没有任何约束,在高维空间中容易造成过拟合,一般来说,最小二乘拟合都会带正则项,比如下面这种:

minw‖Xw−y‖22+α‖w‖22" role="presentation">minw∥Xw−y∥22+α∥w∥22minw‖Xw−y‖22+α‖w‖22

这种带二范数的正则项,称为 ridge regression,其中 α" role="presentation" style="position: relative;">αα 控制系数摆动的幅度,α" role="presentation" style="position: relative;">αα 越大,系数越平滑,意味着系数的方差越小,系数越趋于一种线性关系。下面这个例子给出了 α" role="presentation" style="position: relative;">αα 与系数之间的关系:

import matplotlib.pyplot as plt
from sklearn import linear_model
import numpy as np X = 1. / ( np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis] ) # broadcasting
# a = np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis] y = np.ones(10) n_alphas = 100
alphas = np.logspace(-10, -2, n_alphas) coefs = [] for a in alphas:
ridge = linear_model.Ridge(alpha=a, fit_intercept=False)
ridge.fit(X, y)
coefs.append(ridge.coef_) ax = plt.gca() ax.plot(alphas, coefs)
ax.set_xscale('log')
# reverse the axis
ax.set_xlim(ax.get_xlim()[::-1]) plt.xlabel('alpha')
plt.ylabel('weights')
plt.title('Ridge coefficients as a function of the regularization')
plt.axis('title') plt.show()

scikit-learn 学习笔记-- Generalized Linear Models (一)的更多相关文章

  1. scikit-learn 学习笔记-- Generalized Linear Models (三)

    Bayesian regression 前面介绍的线性模型都是从最小二乘,均方误差的角度去建立的,从最简单的最小二乘到带正则项的 lasso,ridge 等.而 Bayesian regression ...

  2. scikit-learn 学习笔记-- Generalized Linear Models (二)

    Lasso regression 今天介绍另外一种带正则项的线性回归, ridge regression 的正则项是二范数,还有另外一种是一范数的,也就是lasso 回归,lasso 回归的正则项是系 ...

  3. Andrew Ng机器学习公开课笔记 -- Generalized Linear Models

    网易公开课,第4课 notes,http://cs229.stanford.edu/notes/cs229-notes1.pdf 前面介绍一个线性回归问题,符合高斯分布 一个分类问题,logstic回 ...

  4. 机器学习-scikit learn学习笔记

    scikit-learn官网:http://scikit-learn.org/stable/ 通常情况下,一个学习问题会包含一组学习样本数据,计算机通过对样本数据的学习,尝试对未知数据进行预测. 学习 ...

  5. [Scikit-learn] 1.1 Generalized Linear Models - from Linear Regression to L1&L2

    Introduction 一.Scikit-learning 广义线性模型 From: http://sklearn.lzjqsdd.com/modules/linear_model.html#ord ...

  6. [Scikit-learn] 1.5 Generalized Linear Models - SGD for Regression

    梯度下降 一.亲手实现“梯度下降” 以下内容其实就是<手动实现简单的梯度下降>. 神经网络的实践笔记,主要包括: Logistic分类函数 反向传播相关内容 Link: http://pe ...

  7. [Scikit-learn] 1.5 Generalized Linear Models - SGD for Classification

    NB: 因为softmax,NN看上去是分类,其实是拟合(回归),拟合最大似然. 多分类参见:[Scikit-learn] 1.1 Generalized Linear Models - Logist ...

  8. [Scikit-learn] 1.1 Generalized Linear Models - Logistic regression & Softmax

    二分类:Logistic regression 多分类:Softmax分类函数 对于损失函数,我们求其最小值, 对于似然函数,我们求其最大值. Logistic是loss function,即: 在逻 ...

  9. 广义线性模型(Generalized Linear Models)

    前面的文章已经介绍了一个回归和一个分类的例子.在逻辑回归模型中我们假设: 在分类问题中我们假设: 他们都是广义线性模型中的一个例子,在理解广义线性模型之前需要先理解指数分布族. 指数分布族(The E ...

随机推荐

  1. java反射获得泛型参数getGenericSuperclass():获取到父类泛型的类型

    public class Person<T> { } import java.lang.reflect.ParameterizedType; import java.lang.reflec ...

  2. 72. Edit Distance(编辑距离 动态规划)

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  3. 11Qt样式表

    Qt样式表 Qt样式表的思想很大程度上是来自原HTML的层叠式样式表(CSS),通过调用Qwdiget::setStyleSheet()或是Qapplication::setStyleSheet(), ...

  4. 587. Erect the Fence(凸包算法)

    问题 给定一群树的坐标点,画个围栏把所有树围起来(凸包). 至少有一棵树,输入和输出没有顺序. Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: ...

  5. kafka环境安装

    源码包下载: http://archive.apache.org/dist/kafka/1.0.0/ 集群环境: master 192.168.1.99 slave1 192.168.1.100 sl ...

  6. U盘安装window系统

    U盘安装window系统: 1. 制作系统启动U盘,推荐使用老毛桃. 2. 电脑上插入U盘,启动系统,选择U盘启动. 3. 进入老毛桃选择界面,选择生成PE系统.推荐win8,之前在一个戴尔电脑上使用 ...

  7. FFmpeg 入门(5):视频同步

    本文转自:FFmpeg 入门(5):视频同步 | www.samirchen.com 视频如何同步 在之前的教程中,我们已经可以开始播放视频了,也已经可以开始播放音频了,但是视频和音频的播放还未同步, ...

  8. # 20145106 《Java程序设计》第4周学习总结

    教材学习内容总结 翻开第六章的书,发现书中的例子居然是"假设我正在开发一款rpg游戏" public class Magician extends Role { public vo ...

  9. android的hook方面知识点

    android hook分为另种: native层hook---理解ELF文件 java层---虚拟机特性和Java上的反射的作用 注入代码: 存放在哪? 用mmap函数分配临时内存来完成代码存放,对 ...

  10. Using SQLXML Bulk Load in the .NET Environment

    http://msdn.microsoft.com/en-us/library/ms171878.aspx 1.首先创建一张表 CREATE TABLE Ord ( OrderID ,) PRIMAR ...