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. Checkpoint的运行原理和源码实现

    引言 Checkpoint 到底是什么和需要用 Checkpoint 解决什么问题: Spark 在生产环境下经常会面临 Transformation 的 RDD 非常多(例如一个Job 中包含1万个 ...

  2. node必知必会之node简介

    1.什么是node.js 按照: Node.js官方网站主页 的说法: Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript ...

  3. rails 数据验证

    validates :money,      :presence => true, :numericality => {:only_integer => true}

  4. python webdriver 显示等待-自动登录126邮箱,添加联系人

    脚本内容:#encoding=utf-8#author-夏晓旭from selenium import webdriverimport timefrom selenium.webdriver.supp ...

  5. 获取WebView加载的网页内容并进行动态修改

    http://www.jianshu.com/p/3f207a8e32cb [Android]WebView读取本地图片 http://www.cnblogs.com/kimmy/p/4769788. ...

  6. mysql 触发器 trigger用法 one (简单的)

    实例~~ example1: 创建表tab1 1 2 3 4 DROP TABLE IF EXISTS tab1; CREATE TABLE tab1(     tab1_id varchar(11) ...

  7. P1052 过河(离散化+dp)

    P1052 过河 dp不难,重点是要想到离散化. 石子个数$<=100$意味着有大量空间空置,我们可以缩掉这些空间. 实现的话自己yy下就差不多了. #include<iostream&g ...

  8. js的原型继承

    <script> //动物(Animal),有头这个属性,eat方法 //名字这个属性 //猫有名字属性,继承Animal,抓老鼠方法 function Animal(name){ thi ...

  9. SDOI2011_染色

    SDOI_染色 背景:很早就想学习树链剖分,趁着最近有点自由安排的时间去学习一下,发现有个很重要的前置知识--线段树.(其实不一定是线段树,但是线段树应该是最常见的),和同学吐槽说树剖的剖和分都很死板 ...

  10. TP中上传文件图片的实现

    GoodsController.class.php控制器页面<?php namespace Admin\Controller; use Think\Controller; class Goods ...