【问题标题】:Python Functions in Classes NameError类 NameError 中的 Python 函数
【发布时间】: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 调用。此外,您不想使用binminmax 作为变量名,因为它们隐藏了具有相同名称的内置函数
  • 而且您还使用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


【解决方案1】:

我想说的第一件事是你不应该在函数中导入模块。足以将它们导入顶部。 第二件事,你在做什么

def bin_width(变量)

并像这样使用

bin_width(self.A)

正确的方法 def bin_width(self, variable)

用法:

bin = bin_width(self, yourvariable)

【讨论】:

  • 函数中的模块是什么意思?我不能在 python 的函数中调用函数吗? self.A 是对变量的引用,它们之间不应该有逗号,对吧?
  • 你在创建函数和调用时错过了自赋值
猜你喜欢
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 2018-11-04
  • 2018-10-29
相关资源
最近更新 更多