Python 多变量回归误差“;属性错误:';numpy.ndarray和#x27;对象没有属性';列'&引用;

Python 多变量回归误差“;属性错误:';numpy.ndarray和#x27;对象没有属性';列'&引用;,python,pandas,linear-regression,multivariate-testing,Python,Pandas,Linear Regression,Multivariate Testing,我试图运行一个多元线性回归,但是当我试图得到回归模型的系数时,我得到了一个错误 我得到的错误是: AttributeError:'numpy.ndarray'对象没有属性'columns' 以下是我使用的代码: import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as seabornInstance from sklearn.model_selection imp

我试图运行一个多元线性回归,但是当我试图得到回归模型的系数时,我得到了一个错误

我得到的错误是: AttributeError:'numpy.ndarray'对象没有属性'columns'

以下是我使用的代码:

import pandas as pd  
import numpy as np  
import matplotlib.pyplot as plt  
import seaborn as seabornInstance 
from sklearn.model_selection import train_test_split 
from sklearn.linear_model import LinearRegression
from sklearn import metrics
%matplotlib inline

# Main files
dataset = pd.read_csv('namaste_econ_model.csv')
dataset.shape
dataset.describe()
dataset.isnull().any()

#Dividing data into "attributes" and "labels". X variable contains all the attributes and y variable contains labels.

X = dataset[['Read?', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6' , 'x7','x8','x9','x10','x11','x12','x13','x14','x15','x16','x17','x18','x19','x20','x21','x22','x23','x24','x25','x26','x27','x28','x29','x30','x31','x32','x33','x34','x35','x36','x37','x38','x39','x40','x41','x42','x43','x44','x45','x46','x47']].values
y = dataset['Change in Profit (BP)'].values
plt.figure(figsize=(15,10))
plt.tight_layout()
seabornInstance.distplot(dataset['Change in Profit (BP)'])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
regressor = LinearRegression()  
regressor.fit(X_train, y_train)
coeff_df = pd.DataFrame(regressor.coef_, X.columns, columns=['Coefficient'])  
coeff_df
完全错误:

Traceback (most recent call last):

  File "<ipython-input-67-773b9f78bc01>", line 14, in <module>
    coeff_df = pd.DataFrame(regressor.coef_, X.columns, columns=['Coefficient'])

AttributeError: 'numpy.ndarray' object has no attribute 'columns'
回溯(最近一次呼叫最后一次):
文件“”,第14行,在
coeff_df=pd.DataFrame(regressor.coef_,X.columns,columns=['Coefficient']))
AttributeError:'numpy.ndarray'对象没有属性'columns'

在此方面的任何帮助都将不胜感激

我做了完全相同的事情,从第18行的X变量中删除.value对我来说很有效

我做了完全相同的事情,从第18行的X变量中删除.value对我来说很有效

始终对错误进行完整跟踪。至少给出行号。它将帮助调试。您的
X
Y
是numpy数组,而不是数据帧。您的错误可能在
系数df
中。删除
.values
并尝试一下。欢迎使用StackOverflow。请按照创建此帐户时的建议,遵循帮助文档中的过帐指导原则,而且。。。在这里申请。StackOverflow不是设计、编码、研究或教程资源。然而,如果你遵循你在网上找到的任何资源,做出诚实的解决方案尝试,并遇到问题,你会有一个好的例子来发布。不清楚最终目标,你想输出每列的系数吗?@Hi@abheet22,是的,我想得到每列的系数。非常感谢您的支持。请始终完整地跟踪错误。至少给出行号。它将帮助调试。您的
X
Y
是numpy数组,而不是数据帧。您的错误可能在
系数df
中。删除
.values
并尝试一下。欢迎使用StackOverflow。请按照创建此帐户时的建议,遵循帮助文档中的过帐指导原则,而且。。。在这里申请。StackOverflow不是设计、编码、研究或教程资源。然而,如果你遵循你在网上找到的任何资源,做出诚实的解决方案尝试,并遇到问题,你会有一个好的例子来发布。不清楚最终目标,你想输出每列的系数吗?@Hi@abheet22,是的,我想得到每列的系数。非常感谢您的支持。这应该是评论而不是回答。这应该是评论而不是回答