Skip to content

Instantly share code, notes, and snippets.

@FGtatsuro
Created January 12, 2019 02:15
Show Gist options
  • Save FGtatsuro/cd60c9072a822cdd2a3654e33beb5e18 to your computer and use it in GitHub Desktop.
Save FGtatsuro/cd60c9072a822cdd2a3654e33beb5e18 to your computer and use it in GitHub Desktop.
回帰分析
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
f = open('springData.txt')
f.readline()
df = pd.read_csv(f, header=None, names=('dist', 'mass'), delimiter=' ')[:-6]
df['force'] = df['mass'] * 9.81
# FYI: https://ja.stackoverflow.com/questions/38755/matplotlib%E3%81%A7%E6%8F%8F%E7%94%BB%E3%81%97%E3%81%9F%E6%95%A3%E5%B8%83%E5%9B%B3%E3%81%AB%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0%E7%9B%B4%E7%B7%9A%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95
# 1-degree
#a, b = np.polyfit(df['force'], df['dist'], 1)
#df['fit'] = a * df['force'] + b
# 3-degree
fit = np.polyfit(df['force'], df['dist'], 3)
df['fit'] = np.polyval(fit, df['force'])
ax = df.plot.scatter(x='force', y='dist')
df.plot(x='force', y='fit', color='black', ax=ax)
plt.show()
Distance (m) Mass (kg)
0.0865 0.1
0.1015 0.15
0.1106 0.2
0.1279 0.25
0.1892 0.3
0.2695 0.35
0.2888 0.4
0.2425 0.45
0.3465 0.5
0.3225 0.55
0.3764 0.6
0.4263 0.65
0.4562 0.7
0.4502 0.75
0.4499 0.8
0.4534 0.85
0.4416 0.9
0.4304 0.95
0.437 1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment