利用sklearn.preprocessing.PolynomialFeatures生成交叉特征
当我们使用一次多项式拟合一组数据时,可能不太理想,如下图:
如果用直线来进行拟合的话:
如果用三次函数来拟合的话:
如何用python的sklearn库来做呢?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
x = np.linspace(-3,3,num = 50)
#用三次多项式来拟合
from sklearn.pipeline import Pipeline
def PolyLin(degree):
return Pipeline(
[
("poly",PolynomialFeatures(degree=degree)),
("Linearmodel",LinearRegression())
])
lin_3 = PolyLin(degree=3)
lin_3.fit(X,y)
y_predict3 = lin_3.predict(X)
plt.scatter(X,y)
y_predict_3 = lin_3.predict(X)
更多关于sklearn.preprocessing.PolynomialFeatures的描述请看:
sklearn关于preprocessing.PolynomialFeatures的文档