电脑
anaconda(python3+)
jupyter编译器
需要下图里面的模块。
构造一组随机数据:x = np.arange(0, 1, 0.001)y = norm.rvs(0, size=1000, scale=0.1)y = y + x**2
把这些点在平面上画出来:plt.scatter(x, y, s=1,color='green')
分别用0次多项式、1次多项式、……、9次多项式来拟合这组数据:y_test = []y_test = np.array(y_test)for d in range(10): clf = Pipeline([('poly', PolynomialFeatures(degree=d)), ('linear', LinearRegression(fit_intercept=False))]) clf.fit(x[:, np.newaxis], y) y_test = clf.predict(x[:, np.newaxis]) print(clf.named_steps['linear'].coef_)
画出这10次的拟合的图像:y_test = []y_test = np.array(y_test)for d in range(10): clf = Pipeline([('poly', PolynomialFeatures(d)), ('linear', LinearRegression(fit_intercept=False))]) clf.fit(x[:, np.newaxis], y) y_test = clf.predict(x[:, np.newaxis]) plt.plot(x, y_test, linewidth=2)plt.scatter(x, y, s=1,color='green')plt.show()
分别用10次、20次、100次多项式来拟合这些数据:degree = [10,100,1000]y_test = []y_test = np.array(y_test)for d in degree: clf = Pipeline([('poly', PolynomialFeatures(degree=d)), ('linear', LinearRegression(fit_intercept=False))]) clf.fit(x[:, np.newaxis], y) y_test = clf.predict(x[:, np.newaxis]) plt.plot(x, y_test, linewidth=2)plt.scatter(x, y, s=1,color='pink')plt.legend(['10','100','1000'], loc='upper left')plt.show()