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.gca().add_collection(lc)#增加连线 plt.legend(('Data', 'Isotonic Fit', 'Linear Fit'), loc='lower right')#标签 plt.title('Isotonic regression') plt.show() ---------------------------------------------------------------------------------------------- check_random_state(seed) 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
-
代替random.randomstate
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.
如果随机种子没有,返回被np.random使用的randomstate,如果种子的类型是整形,返回一个新的randomstate代替被设置的种子
- 如果种子已经是任意的了,那就返回它本身。其他情况报错
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.
如果为0-1变量,代表是增拟合还是降拟合
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.
这个参数控制如何操作超出训练范围的值,当为nan时,y也是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.
当为clip时,y将被设为与相邻末点一样的值,当为raise时允许运行报错函数
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
是否计算截距,如果为flase就不计算
(e.g. data is expected to be already centered).
normalize : boolean, optional, default False
If True, the regressors X will be normalized before regression.
如果为true,x将先被标准化
copy_X : boolean, optional, default True
If True, X will be copied; else, it may be overwritten.
如果为true,x将被
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]]