numpy - create a feature vector using pandas or python -
i have binary classifier takes 200 element input feature vector shown below
[ id, v1, v2, ...,v190, v200, class] [ 7, 0, 0, ..., 0, 0, 0 ], [ 8, 0, 1, ..., 0, 0, 1 ], [ 9, 0, 0, ..., 0, 0, 1 ],
for each element x may have set of attributes in v1-v200
sql = 'select x_id, x_attr elements x_hash = %s' cur.execute(sql, (x_hash,)) x1 = cur.fetchone() x1 # x1 returns id , list of attributes (123, [v2,v56,v200])
given output want create feature vector such 1 above, if attribute in list matches attribute in set v1- v200 set 1.
[ id, v1, v2,...,v56,...,v190, v200, class ], [ 123, 0, 1,...,1,..., 0, 1, ? ],
how can in pandas or python?
first initializing pandas dataframe , building on example:
df = pd.dataframe(none, columns=['v'+str(i) in range(1,201)]) sql = 'select x_id, x_attr elements x_hash = %s' cur.execute(sql, (x_hash,)) x1_id, features = cur.fetchone() df.loc[x1_id] = 0 # initializes values id zero. df.loc[x1_id, features] = 1 # sets relevant features value of one.
i haven't included class, wasn't sure how using it.
Comments
Post a Comment