本文整理汇总了Python中sklearn.linear_model.RANSACRegressor方法的典型用法代码示例。如果您正苦于以下问题:Python linear_model.RANSACRegressor方法的具体用法?Python linear_model.RANSACRegressor怎么用?Python linear_model.RANSACRegressor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model
的用法示例。
在下文中一共展示了linear_model.RANSACRegressor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ensure_many_models
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def ensure_many_models(self):
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.linear_model import ElasticNet, RANSACRegressor, HuberRegressor, PassiveAggressiveRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR, LinearSVR
import warnings
from sklearn.exceptions import ConvergenceWarning
warnings.filterwarnings('ignore', category=ConvergenceWarning)
for learner in [GradientBoostingRegressor, RandomForestRegressor, MLPRegressor,
ElasticNet, RANSACRegressor, HuberRegressor, PassiveAggressiveRegressor,
KNeighborsRegressor, SVR, LinearSVR]:
learner = learner()
learner_name = str(learner).split("(", maxsplit=1)[0]
with self.subTest("Test fit using {learner}".format(learner=learner_name)):
model = self.estimator.__class__(learner)
model.fit(self.data_lin["X"], self.data_lin["a"], self.data_lin["y"])
self.assertTrue(True) # Fit did not crash
示例2: test_ransac_custom_base_estimator
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_ransac_custom_base_estimator():
base_estimator = DecisionTreeRegressor()
estimator = linear_model.RANSACRegressor(
base_estimator=base_estimator,
random_state=1)
estimator.fit([[1], [2], [3]], [1, 2, 3])
assembler = assemblers.RANSACModelAssembler(estimator)
actual = assembler.assemble()
expected = ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(2.5),
ast.CompOpType.LTE),
ast.NumVal(2.0),
ast.NumVal(3.0))
assert utils.cmp_exprs(actual, expected)
示例3: test_ransac_is_data_valid
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_ransac_is_data_valid():
def is_data_valid(X, y):
assert_equal(X.shape[0], 2)
assert_equal(y.shape[0], 2)
return False
rng = np.random.RandomState(0)
X = rng.rand(10, 2)
y = rng.rand(10, 1)
base_estimator = LinearRegression()
ransac_estimator = RANSACRegressor(base_estimator, min_samples=2,
residual_threshold=5,
is_data_valid=is_data_valid,
random_state=0)
assert_raises(ValueError, ransac_estimator.fit, X, y)
示例4: test_ransac_max_trials
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_ransac_max_trials():
base_estimator = LinearRegression()
ransac_estimator = RANSACRegressor(base_estimator, min_samples=2,
residual_threshold=5, max_trials=0,
random_state=0)
assert_raises(ValueError, ransac_estimator.fit, X, y)
# there is a 1e-9 chance it will take these many trials. No good reason
# 1e-2 isn't enough, can still happen
# 2 is the what ransac defines as min_samples = X.shape[1] + 1
max_trials = _dynamic_max_trials(
len(X) - len(outliers), X.shape[0], 2, 1 - 1e-9)
ransac_estimator = RANSACRegressor(base_estimator, min_samples=2)
for i in range(50):
ransac_estimator.set_params(min_samples=2, random_state=i)
ransac_estimator.fit(X, y)
assert_less(ransac_estimator.n_trials_, max_trials + 1)
示例5: test_ransac_warn_exceed_max_skips
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_ransac_warn_exceed_max_skips():
global cause_skip
cause_skip = False
def is_data_valid(X, y):
global cause_skip
if not cause_skip:
cause_skip = True
return True
else:
return False
base_estimator = LinearRegression()
ransac_estimator = RANSACRegressor(base_estimator,
is_data_valid=is_data_valid,
max_skips=3,
max_trials=5)
assert_warns(ConvergenceWarning, ransac_estimator.fit, X, y)
assert_equal(ransac_estimator.n_skips_no_inliers_, 0)
assert_equal(ransac_estimator.n_skips_invalid_data_, 4)
assert_equal(ransac_estimator.n_skips_invalid_model_, 0)
示例6: test_ransac_multi_dimensional_targets
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_ransac_multi_dimensional_targets():
base_estimator = LinearRegression()
ransac_estimator = RANSACRegressor(base_estimator, min_samples=2,
residual_threshold=5, random_state=0)
# 3-D target values
yyy = np.column_stack([y, y, y])
# Estimate parameters of corrupted data
ransac_estimator.fit(X, yyy)
# Ground truth / reference inlier mask
ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_
).astype(np.bool_)
ref_inlier_mask[outliers] = False
assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask)
示例7: _vlines
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def _vlines(lines, ctrs=None, lengths=None, vecs=None, angle_lo=20, angle_hi=160, ransac_options=RANSAC_OPTIONS):
ctrs = ctrs if ctrs is not None else lines.mean(1)
vecs = vecs if vecs is not None else lines[:, 1, :] - lines[:, 0, :]
lengths = lengths if lengths is not None else np.hypot(vecs[:, 0], vecs[:, 1])
angles = np.degrees(np.arccos(vecs[:, 0] / lengths))
points = np.column_stack([ctrs[:, 0], angles])
point_indices, = np.nonzero((angles > angle_lo) & (angles < angle_hi))
points = points[point_indices]
if len(points) > 2:
model_ransac = linear_model.RANSACRegressor(**ransac_options)
model_ransac.fit(points[:, 0].reshape(-1, 1), points[:, 1].reshape(-1, 1))
inlier_mask = model_ransac.inlier_mask_
valid_lines = lines[point_indices[inlier_mask], :, :]
else:
valid_lines = []
return valid_lines
示例8: _hlines
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def _hlines(lines, ctrs=None, lengths=None, vecs=None, angle_lo=20, angle_hi=160, ransac_options=RANSAC_OPTIONS):
ctrs = ctrs if ctrs is not None else lines.mean(1)
vecs = vecs if vecs is not None else lines[:, 1, :] - lines[:, 0, :]
lengths = lengths if lengths is not None else np.hypot(vecs[:, 0], vecs[:, 1])
angles = np.degrees(np.arccos(vecs[:, 1] / lengths))
points = np.column_stack([ctrs[:, 1], angles])
point_indices, = np.nonzero((angles > angle_lo) & (angles < angle_hi))
points = points[point_indices]
if len(points) > 2:
model_ransac = linear_model.RANSACRegressor(**ransac_options)
model_ransac.fit(points[:, 0].reshape(-1, 1), points[:, 1].reshape(-1, 1))
inlier_mask = model_ransac.inlier_mask_
valid_lines = lines[point_indices[inlier_mask], :, :]
else:
valid_lines = []
return valid_lines
示例9: test_model_ransac_regressor_default
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_model_ransac_regressor_default(self):
model, X = fit_regression_model(
linear_model.RANSACRegressor())
model_onnx = convert_sklearn(
model, "ransac regressor",
[("input", FloatTensorType([None, X.shape[1]]))])
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
verbose=False,
basename="SklearnRANSACRegressor-Dec4",
allow_failure="StrictVersion("
"onnxruntime.__version__)"
"<= StrictVersion('0.2.1')",
)
示例10: test_model_ransac_regressor_mlp
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_model_ransac_regressor_mlp(self):
model, X = fit_regression_model(
linear_model.RANSACRegressor(
base_estimator=MLPRegressor(solver='lbfgs')))
model_onnx = convert_sklearn(
model, "ransac regressor",
[("input", FloatTensorType([None, X.shape[1]]))])
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
verbose=False,
basename="SklearnRANSACRegressorMLP-Dec3",
allow_failure="StrictVersion("
"onnxruntime.__version__)"
"<= StrictVersion('0.2.1')",
)
示例11: test_model_ransac_regressor_tree
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_model_ransac_regressor_tree(self):
model, X = fit_regression_model(
linear_model.RANSACRegressor(
base_estimator=GradientBoostingRegressor()))
model_onnx = convert_sklearn(
model, "ransac regressor",
[("input", FloatTensorType([None, X.shape[1]]))])
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
verbose=False,
basename="SklearnRANSACRegressorTree-Dec3",
allow_failure="StrictVersion("
"onnxruntime.__version__)"
"<= StrictVersion('0.2.1')",
)
示例12: RANSAC_m
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def RANSAC_m(X_ransac,y_ransac,predFeat=False):
ransac=RANSACRegressor(LinearRegression(),max_trials=100,min_samples=10,residual_metric=lambda x:np.sum(np.abs(x),axis=1),residual_threshold=1.0,random_state=0) #max_trials为最大迭代次数,min_samples随机抽取作为内点的最小样本数量,residual_metric传递了一个lambda函数,拟合曲线与样本点间垂直距离的绝对值,residual_threshold残差阈值,只有小于该值的样本点从加入内点inliers中,否则为外电outliers中,默认使用MAD(Median Absolute Deviation中位数决定偏差)估计内点阈值
ransac.fit(X_ransac,y_ransac)
print('Slope:%.3f;Intercept:%.3f'%(ransac.estimator_.coef_[0],ransac.estimator_.intercept_))
X=X_ransac
y=y_ransac
inlier_mask=ransac.inlier_mask_ #内点掩码
# print(inlier_mask)
outlier_mask=np.logical_not(inlier_mask) #外点掩码
line_X=np.arange(0,5,0.5)
line_y_ransac=ransac.predict(line_X[:,np.newaxis])
plt.scatter(X[inlier_mask],y[inlier_mask],c='blue',marker='o',label='Inliers')
plt.scatter(X[outlier_mask],y[outlier_mask],c='lightgreen',marker='s',label='OutLiers')
plt.plot(line_X,line_y_ransac,color='red')
plt.xlabel('hygiene_num')
plt.ylabel('Price in $1000')
plt.legend(loc='upper left')
plt.show()
if type(predFeat).__module__=='numpy': #判断是否有空间几何数据输入
return ransac.predict(predFeat)
示例13: test_ransac_warn_exceed_max_skips
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_ransac_warn_exceed_max_skips():
global cause_skip
cause_skip = False
def is_data_valid(X, y):
global cause_skip
if not cause_skip:
cause_skip = True
return True
else:
return False
base_estimator = LinearRegression()
ransac_estimator = RANSACRegressor(base_estimator,
is_data_valid=is_data_valid,
max_skips=3,
max_trials=5)
assert_warns(UserWarning, ransac_estimator.fit, X, y)
assert_equal(ransac_estimator.n_skips_no_inliers_, 0)
assert_equal(ransac_estimator.n_skips_invalid_data_, 4)
assert_equal(ransac_estimator.n_skips_invalid_model_, 0)
示例14: test_ransac_multi_dimensional_targets
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def test_ransac_multi_dimensional_targets():
base_estimator = LinearRegression()
ransac_estimator = RANSACRegressor(base_estimator, min_samples=2,
residual_threshold=5, random_state=0)
# 3-D target values
yyy = np.column_stack([y, y, y])
# Estimate parameters of corrupted data
ransac_estimator.fit(X, yyy)
# Ground truth / reference inlier mask
ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_
).astype(np.bool_)
ref_inlier_mask[outliers] = False
assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask)
# XXX: Remove in 0.20
示例15: _ransac_regressor
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import RANSACRegressor [as 别名]
def _ransac_regressor(candidate_data, reference_data, max_trials=10000):
model = linear_model.RANSACRegressor(linear_model.LinearRegression(),
max_trials=max_trials)
model.fit(numpy.array([[c] for c in candidate_data]),
numpy.array(reference_data))
gain = model.estimator_.coef_
offset = model.estimator_.intercept_
return gain, offset