python - 如果可能的话,如何使用函数参数作为 npy 方法

标签 python numpy numpy-random

我正在尝试创建一个函数,它将采用 numpy dstr 名称作为参数,并绘制该分布中的随机数据点的直方图。

如果它仅适用于需要 1 个参数的 npy 发行版,那就没问题。只是真的坚持尝试创建 np.random.distribution()... \

# import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline 

#Define a function (Fnc) that produces random numpy distributions (dstr)
#Fnc args: [npy dstr name as lst of str], [num of data pts]
def get_rand_dstr(dstr_name):
  npdstr = dstr_name
  dstr = np.random.npdstr(user.input("How many datapoints?"))
  #here pass each dstr from dstr_name through for loop
  #for loop will prompt user for required args of dstr (nbr of desired datapoints)
  return plt.hist(df)

get_rand_dstr('chisquare')

最佳答案

使用这个代码,它可能会对你有帮助below image shows the value and plot

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline 

def get_rand_dstr(dstr_name):
#     npdstr = dstr_name  
    dstr = 'np.random.{}({})'.format(dstr_name, (input("How many datapoints?"))) # for using any distribution need to manipulate here 
                                                                                 # cause number of args are diffrent for diffrent distibutions           
    print(dstr)
    df = eval(dstr)
    print(df)
#     dstr1 = np.random.chisquare(int(input("How many datapoints?")))
#     print(dstr1)
    return plt.hist(df)

# get_rand_dstr('geometric')  
get_rand_dstr('chisquare')

关于python - 如果可能的话,如何使用函数参数作为 npy 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58966534/

相关文章:

python - 在python中一切都是对象是什么意思

python值变为零,如何防止

python - 当需要抽样的数量多于样本数量时,不放回地随机抽样

python - 与 numpy 的 np.random.RandomState 和 Python 的 random.Random 相互转换?

python - 这些替代的 numpy `uniform` 与 `random` 结构可能有何不同?

python - 将 R data.table 转换为 pandas.DataFrame 的最佳方法?

python - 如何在图像中找到签名?

python - 需要使用 python 替换配置文件中的相似键值

python - matplotlib 的线性回归线给出 ValueError

python - 为什么 numpy.random.Generator.choice 与默认均匀分布相比,给定均匀分布提供不同的结果(种子)?