import random
def calc_bmi(h, w):
bmi = w / (h / 100) ** 2
if bmi < 18.5: return "thin"
if bmi < 25: return "normal"
return "fat"
fp = open("bmi.csv", "w", encoding="utf-8")
fp.write("height, weight, label\r\n")
cnt = {"thin":0, "normal":0, "fat":0}
for i in range(20000):
h = random.randint(120, 200)
w = random.randint(35, 80)
label = calc_bmi(h, w)
cnt[label] += 1
fp.write("{0},{1},{2}\r\n".format(h, w, label))
fp.close()
print("ok", cnt)
[/python]
[python]
from sklearn import cross_validation, svm, metrics
import matplotlib.pyplot as plt
import pandas as pd
tbl = pd.read_csv("bmi.csv")
label = tbl["label"]
w = tbl["weight"] / 100
h = tbl["height"] / 200
wh = pd.concat([w, h], axis=1)
data_train, data_test, label_train, label_test = \
cross_validation.train_test_split(wh, label)
clf = svm.SVC()
clf.fit(data_train, label_train)
predict = clf.predict(data_test)
ac_score = metrics.accuracy_score(label_test, predict)
cl_report = metrics.classification_report(label_test, predict)
print("正解率=", ac_score)
print("レポート=\n", cl_report)
[/python]
[python]
import matplotlib.pyplot as plt
import pandas as pd
tbl = pd.read_csv("bmi.csv", index_col=2)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
def scatter(lbl, color):
b = tbl.loc[lbl]
ax.scatter(b["weight"],b["height"], c=color, label=lbl)
scatter("fat", "red")
scatter("normal", "yellow")
scatter("thin", "purple")
ax.legend()
plt.savefig("bmi-test.png")
[/python]