
本文共 4611 字,大约阅读时间需要 15 分钟。
���������������������������������
������������������������ ����328 Pilaf ���������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������
���������������������������������������������������IPython���scikit-learn������
���������������������IPython Notebook������������������������������������������������������IPython��������� notebooks ���������������������������������������������������������������������������������������
pip install ipython[all]ipython3 notebook
������IPython Notebook������������������������������������������������������������������������������������������������������������Notebook������������������ ctrl+c ������������
������������������������scikit-learn������������������������������������������������������������������������������������������������������������������������������������������������
pip3 install -U scikit-learn
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
1. ���������������
���������������������������
import numpy as npfilename = 'affinity_dataset.txt'x = np.loadtxt(filename)n_samples, n_features = x.shapeprint("��������������� {} ������������ {} ���������".format(n_samples, n_features))
���������������������������������������5������������������������
[1, 0, 1, 1, 1][1, 1, 0, 1, 0][0, 1, 1, 0, 1][1, 0, 1, 1, 0][1, 1, 1, 1, 0]
2. ������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
num_apple = 0num_banana = 0for sample in x: if sample[3] == 1: # ������������������ num_apple += 1 if sample[4] == 1: # ������������������ num_banana += 1print("������������������������{}", num_apple)print("������������������������{}", num_banana)
������������������������������������������������������������������������������������������������������
from collections import defaultdictvalid_rules = defaultdict(int)invalid_rules = defaultdict(int)num_occurances = defaultdict(int)for sample in x: for premise in range(n_features): num_occurances[premise] += 1 if sample[premise] == 0: continue for conclusion in range(n_features): if premise == conclusion: continue if sample[conclusion] == 1: valid_rules[(premise, conclusion)] += 1 else: invalid_rules[(premise, conclusion)] += 1support = valid_rulesconfidence = defaultdict(float)for premise, conclusion in valid_rules.keys(): confidence[premise, conclusion] = support[(premise, conclusion)] / num_occurances[premise]
3. ������������
���������������������������������������������������������������������������������������������������������������������������������������������������
from operator import itemgeter# ������������������sorted_support = sorted(support.items(), key=itemgeter(1), reverse=True)# ������������������sorted_confidence = sorted(confidence.items(), key=itemgeter(1), reverse=True)
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
4. ������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
