Home > Software engineering >  Create sub-group with indexes
Create sub-group with indexes

Time:12-12

I have a time series of 0s and 1s in python. This shows the error status of a machine at certain intervals. It means that it gave 1 error. What I want to do here is to divide this data into 10 subgroups and find the percentage of errors (ie 1s) occurring in each group in that subgroup. Indexes are increased by 1 while creating subgroups. In other words, the first 10 values ​​with index number 0-9 should be the first group, the second 10 values ​​with index number 1-10 should be the second group, and the third 10 values ​​with index number 2-11 should be the third group. Can you help me?

0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 My data like this.result ı want: 1.gruop:0011001111 2.group:0110011110 3.group:1100111100 4.group:1001111000 ....

CodePudding user response:

You can use list slices to get your required groups -

data = ['0', '0', '1', '1', '0', '0', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '0']
groups = []
for x in range(len(data) - 9):
    groups.append(''.join(data[x:10 x]))
print(groups)

for i, group in enumerate(groups):
    print(f"Group{i}: {group}")

Output:

['0011001111', '0110011110', '1100111100', '1001111000', '0011110000', '0111100001', '1111000011', '1110000111', '1100001111', '1000011111', '0000111111', '0001111110']
Group0: 0011001111
Group1: 0110011110
Group2: 1100111100
Group3: 1001111000
Group4: 0011110000
Group5: 0111100001
Group6: 1111000011
Group7: 1110000111
Group8: 1100001111
Group9: 1000011111
Group10: 0000111111
Group11: 0001111110

If you don't want the strings and instead want a list of each subgroup then you can replace groups.append(''.join(data[x:10 x])) with groups.append(data[x:10 x]))


Follow-up to print the error ratio in each group, count number of 1's and then divide it by observations in the group -

for i, group in enumerate(groups):
    error_ratio = group.count('1')/len(group)
    print(f"Group{i} error ratio: {error_ratio}")

Output:

Group0 error ratio: 0.6
Group1 error ratio: 0.6
Group2 error ratio: 0.6
Group3 error ratio: 0.5
Group4 error ratio: 0.4
Group5 error ratio: 0.5
Group6 error ratio: 0.6
Group7 error ratio: 0.6
Group8 error ratio: 0.6
Group9 error ratio: 0.6
Group10 error ratio: 0.6
Group11 error ratio: 0.6

CodePudding user response:

Code:-

data="001100111100001111110"
i=0
while i<len(data)-9:
    print("Group" str(i) ": " data[i:i 10] "\t Percentage Error: " str(data[i:i 10].count('1')/10))
    i =1

Output:-

Group0: 0011001111   Percentage Error: 0.6
Group1: 0110011110   Percentage Error: 0.6
Group2: 1100111100   Percentage Error: 0.6
Group3: 1001111000   Percentage Error: 0.5
Group4: 0011110000   Percentage Error: 0.4
Group5: 0111100001   Percentage Error: 0.5
Group6: 1111000011   Percentage Error: 0.6
Group7: 1110000111   Percentage Error: 0.6
Group8: 1100001111   Percentage Error: 0.6
Group9: 1000011111   Percentage Error: 0.6
Group10: 0000111111  Percentage Error: 0.6
Group11: 0001111110  Percentage Error: 0.6
  • Related