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. php5.6安装gd库

    rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -Uvh ht ...

  2. java中string.trim()函数的作用

    trim  /[trɪm] / 英文意思:整理,修理,修剪,整齐的 trim()的作用:去掉字符串首尾的空格. public static void main(String arg[]){ Strin ...

  3. A Practical Guide to Support Vector Classi cation

    <A Practical Guide to Support Vector Classication>是一篇libSVM使用入门教程以及一些实用技巧. 1. Basic Kernels: ( ...

  4. 去掉每行最后n个字符

    awk '{sub(/.{n}$/,"")}1' nation.tbl > nation.tbl2 把n替换成具体长度

  5. Bootstrap风格zTree树形菜单插件

    这是一款bootstrap风格jQuery zTree树形菜单插件,支持自定义编辑.添加列表菜单.删除列表等功能的jQuery树形菜单代码.在线演示 具体代码实现: <!DOCTYPE html ...

  6. springcloud12---sidecar

    Sidecar:异构平台整合.做了一个桥 package com.itmuch.cloud; import org.springframework.boot.SpringApplication; im ...

  7. 【运维技术】Nginx安装教程(yum安装,源码编译)

    安装方式 yum直接更新源安装 源码直接编译之后安装 使用yum进行直接安装 Installing a Prebuilt CentOS/RHEL Package from an OS Reposito ...

  8. Python笔记 #07# NumPy 文档地址 & Subsetting 2D Arrays

    文档地址:np.array() 1.<class 'numpy.ndarray'> ndarray 表示 n 维度(n D)数组 (= n 行数组). 2.打印 array 结构 —— n ...

  9. 20145219《网络对抗》Web安全基础实践

    20145219<网络对抗>Web安全基础实践 基础问题回答 SQL注入攻击原理,如何防御? 原理:SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL ...

  10. Duilib嵌入CEF禁止浏览器响应拖拽事件

    转载:http://blog.csdn.net/liuyan20092009/article/details/53819473 转载:https://blog.csdn.net/u012778714( ...