你如何解决python中的类型错误

时间:2016-01-03 21:07:17

标签: python

我试图取一个名为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'

1 个答案:

答案 0 :(得分:1)

看起来你正在尝试使用一个数组和一个带操作数的int&#34; **&#34;虽然它预计有两个整数或至少两个数字。也许您想使用sum_x或sum_xy作为第一个参数?