回答編集履歴
1
追記
answer
CHANGED
@@ -4,4 +4,37 @@
|
|
4
4
|
|
5
5
|
決定係数R^2の計算はsklearnとかにあります。
|
6
6
|
|
7
|
-
[sklearn.metrics.r2_score — scikit-learn 0.19.2 documentation](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html)
|
7
|
+
[sklearn.metrics.r2_score — scikit-learn 0.19.2 documentation](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html)
|
8
|
+
|
9
|
+
### 追記
|
10
|
+
やってみました。
|
11
|
+
|
12
|
+
```python
|
13
|
+
import numpy as np
|
14
|
+
from scipy import stats
|
15
|
+
from scipy.optimize import curve_fit
|
16
|
+
import matplotlib.pyplot as plt
|
17
|
+
|
18
|
+
def cdf(x, a, b, c):
|
19
|
+
return stats.lognorm.cdf(x, a, b, c)
|
20
|
+
|
21
|
+
def main():
|
22
|
+
# xとyが実際に観測されたデータ
|
23
|
+
# ここでは擬似データとしてs=1,loc=2,scale=3のscipy.stats.lognorm.cdfを作る。ノイズを適当に加算している
|
24
|
+
x = np.linspace(0, 100, 200)
|
25
|
+
y = stats.lognorm.cdf(x, 1,2,3) + np.random.normal(size=x.shape)*0.02
|
26
|
+
|
27
|
+
param, cov = curve_fit(cdf, x, y) # 推定
|
28
|
+
y_true = cdf(x, 1, 2, 3)
|
29
|
+
y_pred = cdf(x, *param)
|
30
|
+
print(param) # => [0.96029726 1.90694707 3.08647047] くらい
|
31
|
+
plt.plot(x, y, label="観測値")
|
32
|
+
plt.plot(x, y_true, label="真値")
|
33
|
+
plt.plot(x, y_pred, linestyle="-.", label="フィッティング")
|
34
|
+
plt.legend()
|
35
|
+
plt.show()
|
36
|
+
|
37
|
+
if __name__ == "__main__":
|
38
|
+
main()
|
39
|
+
|
40
|
+
```
|