0

我有一个文本文件,它只是一个保存为 csv 的熊猫数据框。以下是该文件的内容:

combination_output,total_true,frequency,priori-probability
000,0,275,0.0
001,0,25,0.0
010,16,16,1.0
011,14,14,1.0
100,0,0,0
101,0,44,0.0
110,0,0,0
111,247,247,1.0

我的问题很简单:给定包含 0 或 1 的三个数字的组合输出,我在上述文件中搜索此组合并返回先验概率(该文件的最后一列)。考虑到我应该在该文件中搜索的大组合矩阵,我是这样做的:

#open the file as a pandas dataframe 
table=pd.read_csv("myfile.csv")

#I have a big matrix where its several lines contain one combination 
# of 3 binary numbers that I 
# should search in that pandas dataframe
# For each value, I search it in that dataframe 
for index_combination in range(combination.shape[0]):

        #I get the probability in that table where the combination of
        #1 and 0s is the same I want to search
        probability=table.loc[table['combination_output'] == combination[index_combination],'priori-probability']

但是,这是我打印时得到的

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = method(y)
000
Series([], Name: priori-probability, dtype: float64)

似乎无法在该表中搜索此类值,例如 000。通过打印 Pandas 数据框,我得到以下信息:

       combination_output  total_true  frequency  priori-probability
0                   0           0        275                 0.0
1                   1           0         25                 0.0
2                  10          16         16                 1.0
3                  11          14         14                 1.0
4                 100           0          0                 0.0
5                 101           0         44                 0.0
6                 110           0          0                 0.0
7                 111         247        247                 1.0

如您所见,pandas 数据框显示的不是 000,而是 0;它显示 1 而不是 001;而不是 010,它显示 10 等等。如果我在该表中找到 000,它应该返回我 0,这是该组合的概率。

我怎样才能让熊猫读取二进制值,就像它们保存在我的文本文件中一样,顺便说一句,它以前也是熊猫数据框?

4

1 回答 1

1

您可以将它们读取为字符串数据类型:

table=pd.read_csv("myfile.csv", dtype={'combination_output': str})

这会将组合读取为字符串而不是数字。

我假设您的组合矩阵中有字符串值

于 2020-05-18T11:09:19.160 回答