TypeError:float()参数必须是字符串或数字:float(ab [1:])

时间:2018-05-20 07:15:31

标签: python python-2.7

我有这样的代码我在float(uji[1:])

中收到错误
    ab = []
    for fitur in self.fituruji:
        if (fitur[0] == 'F1') or (fitur[0] == 'F2') or (fitur[0] == 'F3') or (
                fitur[0] == 'F4') or (fitur[0] == 'F11') or (fitur[0] == 'F12'):
            ab.append(fitur)
    print ab

    for i, uji in enumerate(ab):
        print i, uji[1:]
        f_pd_neg = (bpf_neg[i] ** float(uji[1:])) * ((1 - bpf_neg[i]) ** (1 - float(uji[1:])))
        f_pd_pos = (bpf_pos[i] ** float(uji[1:])) * ((1 - bpf_pos[i]) ** (1 - float(uji[1:])))
        print f_pd_pos
        print f_pd_neg

在uji中是这样一个数组:

['F11', '1', '0', '0', '1', '1'] 
['F2', '1', '1', '0', '1', '0'] 
['F4', '1', '0', '0', '0', '0']

我已经给了漂浮物发生了什么,我不知道,而且bpf_pos是这样的:

[0.42857142857142855, 0.5714285714285714, 0.5714285714285714]

和bpf_neg:

[0.14285714285714285, 0.2857142857142857, 0.2857142857142857]

错误是:

C:\Users\ACER\PycharmProjects\ProgramSkripsi\venv\Scripts\python.exe C:/Users/ACER/PycharmProjects/ProgramSkripsi/NaiveBayes.py
Traceback (most recent call last):
[0.42857142857142855, 0.5714285714285714, 0.5714285714285714]
  File "C:/Users/ACER/PycharmProjects/ProgramSkripsi/NaiveBayes.py", line 219, in <module>
    nb.BernoulliTest()
[0.14285714285714285, 0.2857142857142857, 0.2857142857142857]
[['F11', '1', '0', '0', '1', '1'], ['F2', '1', '1', '0', '1', '0'], ['F4', '1', '0', '0', '0', '0']]
  File "C:/Users/ACER/PycharmProjects/ProgramSkripsi/NaiveBayes.py", line 92, in BernoulliTest
0 ['1', '0', '0', '1', '1']
    f_pd_neg = (bpf_neg[i] ** float(ab[1:])) * ((1 - bpf_neg[i]) ** (1 - float(ab[1:])))
TypeError: float() argument must be a string or a number

谁能告诉我这是什么问题?

1 个答案:

答案 0 :(得分:1)

您需要遍历uji[1:]。类似的东西:

f_pd_neg = sum(bpf_neg[i] ** x * (1 - bpf_neg[i]) ** (1 - x) for x in map(int, uji[1:]))