-9

I am trying to take a square root of a variable called input_features:

type(test_data['sqft_living'])

dtype: float
Rows: 10
[1430.0, 2950.0, 1710.0, 2320.0, 1090.0, 2620.0, 4220.0, 2250.0, 1260.0, 2750.0]

when I do this:

   def simple_linear_regression(input_features, output):
       sum_y = output.sum()
       sum_x = input_features.sum()
       sum_yx = (output*input_features).sum()
       sum_xx = (input_features**2).sum()
       sum_xx=sum_xx.sum()
       n = float(len(output))
       slope = (sum_yx - ((sum_y*sum_x)/n))/(sum_xx - ((sum_x*sum_x)/n))
       intercept = (sum_y/n) - slope*(sum_x/n)
       return(intercept, slope)

My data set looks like this:

     id     date    price   bedrooms    bathrooms   sqft_living     sqft_lot    floors  waterfront
7129300520  2014-10-13 00:00:00+00:00   221900.0    3.0     1.0     1180.0  5650    1   0
6414100192  2014-12-09 00:00:00+00:00   538000.0    3.0     2.25    2570.0  7242    2   0
5631500400  2015-02-25 00:00:00+00:00   180000.0    2.0     1.0     770.0   10000   1   0
2487200875  2014-12-09 00:00:00+00:00   604000.0    4.0     3.0     1960.0  5000    1   0

I get type error on this line:

sum_xx = (input_features**2).sum()

any ideas what might be happening here?

This is the whole error from the function call:

TypeError                                 Traceback (most recent call last)
<ipython-input-38-6bb15a6a5df2> in <module>()
----> 1 sqft_icept, sqft_slope = simple_linear_regression(test_data['sqft_living'], test_data['price'])
      2 print "Sqft intercept: ", sqft_icept
      3 print "Sqft slope: ", sqft_slope

<ipython-input-37-bb896d3cac4f> in simple_linear_regression(input_features, output)
      3     sum_x = input_features.sum()
      4     sum_yx = (output*input_features).sum()
----> 5     sum_xx = (input_features**2).sum()
      6     sum_xx=sum_xx.sum()
      7     n = float(len(output))

TypeError: unsupported operand type(s) for ** or pow(): 'SArray' and 'int'
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
user1471980
  • 10,127
  • 48
  • 136
  • 235

1 Answers1

0

It seems like you are trying to use an array and an int with the operand "**" althought it expects two ints or at least two numbers. Maybe you want to use sum_x or sum_xy as the first argument instead?

Schnodderbalken
  • 3,257
  • 4
  • 34
  • 60