【发布时间】:2021-06-29 08:36:51
【问题描述】:
我创建了一个类,该类具有输出不同图形的不同功能。功能之一是计算正在传递的任何变量的投标宽度。昨晚我的笔记本运行得非常好,但现在我得到了 NameError: name 'bin_width' is not defined 我不知道为什么。我在一个单独的笔记本中测试了 bin 函数,它可以工作,但由于某种原因,它在课堂上不起作用。
错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-47-1bce9e9c118e> in <module>
2 histo = Graphs('YRONJOB', None, None, None)
3 #Call histogram() function to apply to object
----> 4 histo.histogram()
<ipython-input-46-02e598bef21f> in histogram(self)
42
43 #Create variable that we call the function to calculate the bin width
---> 44 bin = bin_width(self.A)
45 print(bin)
46
NameError: name 'bin_width' is not defined
谁能告诉我我做错了什么以及如何解决这个问题。下面我附上了我的导入单元格、我的类的前四个函数,以及我创建对象的单元格。
#Import library
import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt
#Automatically creates a dataframe don't need pd.DataFrame
data = pd.read_csv("/Users/tianachargin/Desktop/PythonSG/STAT 4490/WidgeOne_CSV.csv")
#print out dataset
print(data)
class Graphs:
#Constructor with parameters
#Self isn't a pass by value parameter but it allows you to reference
def __init__(self, quantVar1, quantVar2, qualVar1, qualVar2):
self.A = quantVar1 #First quantitative variable
self.B = quantVar2 #Second quantitative variable
self.C = qualVar1 #First qualitative variable
self.D = qualVar2 #Second qualitative variable
#Function that calculates bin width for the histogram
def bin_width(variable):
#Import libaray
import math
#Create variable to create array for bins
#Find min of column
min = data[variable].min()
#Find max of column
max = data[variable].max()
#Find the the count of rows (number of data/size/n)
index = data.index
number_of_rows = len(index)
#Calculate number of bins and round up
num_of_bins = (math.ceil(math.sqrt(number_of_rows)))
#Calculate bin width (max - min)/# of bins
bin_width = ((max - min)/num_of_bins)
#Round bin width to one decimal place
increment_bin = round(bin_width, 1)
#Start bin
start_bin = (min - increment_bin)
#End bin
end_bin = (max + increment_bin)
return start_bin, end_bin, increment_bin
#Histogram Function
def histogram(self):
#Import libraries
import math
import numpy as np
import matplotlib.pyplot as plt
#Create variable that we call the function to calculate the bin width
bin = bin_width(self.A)
#Start at value = bin[0], Stop at value = bin[1], Increment by value of bin[2]
bins = np.array(np.arange(start = bin[0], stop = bin[1], step = bin[2]))
#Histogram function
plt.hist(data[self.A], bins, label = self.A, color = "red")
#x-axis label
plt.xlabel(self.A, fontsize = 16)
#y-axis lable
plt.ylabel("Frequency of " + self.A, fontsize = 16)
#Title of graph
plt.title("Histogram of " + self.A, loc = 'center')
plt.show()
return
#Stacked Histogram Function
def stacked_histogram(self):
#Import libraries
import numpy as np
from matplotlib import pyplot as plt
#Create combonations of the values for the two options
data[self.C + "-" + self.D] = data[self.C] + " " + data[self.D]
combos = np.unique(data[self.C + "-" + self.D])
#Create variable that we call the function to calculate the bin width
bin = bin_width(self.A)
#Start at value = bin[0], Stop at value = bin[1], Increment by value of bin[2]
bins = np.array(np.arange(start = bin[0], stop = bin[1], step = bin[2]))
#Create histogram
for i in range(len(combos)):
plt.hist(data[data[self.C + "-" + self.D].isin(combos[i:(len(combos))])][self.A], bins, label = combos[i:(len(combos))])
#x-axis label
plt.xlabel(self.A, fontsize = 16)
#y-axis lable
plt.ylabel("Frequency of ", fontsize = 16)
#Legend of graph
plt.legend(loc = 'upper left')
#Title of graph
plt.title("Histogram of " + self.A + " with unique combinations of " + self.D + " and " + self.C, loc = 'center')
plt.show()
return
#Overlapping Histogram Function
def overlap_histogram(self):
#Import libraries
import numpy as np
from matplotlib import pyplot as plt
#Create variable that we call the function to calculate the bin width
bin = bin_width(self.A)
#Start at value = bin[0], Stop at value = bin[1], Increment by value of bin[2]
bins = np.array(np.arange(start = bin[0], stop = bin[1], step = bin[2]))
#Create histogram
plt.hist(data[self.A], bins, alpha = 0.5, label = self.A, color = "red")
plt.hist(data[self.B], bins, alpha = 0.5, label = self.B, color = "blue")
#x-axis label
plt.xlabel("Variables", fontsize = 16)
#y-axis lable
plt.ylabel("Frequency", fontsize = 16)
#Legend of graph
plt.legend(loc = 'upper left')
#Title of graph
plt.title("Overlapping Histogram of Variables " + self.A + " and " + self.B, loc = 'center')
plt.show()
#Create an object from class Graphs that will have one parameter
histo = Graphs('YRONJOB', None, None, None)
#Call histogram() function to apply to object
histo.histogram()
【问题讨论】:
-
抛开风格不谈,
bin_width的定义缺少self作为参数,它应该用self.bin_width调用。此外,您不想使用bin、min和max作为变量名,因为它们隐藏了具有相同名称的内置函数 -
而且您还使用
bin_width作为变量名在bin_width方法中。这也应该避免 -
附带说明,您不需要每个方法中的所有导入语句。将它们放在模块级别的顶部就足够了
-
这个函数更新是否正确? def bin_width(self): import math min = data[self.A].min() max = data[self.A].max() index = data.index number_of_rows = len(index) num_of_bins = (math.ceil( math.sqrt(number_of_rows))) bin_size = ((max - min)/num_of_bins) increment_bin = round(bin_size, 1) start_bin = (min - increment_bin) end_bin = (max + increment_bin) return start_bin, end_bin, increment_bin
-
不要在 cmets 中添加代码。另外,你为什么不试试看它是否有效?
标签: python-3.x function python-class