本文介绍了'PolynomialFeatures'对象没有属性'predict'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对以下回归模型应用k倍交叉验证:

I want to apply k-fold cross validation on the following regression models:

  1. 线性回归
  2. 多项式回归
  3. 支持向量回归
  4. 决策树回归
  5. 随机森林回归

除了多项式回归之外,我都能对所有方法应用k倍交叉验证,这会给我这个错误 PolynomialFeatures'对象没有属性'predict .如何解决此问题.我也是正确地完成了这项工作,实际上我的主要动机是看哪种模型表现更好,所以有更好的方法来完成这项工作吗?

I am able to apply k-fold cross validation on all except polynomial regression which gives me this error PolynomialFeatures' object has no attribute 'predict. How to work around this issue. Also am I doing the job correctly, actually my main motive is to see which model is performing better, so is there a better way to do this job ??

# Compare Algorithms
import pandas
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.linear_model import LinearRegression

from sklearn.preprocessing import PolynomialFeatures
from sklearn.svm import SVR
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

# load dataset
names = ['YearsExperience', 'Salary']
dataframe = pandas.read_csv('Salary_Data.csv', names=names)
array = dataframe.values
X = array[1:,0]
Y = array[1:,1]

X = X.reshape(-1, 1)
Y = Y.reshape(-1, 1)

# prepare configuration for cross validation test harness
seed = 7

# prepare models
models = []
models.append(('LR', LinearRegression()))

models.append(('PR', PolynomialFeatures(degree = 4)))
models.append(('SVR', SVR(kernel = 'rbf')))
models.append(('DTR', DecisionTreeRegressor()))
models.append(('RFR', RandomForestRegressor(n_estimators = 10)))

# evaluate each model in turn
results = []
names = []
scoring = 'neg_mean_absolute_error'
for name, model in models:
    kfold = model_selection.KFold(n_splits=10, random_state=seed)
    cv_results = model_selection.cross_val_score(model, X, Y.ravel(), cv=kfold, scoring=scoring)
    results.append(cv_results)
    names.append(name)
    msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)

# boxplot algorithm comparison
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()

推荐答案

sklearn 中,您可以通过以下方式获得多项式回归:

In sklearn you get polynomial regression by:

    通过使用 sklearn.preprocessing.PolynomialFeatures
  1. 通过使用 sklearn.linear_model.LinearRegression


玩具示例:


Toy example:

from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model

# Create linear regression object
poly = PolynomialFeatures(degree=3)

X_train = poly.fit_transform(X_train)
X_test = poly.fit_transform(X_test)

model = linear_model.LinearRegression()
model.fit(X_train, y_train)

print(model.score(X_train, y_train))

这篇关于'PolynomialFeatures'对象没有属性'predict'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:58