[Python] Polynomial regression with 2D array

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have two 2D datasets Xand Y, which respectively represent experimental and theoretical data. In the two cases, each row corresponds to a sample (one physical configuration), and each column corresponds to a measurement done by a sensor at one particular location (feature).

    These two datasets have the same shape (n_samples, n_features)and are linked by an unknown relationship, that I believe to be some kind of polynomial equation.

    Y = a + b*X + c*(X**2) + ...

    where :

    • Y = theoretical values matrix
    • X = experimental values matrix

    Therefore, I try to obtain the polynomial coefficients a, b, c for degree = 2, with sklearn.

    Before trying with real dataset, I am trying with synthetic data but I don't manage to properly obtain the right polynomial coefficients. I have tried with a 1D-array (1 feature) and it's working well but I don't know why it doesn't work with the whole 2D-array.

    I don't know if the problem comes from the method or from my script but any help would be greatly appreciated. Please find below my attempt :


    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)

    Continue reading...

Compartilhe esta Página