gpt4 book ai didi

python - 使用 pandas 或 python 创建特征向量

转载 作者:行者123 更新时间:2023-12-01 04:47:41 25 4
gpt4 key购买 nike

我有一个二元分类器,它采用 200 个元素的输入特征向量,如下所示

   [ id, v1,   v2, ...,v190, v200, class]
[ 7, 0, 0, ..., 0, 0, 0 ],
[ 8, 0, 1, ..., 0, 0, 1 ],
[ 9, 0, 0, ..., 0, 0, 1 ],

对于每个元素 X,它可能具有 v1-v200 中的任何属性集

   sql = 'SELECT x_id, x_attr FROM elements WHERE x_hash = %s'
cur.execute(sql, (x_hash,))
x1 = cur.fetchone()
x1 # x1 returns the id and a list of attributes
(123, [v2,v56,v200])

鉴于该输出,我想创建一个特征向量,例如上面的特征向量,如果列表中的属性与集合 v1-v200 中的任何属性匹配,那么它将被设置为 1。

   [  id,   v1,  v2,...,v56,...,v190, v200,    class ],
[ 123, 0, 1,...,1,..., 0, 1, ? ],

我如何在 pandas 或 python 中做到这一点?

最佳答案

首先初始化 pandas 数据框,然后构建您的示例:

df = pd.DataFrame(None, columns=['v'+str(i) for i in range(1,201)])
sql = 'SELECT x_id, x_attr FROM elements WHERE x_hash = %s'
cur.execute(sql, (x_hash,))
x1_id, features = cur.fetchone()
df.loc[x1_id] = 0 # Initializes all values for the ID to zero.
df.loc[x1_id, features] = 1 # Sets relevant features to a value of one.

我没有包含类,因为我不确定你是如何使用它的。

关于python - 使用 pandas 或 python 创建特征向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29087284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com