'loops for calculating probability of values in an array

uni is the array created by counting the unique values of an array

uni=cs.unique()

I have the unique values in an array like this

uni = array(['suggest', 'inform', 'discuss', 'sell_or_buy', 'get_information','entertain'], dtype=object)

I want to calculate the probability for naive bays so I gave to following as a loop

prob =[]

for i in uni:
    prob1 = cs.value_counts().i/cs.count()
    prob.append(prob1)
print(prob)

I got the following error

'Series' object has no attribute 'i'

where did I go wrong pls help me understand.



Solution 1:[1]

Just use list.

prob =[]
#just an example
cs = ['suggest', 'suggest', 'inform', 'discuss', 'discuss', 'sell_or_buy', 'get_information','entertain']
uni = ['suggest', 'inform', 'discuss', 'sell_or_buy', 'get_information','entertain']
for i in uni:
    prob1 = cs.count(i)/len(cs)
    prob.append(prob1)
print(prob)

Output: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ka-Wa Yip