gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:21:07 28 4
gpt4 key购买 nike

我正在尝试创建一个函数,它将采用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com