Multivariete Regression Error“ AttributeError:'numpy.ndarray'对象没有属性'columns'”]

问题描述 投票:1回答:1

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

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

完整错误:

回溯(最近通话):

文件“”,第14行,在coeff_df = pd.DataFrame(regressor.coef_,X.columns,columns = ['Coefficient'])

AttributeError:'numpy.ndarray'对象没有属性'columns'

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

python pandas linear-regression multivariate-testing
1个回答
0
投票

我做了完全一样的事情,从第18行的X变量中删除了.value对我有用!

© www.soinside.com 2019 - 2024. All rights reserved.