0

i have created a class called errors in which i am trying to do some errors analysis. i keep getting the error code:

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

i am trying to use the just_print method in order to just use it print the result to the interpreter module by passing it the two arrays x,y as years and temps.

Thank you for any help :D

my original code is al follows:

#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)
Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • What is that `return;` in your `errors` class supposed to be doing? – khelwood Mar 03 '17 at 19:18
  • `test = errors` this doesn't create an `errors` instance, it assigns the *class* errors (classes are just another object type in Python) to the variable `test`. So now `test` and `errors` are different names referring to the same exact class. To instantiate an object, use `errors()`. And your classes should be UpperCase by convention. – juanpa.arrivillaga Mar 03 '17 at 19:21
  • 1
    You migth want to read the [Classes section of the Tutorial](https://docs.python.org/3/tutorial/classes.html) – wwii Mar 03 '17 at 19:22

1 Answers1

2

You are tying to access test.just_print(years, temps) where test is not a instance of class, please create object.

You have to declare just_print method a staticmethod or change its signature as def just_print(self, x,y): and call with instance.

test = errors()
test.just_print(years, temps)
Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • 2
    Also `def just_print(x,y)` should have a `self` parameter. – khelwood Mar 03 '17 at 19:17
  • Updated answer on your comment :) – Nilesh Mar 03 '17 at 19:19
  • man, that was such a simple fix just adding @static method. What's the difference between doing either one of the 2 methods you said about, the static method and the self addition in the passed variable section??? – Alan Manning Mar 03 '17 at 19:41
  • 1
    @AlanManning Go google static methods. There's plenty of information on the internet. – khelwood Mar 03 '17 at 19:46
  • @AlanManning there are different beween staticmethod and bound method, you will get more detail on google. – Nilesh Mar 03 '17 at 22:44
  • Is there any way that i can host a class as a py file on a web server and import the module from the web? – Alan Manning Mar 05 '17 at 00:15
  • @AlanManning can you please ask question in seperate post? I might be not aware of this, if you add in comment, then only I get notification to check, but if you add new post, all community can look for your problem. – Nilesh Mar 06 '17 at 15:05