0

The following code statement return errors, probably coming from function arguments help me solve this.

#calculating b and a to find the linear regression:
class calculate:
    def sum_of_xtrain(x_train):
        sum_xtrain = 0
        for i in range(0,len(x_train)):
            sum_xtrain += x_train[i] 
            i +=1
            return sum_xtrain
    
    def sum_of_ytrain(y_train):
        sum_ytrain = 0
        for i in range(0,len(y_train)):
            sum_ytrain += y_train[i] 
            i +=1
        

    def sum_of_product_of_xytrain(x_train,y_train):
         sum_of_product = 0
         for i in range(0, len(x_train)):
            sum_of_product += x_train[i]*y_train[i]
            i += 1
        
    def square_x_train(x_train):
        square_x = 0
        for i in range(0, len(x_train)):
            square_x += x_train[i] * x_train[i]
            i += 1           

                    
a = calculate()
print(a.sum_of_xtrain(x_train))

    
   
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9172\2110094580.py in <module>
     28 
     29 a = calculate()
---> 30 print(a.sum_of_xtrain(x_train))
     31 
     32 

TypeError: sum_of_xtrain() takes 1 positional argument but 2 were given

 Data is a simple example of common data used in linear regression test programs. I want to calculate the value of a and b in y = a + bx.

user11717481
  • 1
  • 9
  • 15
  • 25
  • [python - should I use static methods or top-level functions](https://stackoverflow.com/questions/18262595/python-should-i-use-static-methods-or-top-level-functions) – wwii Jan 01 '23 at 00:40
  • Typically *in* Python, functions that are related to each other are *encapsulated* in a module (file) rather than a class. The module is imported and the function called with dot notation `module.function()` - doing it that way would rid you of this error without have to mess up those functions by adding a *self* parameter. – wwii Jan 01 '23 at 00:41

1 Answers1

1

If you invoke a function like this a.foo(x) you are implicitly passing self as the first parameter (and x as the second), but you declare the methods with just one argument. Add self as the first argument when declaring methods inside a class.

Gameplay
  • 1,142
  • 1
  • 4
  • 16