Isotonic Regression

http://scikit-learn.org/stable/auto_examples/plot_isotonic_regression.html

----------------------------------------------------------------------------------------------------------------------

scikit 例子中的源代码

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection #加载画图所需要的函数

from sklearn.linear_model import LinearRegression#加载线性拟合函数
from sklearn.isotonic import IsotonicRegression #加载保序拟合函数
from sklearn.utils import check_random_state #加载随机生成函数

n = 100
x = np.arange(n)#生成一个0到99的一维矩阵
rs = check_random_state(0)#设置随机种子
y = rs.randint(-50, 50, size=(n,)) + 50. * np.log(1 + np.arange(n))#生成一个随机数加上对数函数的一维矩阵

###############################################################################
# Fit IsotonicRegression and LinearRegression models

ir = IsotonicRegression()#生成保序函数的对象

y_ = ir.fit_transform(x, y)#生成保序的y

lr = LinearRegression()#生成线性拟合对象
lr.fit(x[:, np.newaxis], y)  # x needs to be 2d for LinearRegression x需要二维的，生成拟合成功的模型

###############################################################################
# plot result

segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)] #画连线时使用
lc = LineCollection(segments, zorder=0)
lc.set_array(np.ones(len(y)))
lc.set_linewidths(0.5 * np.ones(n))

fig = plt.figure()
plt.plot(x, y, 'r.', markersize=12)
plt.plot(x, y_, 'g.-', markersize=12)
plt.plot(x, lr.predict(x[:, np.newaxis]), 'b-')#根据x和线性拟合拟合出的模型画出图
plt.legend(('Data', 'Isotonic Fit', 'Linear Fit'), loc='lower right')#标签
plt.title('Isotonic regression')
plt.show()
----------------------------------------------------------------------------------------------
check_random_stateseed
http://scikit-learn.org/stable/modules/generated/sklearn.utils.check_random_state.html#sklearn.utils.check_random_state


Turn seed into a np.random.RandomState instance

If seed is None, return the RandomState singleton used by np.random.If seed is an int, return a new RandomState

instance seeded with seed.If seed is already a RandomState instance, return it.Otherwise raise ValueError.

IsotonicRegression(y_min=None, y_max=None, increasing=True, out_of_bounds='nan') http://scikit-learn.org/stable/modules/generated/sklearn.isotonic.IsotonicRegression.html#sklearn.isotonic.IsotonicRegression

y_min : optional, default: None

If not None, set the lowest value of the fit to y_min.

y_max : optional, default: None

If not None, set the highest value of the fit to y_max.

increasing : boolean or string, optional, default: True

If boolean, whether or not to fit the isotonic regression with y increasing or decreasing.

The string value “auto” determines whether y shouldincrease or decrease based on the Spearman correlation estimate’ssign.

out_of_bounds : string, optional, default: “nan”

The out_of_bounds parameter handles how x-values outside of the training domain are handled. When set to “nan”, predicted y-valueswill be NaN.

When set to “clip”, predicted y-values will be set to the value corresponding to the nearest train interval endpoint.When set to “raise”,

allow interp1d to throw ValueError.

Methods

 fit(X, y[, sample_weight]) Fit the model using X, y as training data.适合的x和y作为训练数据 fit_transform(X[, y]) Fit to data, then transform it. get_params([deep]) Get parameters for this estimator. predict(T) Predict new data by linear interpolation. score(X, y[, sample_weight]) Returns the coefficient of determination R^2 of the prediction. set_params(**params) Set the parameters of this estimator. transform(T) Transform new data by linear interpolation

LinearRegression(fit_intercept=True,normalize=False, copy_X=True, n_jobs=1)

http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression

fit_intercept : boolean, optional

whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations

(e.g. data is expected to be already centered).

normalize : boolean, optional, default False

If True, the regressors X will be normalized before regression.

copy_X : boolean, optional, default True

If True, X will be copied; else, it may be overwritten.

n_jobs : int, optional, default 1

The number of jobs to use for the computation.If -1 all CPUs are used. This will only provide speedup forn_targets > 1

and sufficient large problems.

Methods

 decision_function(*args, **kwargs) DEPRECATED: and will be removed in 0.19. fit(X, y[, sample_weight]) Fit linear model. get_params([deep]) Get parameters for this estimator. predict(X) Predict using the linear model score(X, y[, sample_weight]) Returns the coefficient of determination R^2 of the prediction. set_params(**params) Set the parameters of this estimator.

np.newaxis就是在原来的矩阵基础上多加一维度，因为[.......]是纯一位要变成[[],[],[],[],[],[]]形式不变但是就是多了一维度

LineColloection()是画图用的函数，就是在同x的两个y间加一条线，输入矩阵格式为[[i,y1i],[i,y2i]]

Spark中组件Mllib的学习41之保序回归（Isotonic regression）

2016-05-25 16:59:59

103 保序回归 isotonic regression

2016-03-30 11:25:27

spark.mllib源码阅读-回归算法2-IsotonicRegression

2017-03-24 13:22:53

Spark Mllib　回归学习笔记二（java）：保序回归

2016-10-01 21:51:28

Isotonic regression

2014-08-08 15:07:04

scikit-learn： isotonic regression（保序回归，很有意思，仅做知识点了解，但几乎没用到过）

2015-08-16 23:22:03

SparkML之回归(三)保序回归

2017-11-20 15:22:30

Spark机器学习之分类与回归

2017-04-07 21:21:32

机器学习十大算法--回归算法（批量梯度下降）

2017-10-27 17:17:06

Spark MLlib 1.6 -- 分类和回归篇

2016-02-19 21:42:19

不良信息举报

scikit学习心得——Isotonic Regression