简体   繁体   中英

"restaurant" Parameter Not Accessed Pylance

Why does it give me a error saying the parameter "restaurant is not accessed Pylance?

Restaurant Variable

My full code is below

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()

It appears when your function is called, it appears that the function is receiving the list of values as the first parameter as opposed to a reference to the "restaurant" variable, which kind of seems counter to how functions are described for the python language. Possibly because of the type of variable or object the restaurant statistical samples are, that's the way the function accepts the data. I can't explain that. Since that seems to be what is happening, the way I got the program to update your "restaurant1" or "restaurant2" objects was to set the function call up as an equation that receives a restaurant list back. Then, the program generates the appropriate histogram. Following is a revised version of your program.

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()

When I ran the code, I did get a histogram with a fairly symmetrical bell curve look.

样本直方图

As I said, it seems like the function definition treats the restaurant variable like a constant set of data as if you were to call a function with constants.

average_value(24, 88, x)

Kind of a head scratcher. Anyway, go ahead and experiment with that and see if it helps you out.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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