如何解决线性回归中的“异常:数据必须是一维的”问题?

我需要在波士顿房价数据集上运行线性回归,但不使用scikit。

这是我目前想到的方案

import pandas as pdimport numpy as npimport matplotlib.pyplot as mltfrom sklearn.cross_validation import train_test_split data = pd.read_csv("housing.csv", delimiter=' ',                   skipinitialspace=True,                   names=['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE',                          'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']                  )df_x = data.drop('MEDV', axis = 1)df_y = data['MEDV']x_train, x_test, y_train, y_test = train_test_split(df_x, df_y,                                                    test_size=0.2,                                                    random_state=4                                                   )def hypothesis(x, theta):    return np.dot(x, theta.T)def costfn(predictions, y, x):    a = 1 / (2 * len(x)) * np.sum((prediction - y) ** 2)    return adef gradient(theta, alpha, predictions, x, y):    theta = np.subtract(theta, (alpha / len(x)) * np.dot(np.subtract(predictions, y).T, x))    return thetaalpha = 0.001iters = 1000theta = np.zeros([1, 13])predictions = hypothesis(x_train, theta)for i in range(iters):    predictions = hypothesis(x_train, theta)    theta = gradient(theta, alpha, predictions, x_train, y_train)predictions = hypothesis(x_test, theta)print(predictions)

我已经输入并分离了测试和训练案例,一切运作正常。但我遇到了这个错误 –

Exception                                 Traceback (most recent call last)<ipython-input-33-36492e2820ce> in <module>      6 for i in range(iters):      7     predictions = hypothesis(x_train, theta)----> 8     theta = gradient(theta, alpha, predictions, x_train, y_train)      9      10 predictions = hypothesis(x_test, theta)<ipython-input-32-15d0b5b7bf16> in gradient(theta, alpha, predictions, x, y)      9      10 ---> 11     theta = np.subtract(theta, (alpha / len(x)) * np.dot(np.subtract(predictions, y).T, x))     12     return theta/usr/lib/python3/dist-packages/pandas/core/series.py in __array_wrap__(self, result, context)    502         """    503         return self._constructor(result, index=self.index,--> 504                                  copy=False).__finalize__(self)    505     506     def __array_prepare__(self, result, context=None):/usr/lib/python3/dist-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)    262             else:    263                 data = _sanitize_array(data, index, dtype, copy,--> 264                                        raise_cast_failure=True)    265     266                 data = SingleBlockManager(data, index, fastpath=True)/usr/lib/python3/dist-packages/pandas/core/series.py in _sanitize_array(data, index, dtype, copy, raise_cast_failure)   3273     elif subarr.ndim > 1:   3274         if isinstance(data, np.ndarray):-> 3275             raise Exception('Data must be 1-dimensional')   3276         else:   3277             subarr = _asarray_tuplesafe(data, dtype=dtype)Exception: Data must be 1-dimensional

请帮助我。如果我的逻辑有误,请告诉我,因为我是初学者。


回答:

pandas在数据管理方面非常出色,但我倾向于在数学步骤中使用NumPy对象。pandas在这里试图做一些聪明的事情,我不知道是什么,但如果你将df_x.valuesdf_y.values传递给train_test_split(),你的代码就可以运行:

x_train, x_test, y_train, y_test = train_test_split(df_x.values,                                                    df_y.values,                                                    test_size=0.2,                                                    random_state=4                                                   )

Related Posts

神经网络反向传播代码不工作

我需要编写一个简单的由1个输出节点、1个包含3个节点的…

值错误:y 包含先前未见过的标签:

我使用了 决策树分类器,我想将我的 输入 作为 字符串…

使用不平衡数据集进行特征选择时遇到的问题

我正在使用不平衡数据集(54:38:7%)进行特征选择…

广义随机森林/因果森林在Python上的应用

我在寻找Python上的广义随机森林/因果森林算法,但…

如何用PyTorch仅用标量损失来训练神经网络?

假设我们有一个神经网络,我们希望它能根据输入预测三个值…

什么是RNN中间隐藏状态的良好用途?

我已经以三种不同的方式使用了RNN/LSTM: 多对多…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注