異常値を赤いラインでレベルの区切りを行っています。
# -*- coding: utf-8 -*-
import pandas as pd
from matplotlib import pyplot as plt
from scipy.stats import chi2
from matplotlib import pyplot as plt
from scipy.stats import chi2
df = pd.read_csv("Davis.csv")
# df.describe()
# df.describe()
# 体重のヒストグラム
plt.rcParams["font.size"]= 24
plt.hist(df["weight"], normed=True)
plt.title("体重/度数 棒グラフ")
plt.xlabel("体重")
plt.ylabel("度数")
plt.grid(True)
plt.show()
plt.rcParams["font.size"]= 24
plt.hist(df["weight"], normed=True)
plt.title("体重/度数 棒グラフ")
plt.xlabel("体重")
plt.ylabel("度数")
plt.grid(True)
plt.show()
# 2.2.4 ホテリングT2法(1次元)時間的推移がない場合有効
mu = df["weight"].mean() # 平均値μ
s2 = ((df["weight"] - mu)**2).mean() # 標準偏差σの2乗
mu = df["weight"].mean() # 平均値μ
s2 = ((df["weight"] - mu)**2).mean() # 標準偏差σの2乗
print("標本平均: {0}".format(mu)) # sample mean
print("標本分散: {0}".format(s2)) # sample variance
print("標本分散: {0}".format(s2)) # sample variance
a = (df["weight"] - mu)**2 / s2 # 異常度 a(x') = [(x' - μ)/σ]^2
print("異常度: {0}".format(a)) # anomaly score
print("異常度: {0}".format(a)) # anomaly score
th = chi2.isf(1 - 0.99, 1)
print("カイ2乗分布による1%水準の閾値: {0}".format(a)) # threshold
print("カイ2乗分布による1%水準の閾値: {0}".format(a)) # threshold
# 異常度をプロット
plt.rcParams["font.size"]= 24
plt.plot(range(200), a, "bo")
plt.axhline(y=th,color='red')
plt.rcParams["font.size"]= 24
plt.plot(range(200), a, "bo")
plt.axhline(y=th,color='red')
plt.text(xmin+90,8.0+0.2,'閾値',fontsize=30, color='r', ha='left',va='bottom')
plt.text(xmin+20,13+0.2,'119kg',fontsize=30, color='r', ha='left',va='bottom')
plt.text(xmin+12,43+0.2,'166kg',fontsize=30, color='r', ha='left',va='bottom')
plt.grid(True)
plt.show()
plt.text(xmin+20,13+0.2,'119kg',fontsize=30, color='r', ha='left',va='bottom')
plt.text(xmin+12,43+0.2,'166kg',fontsize=30, color='r', ha='left',va='bottom')
plt.grid(True)
plt.show()
=============================================================================
25 1.101094
26 0.034580
27 0.268349
28 3.167960
29 5.465067
...
170 0.458892
171 0.614150
172 0.101623
173 1.157551
174 0.063691
175 0.119266
176 0.148377
177 0.000176
178 1.019054
179 0.021348
180 0.889379
181 2.292872
182 1.157551
183 0.034580
184 0.077805
185 0.423606
186 0.148377
187 0.268349
188 0.458892
189 1.101094
190 2.173783
191 2.374030
192 0.203952
193 0.966126
194 0.063691
195 0.296577
196 1.304869
197 1.019054
198 2.583098
199 0.768525
26 0.034580
27 0.268349
28 3.167960
29 5.465067
...
170 0.458892
171 0.614150
172 0.101623
173 1.157551
174 0.063691
175 0.119266
176 0.148377
177 0.000176
178 1.019054
179 0.021348
180 0.889379
181 2.292872
182 1.157551
183 0.034580
184 0.077805
185 0.423606
186 0.148377
187 0.268349
188 0.458892
189 1.101094
190 2.173783
191 2.374030
192 0.203952
193 0.966126
194 0.063691
195 0.296577
196 1.304869
197 1.019054
198 2.583098
199 0.768525

