回答編集履歴

1

追記

2018/09/21 11:37

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -11,3 +11,69 @@
11
11
 
12
12
 
13
13
  [sklearn.metrics.r2_score — scikit-learn 0.19.2 documentation](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html)
14
+
15
+
16
+
17
+ ### 追記
18
+
19
+ やってみました。
20
+
21
+
22
+
23
+ ```python
24
+
25
+ import numpy as np
26
+
27
+ from scipy import stats
28
+
29
+ from scipy.optimize import curve_fit
30
+
31
+ import matplotlib.pyplot as plt
32
+
33
+
34
+
35
+ def cdf(x, a, b, c):
36
+
37
+ return stats.lognorm.cdf(x, a, b, c)
38
+
39
+
40
+
41
+ def main():
42
+
43
+ # xとyが実際に観測されたデータ
44
+
45
+ # ここでは擬似データとしてs=1,loc=2,scale=3のscipy.stats.lognorm.cdfを作る。ノイズを適当に加算している
46
+
47
+ x = np.linspace(0, 100, 200)
48
+
49
+ y = stats.lognorm.cdf(x, 1,2,3) + np.random.normal(size=x.shape)*0.02
50
+
51
+
52
+
53
+ param, cov = curve_fit(cdf, x, y) # 推定
54
+
55
+ y_true = cdf(x, 1, 2, 3)
56
+
57
+ y_pred = cdf(x, *param)
58
+
59
+ print(param) # => [0.96029726 1.90694707 3.08647047] くらい
60
+
61
+ plt.plot(x, y, label="観測値")
62
+
63
+ plt.plot(x, y_true, label="真値")
64
+
65
+ plt.plot(x, y_pred, linestyle="-.", label="フィッティング")
66
+
67
+ plt.legend()
68
+
69
+ plt.show()
70
+
71
+
72
+
73
+ if __name__ == "__main__":
74
+
75
+ main()
76
+
77
+
78
+
79
+ ```