猿问

多元线性回归和选择列误差

我的问题是,当我尝试拟合模型时,出现此错误。我不知道是什么导致了这个错误,但可能自变量的选择不正确。这是错误


ValueError: Found input variables with inconsistent numbers of samples: [104, 26]

这是我到目前为止构建的代码


import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from scipy import stats


# Import Excel File

data = pd.read_excel("C:\\Users\\AchourAh\\Desktop\\Multiple_Linear_Regression\\SP Level Reasons Excels\\SP00105485_PL22_AAB_05_09_2018_Reasons.xlsx",'Sheet1') #Import Excel file


# Replace null values of the whole dataset with 0

data1 = data.fillna(0)

print(data1)


# Extraction of the independent and dependent variable

X = data1.iloc[0:len(data1),[0,1,2,3]].values.reshape(-1, 1) #Extract the column of the COPCOR SP we are going to check its impact

Y = data1.iloc[0:len(data1),4].values.reshape(-1, 1) #Extract the column of the PAUS SP

print(X)

print(Y)


# Importing

from sklearn.linear_model import LinearRegression

from sklearn import model_selection


# Fitting a Linear Model

lm = LinearRegression() #create an lm object of LinearRegression Class

lm.fit(X, Y)

plt.scatter(X, Y, color = 'red')#plots scatter graph of COP COR against PAUS for values in X_train and y_train

plt.plot(X, lm.predict(X), color = 'blue')#plots the graph of predicted PAUS against COP COR.

plt.title('SP000905974')

plt.xlabel('COP COR Quantity')

plt.ylabel('PAUS Quantity')

plt.show()#Show the graph

我的 excel 文件的第一列包含自变量,第四列包含因变量。我有另一个简单线性回归的代码,它工作正常,但是当我尝试应用多元线性回归时,我只是改变了这条线,但我没有做错什么。


  X = data1.iloc[0:len(data1),[0,1,2,3]].values.reshape(-1, 1)

注意,我是这个的初学者。


白板的微信
浏览 165回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答