0

I am running into an error of "NameError" with the following code (from https://stackoverflow.com/a/14659965/3284469):

>>> import scipy
>>> import numpy
>>> values = [10, 20, 30]
>>> probabilities = [0.2, 0.5, 0.3]
>>> distrib = rv_discrete(values=(values, probabilities))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'rv_discrete' is not defined

Could you give me some solution or hint to solve the problem? Thanks!


Updated: Following the solution by mhlester, I solved the Name error, but bump into type error:

>>> from scipy.stats import rv_discrete
>>> distrib = scipy.stats.rv_discrete(values=(values, probabilities))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 4903, in __init__
    if name[0] in ['aeiouAEIOU']:
TypeError: 'NoneType' object has no attribute '__getitem__'

2 Answers 2

4

You need to give it the full namespace:

import scipy
...
scipy.stats.rv_discrete(values=(values, probabilities))

Or import it directly:

from scipy.stats import rv_discrete
...
rv_discrete(values=(values, probabilities))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! +1. I now bump into a new TypeError, and I updated my post with this new one.
The error is because name is None. I've never used scipy myself so you'll have to turn to someone else for further answer inside distributions
0

It seems like some time has passed, but I was getting the same error as you. It seems that you have to specify the "name" variable here for some reason. For instance, for me the following worked:

from scipy.stats import rv_discrete
values = (10, 20, 30)
probabilities = (0.2, 0.5, 0.3)
distrib = rv_discrete(name='my_special_discrete_simulator', values=(values, probabilities))
distrib.rvs(size=10)
array([20, 20, 20, 30, 20, 20, 30, 20, 10, 30])

but replacing the 4th line with:

distrib = rv_discrete(values=(values, probabilities))

Gives:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 4903, in __init__
  if name[0] in ['aeiouAEIOU']:
TypeError: 'NoneType' object has no attribute '__getitem__'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.