【问题标题】:Multivariete Regression Error "AttributeError: 'numpy.ndarray' object has no attribute 'columns'"多元回归错误“AttributeError:'numpy.ndarray'对象没有属性'columns'”
【发布时间】:2020-01-25 22:56:53
【问题描述】:

我正在尝试运行多元线性回归,但在尝试获取回归模型的系数时出现错误。

我得到的错误是: 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'

对此的任何帮助将不胜感激!

【问题讨论】:

  • 始终提供完整的错误追溯。至少给出行号。它将有助于调试。
  • 您的 XY 是 numpy 数组,而不是数据帧。您的错误可能在coeff_df 中。删除.values 并尝试一下。
  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议遵循帮助文档中的发布指南。 On topichow to ask 和 ...the perfect question 在此处申请。 StackOverflow 不是设计、编码、研究或教程资源。但是,如果您按照您在网上找到的任何资源,做出诚实的解决方案尝试,并遇到问题,您就会有一个很好的例子可以发布。
  • 不清楚最终目标,要不要输出每一列的系数??
  • 嗨@abheet22,是的,这是正确的,我想得到每列的系数。非常感谢您的支持。

标签: python pandas linear-regression multivariate-testing


【解决方案1】:

我做了同样的事情,从第 18 行的 X 变量中删除 .values 对我有用!

【讨论】:

  • 这应该是评论而不是答案
猜你喜欢
  • 2021-09-24
  • 2020-12-03
  • 2020-11-29
  • 2020-10-06
  • 2018-01-25
  • 2016-06-29
  • 2020-03-25
  • 2013-12-07
  • 2017-10-16
相关资源
最近更新 更多