Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected 2D array, got 1D array instead. GradientBostingRegressor #413

Closed
Karoloso opened this issue Feb 11, 2024 · 1 comment
Closed

Expected 2D array, got 1D array instead. GradientBostingRegressor #413

Karoloso opened this issue Feb 11, 2024 · 1 comment

Comments

@Karoloso
Copy link

Karoloso commented Feb 11, 2024

I have this program code:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
from sklearn.ensemble import GradientBoostingRegressor
from matplotlib import pyplot as plt
from mapie.regression import MapieQuantileRegressor

Model = GradientBoostingRegressor(
 n_estimators = 500,
 max_depth = 4,
 min_samples_split = 5,
 learning_rate = 0.01,
 loss = "quantile")

X, y = make_regression(n_samples=5000, n_features=1, noise=20, random_state=59)

X = dict(enumerate(X.flatten(), 1))
y = dict(enumerate(y.flatten(), 1))

df = pd.DataFrame({'X':X, 'y':y})

X_train, X_tmp,  y_train, y_tmp  = train_test_split(df.X, df.y, test_size=2000, random_state=42)

X_calib, X_test, y_calib, y_test = train_test_split(X_tmp, y_tmp, test_size=1000, random_state=42)

alpha = 0.1
mapie = MapieQuantileRegressor(estimator=Model, cv="split", alpha=alpha)
mapie.fit(X_train, y_train, X_calib=X_calib, y_calib=y_calib)
y_pred, y_pis = mapie.predict(X_test)

predictions = y_test.to_frame()
predictions.columns = ['y_true']
predictions["point prediction"] = y_pred
predictions["lower"] = y_pis.reshape(-1,2)[:,0]
predictions["upper"] = y_pis.reshape(-1,2)[:,1]
predictions

When I try to compile the program, I get this error:

ValueError                                Traceback (most recent call last)
<ipython-input-1-2ca3f38dea46> in <cell line: 31>()
     29 alpha = 0.1
     30 mapie = MapieQuantileRegressor(estimator=Model, cv="split", alpha=alpha)
---> 31 mapie.fit(X_train, y_train, X_calib=X_calib, y_calib=y_calib)
     32 y_pred, y_pis = mapie.predict(X_test)
     33 

5 frames
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)
    900             # If input is 1D raise error
    901             if array.ndim == 1:
--> 902                 raise ValueError(
    903                     "Expected 2D array, got 1D array instead:\narray={}.\n"
    904                     "Reshape your data either using array.reshape(-1, 1) if "

ValueError: Expected 2D array, got 1D array instead:
array=[ 0.5914918 -1.9605857  1.3109057 ... -1.1724522 -1.8717328 -2.839449 ].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample```


 - MAPIE Version [0.8.2]
I cannot fix this error myself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant