首页 > 解决方案 > TabNetRegressor 不适用于重塑的数据

问题描述

我正在使用 tabnet 的 PyTorch 实现,但无法弄清楚为什么我仍然会收到此错误。我将数据导入数据框,我使用这个函数来获取我的 X,然后是我的训练测试拆分

def get_X_y(df):
    ''' This function takes in a dataframe and splits it into the X and y variables
    '''
    X = df.drop(['is_goal'], axis=1)
    y = df.is_goal
    
    return X,y

X,y = get_X_y(df)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=101)

然后我用它来重塑我的 y_train

y_train.values.reshape(-1,1)

然后创建模型的一个实例并尝试拟合它

reg = TabNetRegressor()
reg.fit(X_train, y_train)

我得到这个错误

ValueError: Targets should be 2D : (n_samples, n_regression) but y_train.shape=(639912,) given.
Use reshape(-1, 1) for single regression.

我理解为什么我需要重塑它,因为这很常见,但我不明白为什么它仍然给我这个错误。我已经在笔记本中重新启动了内核,所以我也不认为这是持久性内存问题。

标签: python-3.xpytorch

解决方案


您必须重新分配它:

y_train = y_train.values.reshape(-1,1)

否则,它不会改变。


推荐阅读