Created
June 25, 2022 00:35
-
-
Save potass13/7db47f25d57f34f8ff48af5b329ecca6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
def main(): | |
M = 9 | |
N = [15, 100] | |
sigma = 0.3 | |
xx = np.linspace(0, 1, 200) | |
for i, n in enumerate(N): | |
x = np.linspace(0, 1, n) | |
t = np.sin(2.*np.pi*x)+np.random.normal(0, sigma, len(x)) | |
df = pd.DataFrame(np.array([x, t]).transpose(), columns=['x', 't']) | |
y = np.poly1d(np.polyfit(df.x.values, df.t.values, M)) | |
plt.subplot(1, 2, i+1) | |
plt.xlabel('x') | |
plt.ylabel('t') | |
plt.xlim(-0.1, 1.1) | |
plt.ylim(-1.5, 1.5) | |
plt.text(0.8, 1.0, 'N = %d' % n, fontsize=15) | |
plt.scatter(x, t, s=60, c='white', linewidths='2', edgecolors='blue') | |
plt.plot(xx, np.sin(2.*np.pi*xx), color='green', linestyle='dashed') | |
plt.plot(xx, y(xx), color='red') | |
plt.show() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment