Python Benford’s law

Benford’s law states that in real data where the values are spread across several orders of magnitude, about 30% of the values will start with the number 1, whereas only about 4.6% of the values will start with the number 9. This is contrary to what we might expect, namely that values starting with 1 and 9 would be equally likely. Using the file expenses.txt which consists of a number of costs from an expense account, determine what percentage start with each of the digits 1 through 9. This technique is used by accountants to detect fraud.



















d = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}
expenseCost = [line.strip()
for line in open('C:/Users/ZM/Desktop/Documents/New folder/cijene.txt')]
expenseCost1 = [line.split()
for line in expenseCost]
counter =
0
for i in range(1, len(expenseCost1)):
   
if float(expenseCost1[i][6][:1]) == 1:
        d[
1] += 1
   
elif float(expenseCost1[i][6][:1]) == 2:
        d[
2] += 1
   
elif float(expenseCost1[i][6][:1]) == 3:
        d[
3] += 1
   
elif float(expenseCost1[i][6][:1]) == 4:
        d[
4] += 1
   
elif float(expenseCost1[i][6][:1]) == 5:
        d[
5] += 1
   
elif float(expenseCost1[i][6][:1]) == 6:
        d[
6] += 1
   
elif float(expenseCost1[i][6][:1]) == 7:
        d[
7] += 1
   
elif float(expenseCost1[i][6][:1]) == 8:
        d[
8] += 1
   
elif float(expenseCost1[i][6][:1]) == 9:
        d[
9] += 1
   
counter += 1
print(d)
for i in range(1, len(d)+1):
   
print(i, ' = ', round(d[i]/counter*100, 2), '%')
SOLUTION:
   
{
1: 156, 2: 41, 3: 42, 4: 54, 5: 18, 6: 30, 7: 36, 8: 48, 9: 18}
1  =  35.21 %
2  =  9.26 %
3  =  9.48 %
4  =  12.19 %
5  =  4.06 %
6  =  6.77 %
7  =  8.13 %
8  =  10.84 %
9  =  4.06 %

Коментари

Популарни постови са овог блога

Python Fan functions

Python StopWatch milliseconds

Python Accounting