本文介绍了predict() 缺少 1 个必需的位置参数:sklearn LinearRegression 中的“X"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用简单的线性回归来预测薪水.其中 X 是经验年数,y 是薪水.

这是我的代码

# 导入库将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt将熊猫导入为 pd# 导入数据集dataset = pd.read_csv('Salary_Data.csv')X = dataset.iloc[:, :-1].valuesy = dataset.iloc[:, 1].values# 将数据集拆分为训练集和测试集从 sklearn.model_selection 导入 train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)# 将简单线性回归拟合到训练集从 sklearn.linear_model 导入 LinearRegression回归量 = 线性回归回归器(X_train,y_train)# 预测测试集结果Y_pred = regressor.predict(X_test)

这是我的错误

Y_pred = regressor.predict(X_test)回溯(最近一次调用最后一次):文件<ipython-input-28-e33267d5ef4e>",第 1 行,在 <module> 中Y_pred = regressor.predict(X_test)类型错误:predict() 缺少 1 个必需的位置参数:'X'

我做错了什么?我该如何解决这个问题/错误?

解决方案
# Fitting Simple Linear Regression to the Training Set从 sklearn.linear_model 导入 LinearRegressionregressor = LinearRegression() # <-- 你需要像这样实例化回归器regressor.fit(X_train, y_train) # <-- 需要调用regressor的fit方法# 预测测试集结果Y_pred = regressor.predict(X_test)

I am trying to predict the salary using simple linear regression. Where X is year of experience and y is salary.

This is my code


# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values


# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Fitting Simple Linear Regression to the Training Set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression
regressor(X_train, y_train)

# Predicting the Test set results
Y_pred = regressor.predict(X_test)

This is my error

Y_pred = regressor.predict(X_test)
Traceback (most recent call last):

  File "<ipython-input-28-e33267d5ef4e>", line 1, in <module>
    Y_pred = regressor.predict(X_test)

TypeError: predict() missing 1 required positional argument: 'X'

What am I doing wrong? How can I resolve this issue /error ?

解决方案
# Fitting Simple Linear Regression to the Training Set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()  # <-- you need to instantiate the regressor like so 
regressor.fit(X_train, y_train) # <-- you need to call the fit method of the regressor

# Predicting the Test set results
Y_pred = regressor.predict(X_test)

这篇关于predict() 缺少 1 个必需的位置参数:sklearn LinearRegression 中的“X"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:56