【问题标题】:fitting a model in Keras using arrays in single elements of DataFrame使用 DataFrame 的单个元素中的数组在 Keras 中拟合模型
【发布时间】:2022-08-22 05:02:41
【问题描述】:

我正在尝试生成一个神经网络拟合来估计一个标签,该标签在每个标签值中都有几组数据,如下所示,\'cnames\' 是我从中生成 DF 的数据集群的字典。

我正在创建要传递给我的 NN 模型的数据框,该模型应该使用 x1_N 和 x2 来适应标签 \'AGE\'。

df = pandas.DataFrame(
    data=np.zeros((9,5), dtype=object),
    columns=[\"cluster\", \"x1\", \"x1_N\", \"x2\", \"e_x1\"]
)

df[\'cluster\'] = cnames

for i in range(9):
    df.at[i, \"x1\"] = ins[str(cnames[i])][:,0]
    df.at[i, \"x1_N\"] = ins[str(cnames[i])][:,1]
    df.at[i, \"x2\"] = ins[str(cnames[i])][:,2]
    df.at[i, \"e_x1\"] = ins[str(cnames[i])][:,3]
df[\'AGE\'] = scaled[\'AGE\'].unique()

这给出了一个如下所示的 DF,即 AGE 的每个单个值的 x1 和 x2 等数组:

cluster x1  x1_N    x2  e_x1    AGE
0   c1  [432.7, 591.1, 382.1, 506.6, 595.6, 303.2, 580...   [0.8361023362318888, 0.9521203687767078, 1.111...   [1.7193, 2.7785, 1.3238999999999999, 2.6548000...   [45.9, 35.5, 9.6, 57.3, 31.5, 72.4, 19.8, 22.0...   6.3000
1   c2  [224.3, 2.9, 35.6, -5.0, -27.2, 86.1, -44.0, -...   [0.20393164342662082, -0.970076224393567, -0.9...   [1.2696, 2.0625, 1.5247, 2.2449000000000003, 2...   [10.4, 6.2, 10.6, 11.6, 29.5, 15.0, 22.8, 34.6...   7.7100
2   c3  [236.0, 133.8, -44.1, -14.9, 91.8, -23.3, 24.4...   [0.6994358430148963, -0.45785100287607866, -1....   [1.0577, 1.8270000000000002, 2.6435, 2.8359, 1...   [11.1, 8.2, 42.6, 24.5, 12.8, 9.8, 18.3, 11.3,...   7.6400
3   c4  [492.3, 560.0, 549.5, 517.9, 637.8, 534.4, 537...   [0.8486431354299245, 1.0405252121040436, 1.288...   [2.0703, 2.1886, 1.7657, 2.4898, 2.6012, 2.82,...   [28.3, 24.5, 16.5, 37.0, 43.6, 41.0, 27.0, 7.8...   6.6000
4   c5  [21.6, -1.9, -9.2, 13.7, 26.6, 4.3, -25.2, 20....   [-0.9447143556037185, -1.0546569314070438, -1....   [1.6646999999999998, 1.6484999999999999, 1.703...   [7.8, 6.4, 10.8, 16.7, 26.8, 11.6, 23.7, 20.8,...   8.1800
5   c6  [-4.4, -34.1, 338.0, 30.0, 33.9, 105.9, 91.2, ...   [-2.0495987100264625, -1.2389510703276396, 0.4...   [0.8682, 2.6355999999999997, 1.714899999999999...   [14.0, 33.8, 50.4, 15.4, 26.8, 50.9, 77.2, 43....   7.5798
6   c7  [5.2, 50.2, 43.5, 45.6, 101.6, 49.9, 104.1, 7....   [-1.196782707046483, -0.9495773412485725, -1.1...   [1.3129, 1.2438, 1.068, 0.6129, 0.7575, 0.9362...   [10.3, 6.4, 9.8, 13.7, 8.0, 14.3, 15.2, 16.6, ...   8.4800
7   c8  [105.1, 328.4, 505.0, 341.2, 546.1, 1.9, 292.8...   [-0.7503958386481737, -0.009650781445028284, 1...   [2.6511, 2.7773, 1.6239000000000001, 2.746, 2....   [30.3, 70.7, 16.2, 44.6, 20.4, 20.1, 25.7, 15....   7.2600
8   c9 [474.3, 394.0, 525.3, 144.5, 473.6, 489.0, 507...    [0.625315797587088, 1.0568153452073183, 1.0888...   [2.4826, 1.3874000000000002, 1.881800000000000...   [93.4, 73.9, 82.7, 104.2, 85.7, 110.1, 59.0, 1...   6.7800

尝试在 model.fit 函数中使用 x1_N 和 x2 来适应 AGE 时,这不被接受。我收到以下错误:

ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)。

这大概是因为 Keras 模型不接受包含单个元素中的数组的数据框。有没有办法解决这个问题?我还没有找到一种方法来创建与 df 具有相同布局的张量。

使用的模型也如下:

model = keras.Sequential([
        layers.Dense(units=2, input_dim=2, activation = \'leaky_relu\'),
        layers.Dense(units=12, activation = \'leaky_relu\'),
        layers.Dense(units=2, activation = \'softplus\')
    ])

    loss=my_loss

    model.compile(loss = loss, optimizer = keras.optimizers.Adam(0.01))
    return model

    标签: python pandas tensorflow keras neural-network


    【解决方案1】:

    是的,即使我也面临同样的问题。你有没有想过解决这个问题?

    【讨论】:

    猜你喜欢
    • 2021-04-01
    • 2017-12-14
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2021-02-22
    • 2022-12-29
    • 2020-05-12
    相关资源
    最近更新 更多