scikit-learn-contrib / MAPIE

A scikit-learn-compatible module to estimate prediction intervals and control risks based on conformal predictions.
https://mapie.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
1.2k stars 99 forks source link

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

Closed Karoloso closed 4 months ago

Karoloso commented 4 months ago

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.
Karoloso commented 4 months ago

problem already solved: https://stackoverflow.com/questions/77977098/expected-2d-array-got-1d-array-instead-gradientbostingregressor