2016-01-03 138 views
-9

我試圖把一個變量稱爲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] 

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) 

我的數據集看起來像這樣:

 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 

我就在這行類型錯誤:

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

任何想法可能在這裏發生了什麼?

這是從函數調用整個錯誤:

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' 
+4

請提供一個完整的簡單例子 – karlson

+4

什麼是'input_features'?... –

+0

@IronFist,我已經提供了一些數據集的更多信息。 – user1471980

回答

1

好像你要使用數組,並與操作數的int「**」 althought,預計兩個整數或至少兩個數字。也許你想使用sum_x或sum_xy作爲第一個參數呢?