Я пытаюсь преобразовать класс, который я создал:
class LabeledSourceFeatures:
label = ''
features = FeatureSet()
def __init__(self, label, features):
self.label = label
self.features = features
# added this as part the of the workaround
def flat_features(self):
return self.features.__dict__
Проще создать FeatureSet
и добавить его в этот класс (просто сказать, что я не хочу просто взять члены FeatureSet
и поместить их в LabeledSourceFeatures
), но конечный результат я хочу поместить в pandas фрейм данных. Проблема заключалась в том, что я получал фрейм данных с 2 столбцами, один для строки метки и один для объекта FeatureSet
. Что я действительно хочу, так это взять все ключи и значения моих FeatureSet
и сделать их своими столбцами.
Это то, что я пробовал до сих пор:
intermediate_data = [(t.__dict__, x.__dict__ for x in t.features) for t in labeledFeatures]
# This fails for syntax error.
Обходной путь это:
intermediate_data = [(t.label, t.flat_features()) for t in labeledFeatures]
final_data = []
for row in intermediate_data:
new_row = row[1]
new_row['label'] = row[0]
final_data.append(new_row)
, но это выглядит очень неэффективно.
Редактировать:
a FeatureSet
выглядит так:
class FeatureSet:
"""
Adapted from the CSFS presented in De-anonymizing Programmers via Code Stylometry
by:
Aylin Caliskan-Islam, Drexel University; Richard Harang, U.S. Army Research Laboratory;
Andrew Liu, University of Maryland; Arvind Narayanan, Princeton University;
Clare Voss, U.S. Army Research Laboratory; Fabian Yamaguchi, University of Goettingen;
Rachel Greenstadt, Drexel University
"""
# LEXICAL FEATURES
ln_keyword_length = 0
ln_unique_keyword_length = 0
ln_comments_length = 0
ln_token_length = 0
avg_line_length = 0
# LAYOUT FEATURES
ln_tabs_length = 0
ln_space_length = 0
ln_empty_length = 0
white_space_ratio = 0
is_brace_on_new_line = False
do_tabs_lead_lines = False
comment_text = ''