适合的功能不能用灵活型进行减少

时间:2017-10-06 20:33:49

标签: python linear-regression databricks

我有一个数据集,其中包含42套公寓的面积和价格。我使用python和databricks,我加载了一个带有,的csv文件作为列分隔符。后来,我指定区域为整数,价格为double。然后我导入图形库并进行回归:

import matplotlib.pyplot as plt
from sklearn import linear_model

后来我读了我的数据库:

aptos=sqlContext.read.format('csv').options(header='true',
interSchema='true').load('/FileStore/tables/yl3r1mgv1507304115516/aptos_dataset-5ad32.csv')
display(aptos)

使用以下几行,我使用数据库中的列创建了输入变量:

X=aptos.select("area").collect()
Y=aptos.select("precio").collect()

然后我创建我的回归模型:

regr = linear_model.LinearRegression()

此时我没有问题。但是,当我运行以下行时:

regr.fit(X,Y)

我收到了错误:

TypeError: cannot perform reduce with flexible type

我可以看到更多细节:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<command-2158797891361999> in <module>()
      1 
      2 
----> 3 regr.fit(X,Y)

/databricks/python/local/lib/python2.7/site-packages/sklearn/linear_model/base.pyc in fit(self, X, y, sample_weight)
    517         X, y, X_offset, y_offset, X_scale = self._preprocess_data(
    518             X, y, fit_intercept=self.fit_intercept, normalize=self.normalize,
--> 519             copy=self.copy_X, sample_weight=sample_weight)
    520 
    521         if sample_weight is not None:

/databricks/python/local/lib/python2.7/site-packages/sklearn/linear_model/base.pyc in _preprocess_data(X, y, fit_intercept, normalize, copy, sample_weight, return_mean)
    197             else:
    198                 X_scale = np.ones(X.shape[1])
--> 199         y_offset = np.average(y, axis=0, weights=sample_weight)
    200         y = y - y_offset
    201     else:

/databricks/python/local/lib/python2.7/site-packages/numpy/lib/function_base.pyc in average(a, axis, weights, returned)
    933 
    934     if weights is None:
--> 935         avg = a.mean(axis)
    936         scl = avg.dtype.type(a.size/avg.size)
    937     else:

/databricks/python/local/lib/python2.7/site-packages/numpy/core/_methods.pyc in _mean(a, axis, dtype, out, keepdims)
     63         dtype = mu.dtype('f8')
     64 
---> 65     ret = umr_sum(arr, axis, dtype, out, keepdims)
     66     if isinstance(ret, mu.ndarray):
     67         ret = um.true_divide(

TypeError: cannot perform reduce with flexible type

我道歉但我不能共享我的数据库。我是Python的新手,我对R有更多的专业知识。我将非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

感谢Abdou。读取我的数据库时出现输入错误,这是正确的方法:

    aptos=sqlContext.read.format('csv').options(header='true', inferSchema='true').load('/FileStore/tables/yl3r1mgv1507304115516/aptos_dataset-5ad32.csv')

现在回归正在发挥作用:

regr.fit(X,Y)
Out[4]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)