Reputation: 166
Say I want to fit a polynomial model of degree d
via least squares regression. There are two methods I've learned in python. One uses numpy
and the other sklearn
. After I fit the model and get the coefficients, to predict values for test data, in sklearn
, I can do:
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x_train, y_train) # Fitting on Training Data
model.predict(20) #One value in test data is 20
What is the numpy
equivalent for model.predict()
after I fit the model using:
import numpy.polynomial.polynomial as poly
np_model = poly.polyfit(x_train, y_train, d)
Upvotes: 0
Views: 3940
Reputation: 4647
I use numpy.polyval, docs are at https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyval.html - here is a graphical polynomial fitter as an example that uses polyval.
import numpy, matplotlib
import matplotlib.pyplot as plt
xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7, 0.0])
yData = numpy.array([1.1, 20.2, 30.3, 40.4, 50.0, 60.6, 70.7, 0.1])
polynomialOrder = 2 # example quadratic
# curve fit the test data
fittedParameters = numpy.polyfit(xData, yData, polynomialOrder)
print('Fitted Parameters:', fittedParameters)
modelPredictions = numpy.polyval(fittedParameters, xData)
absError = modelPredictions - yData
SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
print('RMSE:', RMSE)
print('R-squared:', Rsquared)
print()
##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(xData, yData, 'D')
# create data for the fitted equation plot
xModel = numpy.linspace(min(xData), max(xData))
yModel = numpy.polyval(fittedParameters, xModel)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
plt.show()
plt.close('all') # clean up after using pyplot
graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)
Upvotes: 2