174人阅读 评论(0)

# 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_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代替被设置的种子如果种子已经是任意的了，那就返回它本身。其他情况报错
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: NoneIf not None, set the lowest value of the fit to y_min.没有的话就设置适合的最小的值
y_max : optional, default: NoneIf not None, set the highest value of the fit to y_max.increasing : boolean or string, optional, default: TrueIf 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.

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时允许运行报错函数Methodsfit(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]]

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：795次
• 积分：36
• 等级：
• 排名：千里之外
• 原创：0篇
• 转载：0篇
• 译文：3篇
• 评论：0条
文章分类
文章存档