我從這里獲取了這段代碼片段。我沒有做的是從預測資料中提取引數。即對于三次函式,我想知道 a、b、c 和 d 從 ax^3 bx^2 cx d 方程。這如何在管道中完成,尤其是對于 RANSAC 估計器?
from matplotlib import pyplot as plt
import numpy as np
from sklearn.linear_model import (
LinearRegression,
TheilSenRegressor,
RANSACRegressor,
HuberRegressor,
)
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
np.random.seed(42)
X = np.random.normal(size=400)
y = np.sin(X)
# Make sure that it X is 2D
X = X[:, np.newaxis]
X_test = np.random.normal(size=200)
y_test = np.sin(X_test)
X_test = X_test[:, np.newaxis]
y_errors = y.copy()
y_errors[::3] = 3
X_errors = X.copy()
X_errors[::3] = 3
y_errors_large = y.copy()
y_errors_large[::3] = 10
X_errors_large = X.copy()
X_errors_large[::3] = 10
estimators = [
("OLS", LinearRegression()),
("Theil-Sen", TheilSenRegressor(random_state=42)),
("RANSAC", RANSACRegressor(random_state=42)),
("HuberRegressor", HuberRegressor()),
]
colors = {
"OLS": "turquoise",
"Theil-Sen": "gold",
"RANSAC": "lightgreen",
"HuberRegressor": "black",
}
linestyle = {"OLS": "-", "Theil-Sen": "-.", "RANSAC": "--", "HuberRegressor": "--"}
lw = 3
x_plot = np.linspace(X.min(), X.max())
for title, this_X, this_y in [
("Modeling Errors Only", X, y),
("Corrupt X, Small Deviants", X_errors, y),
("Corrupt y, Small Deviants", X, y_errors),
("Corrupt X, Large Deviants", X_errors_large, y),
("Corrupt y, Large Deviants", X, y_errors_large),
]:
plt.figure(figsize=(5, 4))
plt.plot(this_X[:, 0], this_y, "b ")
for name, estimator in estimators:
model = make_pipeline(PolynomialFeatures(3), estimator)
model.fit(this_X, this_y)
mse = mean_squared_error(model.predict(X_test), y_test)
y_plot = model.predict(x_plot[:, np.newaxis])
plt.plot(
x_plot,
y_plot,
color=colors[name],
linestyle=linestyle[name],
linewidth=lw,
label="%s: error = %.3f" % (name, mse),
)
legend_title = "Error of Mean\nAbsolute Deviation\nto Non-corrupt Data"
legend = plt.legend(
loc="upper right", frameon=False, title=legend_title, prop=dict(size="x-small")
)
plt.xlim(-4, 10.2)
plt.ylim(-2, 10.2)
plt.title(title)
plt.show()
uj5u.com熱心網友回復:
在我看來,在安裝管道后,您可能需要在第二個 for 回圈中插入類似于以下代碼段的內容:
if name == 'RANSAC':
print(model.named_steps['ransacregressor'].estimator_.coef_)
print(model.named_steps['ransacregressor'].estimator_.intercept_)
事實上,根據檔案:
estimator_ : 物件
最佳擬合模型(base_estimator 物件的副本)。
因此,您需要進入您需要的管道步驟(named_steps['step_name']是一種方法),然后訪問 的學習系數estimator_。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/390112.html
上一篇:如何從大十進制恢復初始分數
