'PolynomialFeatures' object has no attribute 'predict'

核能气质少年 提交于 2021-02-10 11:55:15

问题


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

  1. Linear Regression
  2. Polynomial Regression
  3. Support Vector Regression
  4. Decision Tree Regression
  5. Random Forest Regression

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()

回答1:


In sklearn you get polynomial regression by:

  1. generating polynomial and interaction features on your original dataset by using sklearn.preprocessing.PolynomialFeatures
  2. running ordinary least squares Linear Regression on the transformed dataset by using 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))



回答2:


Here is the changed part of the code if someone wants for reference :

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

models.append(('PR', LinearRegression()))
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)
    if name == 'PR':
        poly_reg = PolynomialFeatures(degree = 4)
        X_poly = poly_reg.fit_transform(X)
        cv_results = model_selection.cross_val_score(model, X_poly, Y.ravel(), cv=kfold, scoring=scoring)
    else:
        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())


来源:https://stackoverflow.com/questions/56201362/polynomialfeatures-object-has-no-attribute-predict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!