Search code examples
pythonpython-3.xscikit-learnregressioncross-validation

'PolynomialFeatures' object has no attribute 'predict'


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

Solution

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