I tried to generate random probs by using the following line code:
probs = [np.clip(random.normalvariate(0.1, 0.05), 0, 1) for x in range(1000)]
Unexpectedly I faced the following error message:
AttributeError: module 'numpy.random' has no attribute 'normalvariate'
Any idea how to solve this? I checked out the docs I find that this attribute exists in the numpy.random however it doesn't work when I used it in above code.
Any help to fix this issue will be appreciated.
It seems that you make confusion between random module whose documenttion is : https://docs.python.org/3.11/library/random.html
And random sub-module that belongs to numpy, its documentation can be found here https://numpy.org/doc/stable/reference/random/index.html
Error origin
It seems that you imported numpy.random and you tried to use normalvariate while the latter function belongs to random module.
Solution
So to solve the issue write the following import:
import random
probs = [np.clip(random.normalvariate(0.1, 0.05), 0, 1) for x in range(1000)]
Output:
[0.10399310517618868,
0.10416076922742254,
0.10683877729386676,
0.14789317007499886,
0.11551976284566698,
...
Related
I am trying to solve a bilevel problem using Pyomo in Python. However, when I try to run the code, I am getting the following error:
"Implicit conversion of Pyomo NumericValue type `mon' to a float is disabled. This error is often the result of using Pyomo components as arguments to one of the Python built-in math module functions when
defining expressions. Avoid this error by using Pyomo-provided math functions."
In Pyomo's documentation there is no reference to Pyomo-provided functions. I want to know how I can modify the penultimate line of code shown so that model.rn[i,j] meet the integer requirement?
The following is my code:
import random
import matplotlib.pyplot as plt
import numpy as np
from pyomo.environ import *
from pyomo.bilevel import *
from pyomo.bilevel.components import SubModel
from pyomo.opt import SolverFactory
capacity =[150,80, 65]
model = ConcreteModel()
model.sub = SubModel()
model.M=RangeSet(1,3)
model.N=RangeSet(1,12)
model.f= Param(model.M,model.N,within=NonNegativeIntegers,initialize=20)
model.v= Param(model.M,model.N,within=NonNegativeIntegers)
model.sub.x = Param(within=Binary)
model.r= Var(model.M,model.N,within=PercentFraction)
model.rp= Var(model.M,model.N,within=NonNegativeReals,bounds=(0, 10))
model.rn = Var(model.M, model.N, within=NonNegativeIntegers)
model.un= Var(model.M,model.N,within=NonNegativeIntegers)
for j in range(1,13):
model.v[1,j] = capacity[0]-model.f[1,j]
model.v[2,j] = capacity[1]-model.f[2,j]
model.v[3,j] = capacity[2]-model.f[3,j]
for j in range(1,13):
for i in range(1,4):
model.rn[i,j]=floor(model.v[i,j]*model.r[i,j])
model.un[i,j]=model.v[i,j]-model.rn[i,j]
That's tricky to do. As far as I know, it may only work on the values of that pyomo object as model.r is a pyomo object. It is not a problem on a parameter but the variable.
You may want to write out constraints that models the python 'floor' function instead.
In Scipy documentation, it clearly says for using statistics(especially for a short description of an array):
Import scipy
scipy.stats.describe(a, axis=0, ddof=1, bias=True, nan_policy='propagate')
But I get the AttributeError:
module 'scipy' has no attribute 'stats'
then in another part of the documentation, it says :
from scipy import stats
which works fine for me. Actually, I don't get the exact difference between . and from ...... import ...... and why the first one doesn't work.
Thank you so much for your help. I would appreciate it if you guys can provide me some links too.
I am trying to build a score matching using pymatch. Unfortunately I am getting the following error
Fitting Models on Balanced Samples: 1\200Error: Unable to coerce to Series, length must be 1: given 1898
Here is my code
from sklearn.datasets.samples_generator import make_blobs
from pymatch.Matcher import Matcher
import pandas as pd
import numpy as np
X, y = make_blobs(n_samples=5000, centers=2, n_features=2, cluster_std=3.5)
df = pd.DataFrame(dict(x=X[:,0], y=X[:,1], label=y))
df['population'] = np.random.choice([1, 0], size=len(df), p=[0.8, 0.2])
control = df[df.label == 1]
test = df[df.label == 0]
m = Matcher(test, control, yvar="population", exclude=['label'])
m.fit_scores(balance=True, nmodels=200)
if I ran this code I will get the error. I am quite sure that I was able to run this before, but after changing some versions, this doesn't work anymore. Unfortunately I wasn't able to fix it by going back to previous versions, so not sure what's going on here...
Downgrading pandas did not work for me, but I found where the problem is.
It is an error in the method _scores_to_accuracy() of Matcher.py. I downloaded the source file, edited the function on my local machine, and now it works fine.
https://github.com/benmiroglio/pymatch/issues/23
Please downgrade your pandas, to version 0.23.4.
Use the code:
pip install pandas==0.23.4
I tried to run following program of using python 3.5.1.
from scipy import optimize
optimize.anneal(f, input_vector0, lower = 0, upper = 2*np.pi)
I got the following error message:
AttributeError: module 'scipy.optimize' has no attribute 'anneal'.
Can anybody tell me what should I do to fix this? i really appreciate it !
The problem is that it is removed in 0.16 and higher.
Replace anneal with basinhopping.
refer to link
I'm trying to generate a random.gauss numbers but I have message error. Here is my code:
import sys,os
import numpy as np
from random import gauss
previous_value1=1018.163072765074389
previous_value2=0.004264112033664
alea_var_n=random.gauss(1,2)
alea_var_tau=random.gauss(1,2)
new_var_n= previous_value1*(1.0+alea_var_n)
new_var_tau=previous_value2*(1.0+alea_var_tau)
print 'new_var_n',new_var_n
print 'new_var_tau',new_var_tau
I got this error:
Traceback (most recent call last):
File "lolo.py", line 15, in <module>
alea_var_n=random.gauss(1,2)
AttributeError: 'builtin_function_or_method' object has no attribute 'gauss'
Someone know what's wrong, I'm a newbye with python. Or is it a numpy version problem.
For a faster option, see Benjamin Bannier's solution (which I gave a +1 to). Your present code that you posted will not work for the following reason: your import statement
from random import gauss
adds gauss to your namespace but not random. You need to do this instead:
alea_var_n = gauss(1, 2)
The error in your post, however, is not the error you should get when you run the code that you have posted above. Instead, you will get the following error:
NameError: name 'random' is not defined
Are you sure you have posted the code that generated that error? Or have you somehow included the wrong error in your post?
Justin Barber shows you an immediate solution for your problem.
Since you are using NumPy you could however use their generators as well since they appear to be significantly faster (about a factor 5-7 on my machine), e.g.
alea_var_n = np.random.normal(1, 2)