Finds python 算法简陋实现

finds.py

import numpy as np

class Finds:
    def __init__(self, samples, labels):
        positive_indices    = np.array([
            index for (index, label) in enumerate(labels)
            if label == 1
        ], dtype='int')
        self.samples    = samples[positive_indices, :]
        self.H          = [None] * samples.shape[1]
        print('initialization finished')
        print(self.samples)
        print(self.H)
        print()

    def train(self):
        for sample in self.samples:
            index = 0
            for _property in sample:
                if self.H[index] == None:
                    self.H[index] = _property
                elif _property != self.H[index]:
                    self.H[index] = '?'
                index += 1

        print('rule of judging is:', self.H)
    
    def predict(self, input):
        index = 0
        for _property in self.H:
            if input[index] == _property or _property == '?':
                index += 1
            else:
                break
        if index == len(self.H):
            print('True')
            return True
        else:
            print('False')
            return False

test_finds.py

import finds
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb

if __name__ == "__main__":
    samples = np.array([
        ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same'],
        ['Sunny', 'Warm', 'High',   'Strong', 'Warm', 'Same'],
        ['Rainy', 'Cold', 'High',   'Strong', 'Warm', 'Change'],
        ['Sunny', 'Warm', 'High',   'Strong', 'Cold', 'Change'],
    ], dtype='str')
    labels = [
        1, 1, 0, 1
    ]
    instance = finds.Finds(samples, labels)
    instance.train()

    instance.predict(samples[0])
    instance.predict(samples[2])

output

initialization finished
[['Sunny' 'Warm' 'Normal' 'Strong' 'Warm' 'Same']
 ['Sunny' 'Warm' 'High' 'Strong' 'Warm' 'Same']
 ['Sunny' 'Warm' 'High' 'Strong' 'Cold' 'Change']]
[None, None, None, None, None, None]

rule of judging is: ['Sunny', 'Warm', '?', 'Strong', '?', '?']
True
False
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值