繁体   English   中英

“restaurant”参数未访问 Pylance

[英]"restaurant" Parameter Not Accessed Pylance

为什么它给我一个错误,说参数“restaurant is not access Pylance?

餐厅变量

我的完整代码如下

import numpy as np
import random as rand
import matplotlib.pyplot as plt

#Unknown restaurant standard deviations
actual_restaurant1 = np.random.normal(loc = rand.randint(1,50), scale = rand.randint(1,10))
actual_restaurant2 = np.random.normal(loc = rand.randint(1,50), scale = rand.randint(1,10))

stand_dev1 = 100
stand_dev2 = 100
mean1 = 0
mean2 = 0
number_of_visits1 = 0
number_of_visits2 = 0

restaurant1 = np.random.normal(loc = mean1, scale = stand_dev1)
restaurant2 = np.random.normal(loc = mean2, scale = stand_dev2)

def update(restaurant, actual_restaurant, number_of_visits, stand_dev, mean):
    number_of_visits = number_of_visits + 1
    stand_dev = (1 / 10000 + number_of_visits) ** (-1)
    mean = actual_restaurant
    restaurant = np.random.normal(loc = mean, scale = stand_dev, size = 100000)

if restaurant1 > restaurant2:
    update(restaurant1, actual_restaurant1, number_of_visits1, stand_dev1, mean1)
elif restaurant2 > restaurant1:
    update(restaurant2, actual_restaurant2, number_of_visits2, stand_dev2, mean2)

plt.hist(restaurant1, 100)
plt.show()

当您的函数被调用时,似乎该函数正在接收值列表作为第一个参数,而不是对“restaurant”变量的引用,这似乎与 Python 语言描述函数的方式相反。 可能由于餐厅统计样本的变量或对象的类型,这就是函数接受数据的方式。 我无法解释。 由于这似乎是正在发生的事情,我让程序更新您的“restaurant1”或“restaurant2”对象的方式是将函数调用设置为接收餐厅列表的方程。 然后,程序生成适当的直方图。 以下是您的程序的修订版本。

import numpy as np
import random as rand
import matplotlib.pyplot as plt

#Unknown restaurant standard deviations
actual_restaurant1 = np.random.normal(loc = rand.randint(1,50), scale = rand.randint(1,10))
actual_restaurant2 = np.random.normal(loc = rand.randint(1,50), scale = rand.randint(1,10))

stand_dev1 = 100
stand_dev2 = 100
mean1 = 0
mean2 = 0
number_of_visits1 = 0
number_of_visits2 = 0

restaurant1 = np.random.normal(loc = mean1, scale = stand_dev1)
restaurant2 = np.random.normal(loc = mean2, scale = stand_dev2)

def update(restaurant, actual_restaurant, number_of_visits, stand_dev, mean):
    number_of_visits = number_of_visits + 1
    stand_dev = (1 / 10000 + number_of_visits) ** (-1)
    mean = actual_restaurant
    restaurant = np.random.normal(loc = mean, scale = stand_dev, size = 100000)
    return restaurant  # This returns the value to be used in updating your restaurant variable.

if restaurant1 >= restaurant2:
    restaurant1 = update(restaurant1, actual_restaurant1, number_of_visits1, stand_dev1, mean1)
    plt.hist(restaurant1, 100)
    plt.title('Restaurant #1')
elif restaurant2 > restaurant1:
    restaurant2 = update(restaurant2, actual_restaurant2, number_of_visits2, stand_dev2, mean2)
    plt.hist(restaurant2, 100)
    plt.title('Restaurant #2')

plt.show()

当我运行代码时,我确实得到了一个具有相当对称的钟形曲线外观的直方图。

样本直方图

正如我所说,函数定义似乎将餐厅变量视为一组常量数据,就好像您要调用带有常量的函数一样。

average_value(24, 88, x)

有点头疼。 无论如何,继续尝试一下,看看它是否对您有帮助。

问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM