Python -重塑数组:线性回归分组依据

dkqlctbz  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(170)

无法从函数输出数据,正在按预测(按ClientID)返回组。
错误是围绕我一直试图重塑工作的数组。

def model(dfTotal3, target):
    y = dfTotal3[['Steps']].values
    X = dfTotal3[['WeightDiff']].values
    X = X.reshape(X.shape[1:])
    X = X.transpose()
    return np.squeeze(LinearRegression().fit(X, y).predict(target))

def group_predictions(df, target):
    target = dfWeightComp[['DTWDG']].values
    return dfTotal3.groupby('ClientID').apply(model, target)

group_predictions(dfTotal3, dfTotal3['DTWDG'])

由此产生的错误是:

ValueError: cannot reshape array of size 10 into shape (1,)

但是,当我将10换成1时,我得到以下错误:

ValueError: cannot reshape array of size 10 into shape ()

我正在寻找一个动态的解决方案,因为我的数据集的增长,任何帮助将不胜感激。

bsxbgnwa

bsxbgnwa1#

下面的代码适用于此解决方案:

def model(dfTotal3, target):
    y = dfTotal3[['Steps']].values
    X = dfTotal3[['WeightDiff']].values
    X = X.reshape(-1, 1)
    return np.squeeze(LinearRegression().fit(X, y).predict(target.reshape(-1, 1)))

def group_predictions(df):
    predictions = df.groupby('ClientID')['PredictedSteps'].apply(list)
    df_steps = pd.DataFrame(columns=['ClientID', 'Steps'])
    for client_id, steps in predictions.iteritems():
        df_steps = pd.concat([df_steps, pd.DataFrame({'ClientID': [client_id], 'Steps': [np.mean(steps)]})], ignore_index=True)
    return df_steps

print(df_steps)

相关问题