类 NameError 中的 Python 函数

问题描述

我创建了一个具有不同函数的类,可以输出不同的图形。功能之一是计算传递的任何变量的出价宽度。昨晚我的笔记本运行得很好,但现在我收到 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)
      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],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],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],step = bin[2]))
        #Create histogram
        plt.hist(data[self.A],alpha = 0.5,color = "red")
        plt.hist(data[self.B],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)
#Call histogram() function to apply to object
histo.histogram()

解决方法

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

定义 bin_width(变量)

并像这样使用

bin_width(self.A)

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

用法:

bin = bin_width(self,yourvariable)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...