Эти два набора данных имеют одинаковую форму (n_samples, n_features)и связаны неизвестным соотношением, которое, как я полагаю, является своего рода полиномиальным уравнением.
Y = a + b*X + c*(X**2) + ...
где:
- Y = матрица теоретических значений
- X = матрица экспериментальных значений
Прежде чем использовать реальный набор данных, я пробую синтетический данные, но мне не удается правильно получить правильные полиномиальные коэффициенты. Я пробовал использовать 1D-массив (1 функция), и он работает хорошо, но я не знаю, почему он не работает со всем 2D-массивом.
Я не знаю. Я знаю, связана ли проблема с методом или с моим сценарием, но буду очень признателен за любую помощь. Ниже представлена моя попытка:
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
# =============================================================================
# x = 1D ARRAY EXAMPLE
# =============================================================================
pipe = make_pipeline(PolynomialFeatures(2),LinearRegression())
rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = 37 + 3*x + 1.5*x**2
# Fit the model
X = x[:, np.newaxis] # Arrange data into a features matrix and target vector.
pipe.fit(X, y)
# Inspect the fit
coef = pipe[1].coef_
print(coef)
# predict
xfit = np.linspace(0, 10, 1000)
Xfit = xfit[:, np.newaxis]
yfit = pipe.predict(Xfit)
# =============================================================================
# x = 2D ARRAY
# =============================================================================
n_subfeatures = 4 # Number of sub-sensors
n_components_per_subf = 3 # Number of components per sub-sensor
n_features = n_subfeatures * n_components_per_subf # Total number of features
n_samples = 100
## GENERATE SYNTHETIC DATA
# Simulated data
np.random.seed(42)
x = np.random.randn(n_samples, n_features)
y = (
0.3
+ 1.7 * x
- 0.5 * x**2
)
# Pipe version
# -------------------------
pipe = make_pipeline(PolynomialFeatures(2),LinearRegression())
# Fit the model
# X = x[:, np.newaxis] # Arrange data into a features matrix and target vector.
X = x
pipe.fit(X, y)
# Inspect the fit
coef = pipe[1].coef_
print(coef)
# Poly / Reg version
# -------------------------
X=x
poly = PolynomialFeatures(degree=2)
poly_features = poly.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(poly_features, y, test_size=0.3, random_state=42)
poly_reg_model = LinearRegression()
poly_reg_model.fit(X_train, y_train)
poly_reg_y_predicted = poly_reg_model.predict(X_test)
coef = poly_reg_model.coef_
print(coef)
Подробнее здесь: https://stackoverflow.com/questions/792 ... h-2d-array
Мобильная версия