多语言展示
当前在线:287今日阅读:155今日分享:35

怎么进行大数据拟合?

本文是按照lsldd的CSDN博客里面的内容来自学的。构造一系列随机点,并试图来拟合这组数据。
工具/原料
1

电脑

2

anaconda(python3+)

3

jupyter编译器

方法/步骤
1

需要下图里面的模块。

2

构造一组随机数据:x = np.arange(0, 1, 0.001)y = norm.rvs(0, size=1000, scale=0.1)y = y + x**2

3

把这些点在平面上画出来:plt.scatter(x, y, s=1,color='green')

4

分别用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_)

5

画出这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()

6

分别用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()

推荐信息