1

I have a selection of lists of variables

import numpy.random as npr 

w = [0.02, 0.03, 0.05, 0.07, 0.11, 0.13, 0.17]

x = 1

y = False

z = [0.12, 0.2, 0.25, 0.05, 0.08, 0.125, 0.175]

v = npr.choice(w, x, y, z)

I want to find the probability of the value V being a selection of variables eg; False or 0.12.

How do I do this. Heres what I've tried;

 import numpy.random as npr
import math 

w = [0.02, 0.03, 0.05, 0.07, 0.11, 0.13, 0.17]
x = 1
y = False
z = [0.12, 0.2, 0.25, 0.05, 0.08, 0.125, 0.175]

v = npr.choice(w, x, y, z)

from collections import Counter 
c = Counter(0.02, 0.03, 0.05, 0.07, 0.11, 0.13, 0.17,1,False,0.12, 0.2, 0.25, 0.05, 0.08, 0.125, 0.175)

def probability(0.12):
    return float(c[v]/len(w,x,y,z))

which I'm getting that 0.12 is an invalid syntax

JuhBuh
  • 9
  • 1
  • You would have to pass a variable name, not a value. So, instead of `def probability(0.12)` you would have to pass `def probability(some_variable_name)` which can also be `def probability(some_variable_name=0.12)`. – Cleb Nov 18 '18 at 21:23
  • In addition, `len(w, x, y, z)` does not make sense. – Cleb Nov 18 '18 at 21:29

1 Answers1

0

There are several issues in the code, I think you want the following:

import numpy.random as npr
import math
from collections import Counter


def probability(v=0.12):
    return float(c[v]/len(combined))


w = [0.02, 0.03, 0.05, 0.07, 0.11, 0.13, 0.17]
x = [1]
y = [False]
z = [0.12, 0.2, 0.25, 0.05, 0.08, 0.125, 0.175]

combined = w + x + y + z

v = npr.choice(combined)

c = Counter(combined)

print(probability())
print(probability(v=0.05))

1) def probability(0.12) does not make sense; you will have to pass a variable which can also have a default value (above I use 0.12)

2) len(w, x, y, z) does not make much sense either; you probably look for a list that combines all the elements of w, x, y and z. I put all of those in the list combined.

3) One would also have to put in an additional check, in case the user passes e.g. v=12345 which is not included in combined (I leave this to you).

The above will print

0.0625
0.125

which gives the expected outcome.

Cleb
  • 25,102
  • 20
  • 116
  • 151