DataFrame具有浮点值,但在其上调用to_csv()会生成一个空CSV

时间:2017-05-24 09:37:57

标签: python csv pandas dataframe

请考虑以下代码:

columns = ['tf-idf','bag_of_words']
index = ['MultinomialNB', 'LinearSVC', 'LogisticRegression', \
        'DecisionTreeClassifier','MLPClassifier']
df = pd.DataFrame(columns = columns, index = index)

estimators_dict = OrderedDict([('MultiNomialNB', MultinomialNB()), \
                    ('LinearSVC', LinearSVC()), \
                    ('LogisticRegression', LogisticRegression()), \
                    ('DecisionTreeClassifier', DecisionTreeClassifier()), \
                    ('MLPClassifier',MLPClassifier(max_iter=10))])

transformers_dict = OrderedDict([('tf-idf', TfidfVectorizer(max_features=500)), \
                                    ('bag_of_words', CountVectorizer())]) 
steps = []

for transformer_name, transformer in transformers_dict.items():         
    steps.append((transformer_name, transformer))

    for estimator_name, estimator in estimators_dict.items():           
        steps.append((estimator_name, estimator))
        model = Pipeline(steps)
        predicted_labels = cross_val_predict(model, all_features,all_labels, cv=5)

        # f1 is float       
        f1 = f1_score(all_labels, predicted_labels, average = 'weighted')
        # writing to DataFrame
        df[transformer_name][estimator_name] = round(f1,2)          
        # This correctly shows the value which just written
        print(str(df[str(transformer_name)][str(estimator_name)]))  # line a 

        del steps[1]    

    del steps[0]        

# but writing to csv create a file with no values whatsoever
df.to_csv('classification_results_f1score') # line b

快速小上下文:在我的分类任务中,我使用了一组特征变换器和另一组sklearn分类器。我正在运行这两个集合的所有可能组合,以查看哪个模型表现最佳。

我正在计算每个模型的f1-score(浮点值)并将其存储在数据框中。该值已成功写入数据帧。我可以通过访问它来验证相同的内容。 (排行

但是当我将数据帧写入csv之后,所有模型运​​行结束(两个for循环结束)后,它生成一个csv,如下所示:

,tf-idf,bag_of_words
MultinomialNB,,
LinearSVC,,
LogisticRegression,,
DecisionTreeClassifier,,
MLPClassifier,,

这里似乎有什么问题?为什么这些值没有出现在csv?

0 个答案:

没有答案