2017-03-03 5 views
0

나는 오류 분석을 시도하는 오류라는 클래스를 만들었습니다. 년 임시 직원으로 y를파이썬에서 만든 클래스를 호출 할 때 오류가 발생했습니다.

TypeError: unbound method just_print() must be called with errors instance as first argument (got ndarray instance instead)

난 그냥 두 배열 X 그것을 전달하여 인터프리터 모듈에 결과를 출력 사용하기 위해 just_print 방법을 사용하는 것을 시도하고있다 : 나는 오류 코드가 계속 .

어떤 도움 주셔서 감사합니다 : D

내 원래의 코드는 알는 다음입니다 : 당신은 액세스 test.just_print(years, temps)에 묶는된다

#imports 
from pylab import * 
#defining error class 
class errors(object): 
    #creates class object 
    def __init__(self): 
     pass 
    #error method 
    def just_print(x,y): # defining error method 
     #error analysis 
     n = len(x) #length of the x data 
     D = sum(x**2) - 1./n * sum(x)**2 # d is the sum of the squares minus the sum squared over n 
     x_bar = mean(x) # average all x values 
     p_coeff, residuals, _, _, _ = polyfit(x, y, 1, full=True) #using only the first 2 results from the poly fit returned values 

     dm_squared = 1./(n-2)*residuals/D # error squared using standard forula 
     dc_squared = 1./(n-2)*(D/n + x_bar**2)*residuals/D #error squared using standard forula 

     dm = sqrt(dm_squared) # rooted squared error 
     dc = sqrt(dc_squared) # rooted squared error 

     #printing results 
     print("The value for gradient is") 
     print("%.3g" % p_coeff[0] , "error:" , "%.3g" % dm) 
     print("The value for intercept is") 
     print("%.3g" % p_coeff[1] , "error:" , "%.3g" % dc) 
    return; 


#reading in data from data file called wales_temp.txt 
f = open("wales_temp.txt") 
temps = loadtxt(f, skiprows=8)#skips thw first 8 rows as they are information aboutthe data 
f.close() 
years = linspace(1911,2012,(2012 - 1911 + 1), dtype=int)#set the years array of euqal length such that each corrosponds to the temp 

print("The max temprature for the lest 100 years was " , "%.3g" % max(temps) , " Celcius in " , years[argmax(temps)]) 
print("The min temprature for the lest 100 years was " , "%.3g" % min(temps) , " Celcius in " , years[argmin(temps)]) 

print("The standard deviation of the tempratures over the past 100 years is : " , "%.3g" % std(temps)) 

years1990 = linspace(1990,2012,(2012 - 1990 + 1), dtype=int) 
temps1990 = temps[-len(years1990):] 
temps1990_9degs = temps1990[9<temps1990] 
percent = (float(len(temps1990_9degs)))/(float(len(temps[9<temps]))) 

print("The percantage of years with tempratures above 9 degrees after 1990 compared to all the years is : " , (100*percent) , "%") 

hist(temps, bins=len(years)) 
hist(temps1990 , bins = len(years)) 
figure("avg temps") 
plot(years, temps, "bx") 
best_fit = poly1d(polyfit(years,temps,1)) 
plot(years,best_fit(years)) 

test = errors 
test.just_print(years, temps) 
+0

'오류'클래스의 'return;'은 무엇을하고 있습니까? – khelwood

+0

'test = errors' 이것은'errors' 인스턴스를 생성하지 않고,'class' 에러 (클래스는 Python의 또 다른 객체 타입입니다)를 변수'test'에 할당합니다. 이제'test'와'errors'는 같은 정확한 클래스를 참조하는 다른 이름입니다. 객체를 인스턴스화하려면'errors()'를 사용하십시오. 그리고 수업은 규칙에 따라 대문자이어야합니다. –

+1

migth는 [튜토리얼의 수업] 섹션 (https://docs.python.org/3/tutorial/classes.html) – wwii

답변

2

test이 클래스의 인스턴스가 아닌 경우, 객체를 생성하십시오.

just_print 메서드를 staticmethod으로 선언하거나 서명을 def just_print(self, x,y):으로 변경하고 인스턴스로 호출해야합니다.

test = errors() 
test.just_print(years, temps) 
+2

을 읽으려고합니다. 또한 'def just_print (x, y)'에는'self '가 있어야합니다 매개 변수. – khelwood

+0

귀하의 의견에 대한 답변을 업데이 트했습니다 :) – Nilesh

+0

남자, 그저 @static 방법을 추가하는 간단한 수정되었습니다. 당신이 말한 2 가지 방법 중 하나를 수행하는 것의 차이는 무엇입니까? 정적 메서드와 전달 된 변수 섹션의 자체 추가 ??? –