Reputation: 1045
Here is my code.
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
X=np.array([[1, 2, 4]]).T
print(X)
y=np.array([1, 4, 16])
print(y)
X_poly = PolynomialFeatures(degree=2).fit_transform(X)
print(X_poly)
# fit_intercept = False since PolynomialFeatures will create a 0-order
polynomial as well.
model = LinearRegression(fit_intercept = False)
model.fit(X_poly,y)
print('Coefficients: \n', model.coef_)
print('Others: \n', model.intercept_)
Please, how can change this code to use many parameters. Example of models:
y=a*x*x+b * z+ c* t (we have y, x, z and t)
Thanks a lot for help. Kind regards.
Upvotes: 0
Views: 728
Reputation: 36599
Just use multiple lists in X.
X = np.array([[1, 2, 4],
[2, 3, 9]]).T #<== Added a second list
print(X)
# Output
feature1 feature2
1 2
2 3
4 9
y = np.array([1, 4, 16])
So here your data have two features. Now you can simply use the same code you are using before:
X_poly = PolynomialFeatures(degree=2).fit_transform(data)
print(X_poly)
# Output
[[ 1. 1. 2. 1. 2. 4.]
[ 1. 2. 3. 4. 6. 9.]
[ 1. 4. 9. 16. 36. 81.]]
model = LinearRegression(fit_intercept = False)
model.fit(X_poly,y)
print('Coefficients: \n', model.coef_)
# Output
('Coefficients: \n', array([-0.10133796, 0.1456888 , -0.01660059, 0.54831516, 0.45019822,
-0.11496531]))
print('Others: \n', model.intercept_)
# Output
('Others: \n', 0.0)
Upvotes: 1