2016-01-03 84 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作为第一个参数呢?