How do perform polynomial features and linear regression on 3 dimensional data (x,y,z)?

Viewed 232

I need help performing polynomial features on 3 dimensional data and performing linear regression to create a line of best fit on the 3 dimensional polynomial.

I have a random dataframe with x, y, and z as the columns that forms a polynomial scatterplot. X and Y are similar values while z is vastly different. Example:

X=(-3,9,-20,-8,-14)
Y=(-2,8,-19,-8,-13)
Z=(-960,110,4867,-149,1493)

I have done this for 2 dimensional data but not 3d.

poly=PolynomialFeatures(degree=2,include_bias=False)
X_poly=poly.fit_transform(X.reshape(-1,1)) 
X_poly[0]

However, how do I handle the data when I have x, y, and z? Do I need to perform poly.fit_transform x and y?

Next I did linear regression

from sklearn.linear_model import LinearRegression

LinReg = LinearRegression()
LinReg.fit(X_poly,z)

Then when I create test data for x and y and perform the predict method on z, the resulting line is linear instead of a polynomial.

Any help would be much appreciated.

2 Answers

I finally figured it out. I needed to pass a DataFrame containing only x and y through the polynomial features and then use the XY_poly and z in the linreg.fit(). This trains the model for my next steps to create the line of best fit for the polynomial.

When PolynomialFeatures says that fit_transform() method requires x and y, here x is an n-dimensional array of features and y are the target values which are optional. In your case I would do the following:

X=(-3,9,-20,-8,-14)
Y=(-2,8,-19,-8,-13)
Z=(-960,110,4867,-149,1493)

foo = np.array([X, Y, Z])
foo = foo.transpose() # This transposes the array to bring it to shape (n, 3)
poly = PolynomialFeatures(3)
poly.fit_transform(foo)

One this is done you can use fit_transform(foo).

Related