1. 随机预测算法

# Generate random predictions

def random_algorithm(train, test):

output_values = [row[-1] for row in train]

unique = list(set(output_values))

predicted = list()

for row in test:

index = randrange(len(unique))

predicted.append(unique[index])

return predicted

from random import seed

from random import randrange

# Generate random predictions

def random_algorithm(train, test):

output_values = [row[-1] for row in train]

unique = list(set(output_values))

predicted = list()

for row in test:

index = randrange(len(unique))

predicted.append(unique[index])

return predicted

seed(1)

train = [[0], [1], [0], [1], [0], [1]] test = [[None], [None], [None], [None]]

predictions = random_algorithm(train, test)

print(predictions)

[0, 1, 1, 0]

2. 零规则算法

= ((0.9 * 0.9) + (0.1 * 0.1)) * 100

= 82%

# zero rule algorithm for classification

def zero_rule_algorithm_classification(train, test):

output_values = [row[-1] for row in train]

prediction = max(set(output_values), key=output_values.count)

predicted = [prediction for i in range(len(test))]

return predicted

from random import seed

from random import randrange

# zero rule algorithm for classification

def zero_rule_algorithm_classification(train, test):

output_values = [row[-1] for row in train]

prediction = max(set(output_values), key=output_values.count)

predicted = [prediction for i in range(len(train))]

return predicted

seed(1)

train = [['0'], ['0'], ['0'], ['0'], ['1'], ['1']] test = [[None], [None], [None], [None]]

predictions = zero_rule_algorithm_classification(train, test)

print(predictions)

['0', '0', '0', '0', '0', '0']

mean = sum(value) / total values

from random import randrange

# zero rule algorithm for regression

def zero_rule_algorithm_regression(train, test):

output_values = [row[-1] for row in train]

prediction = sum(output_values) / float(len(output_values))

predicted = [prediction for i in range(len(test))]

return predicted

10

15

12

15

18

20

mean = (10 + 15 + 12 + 15 + 18 + 20) / 6

mean = 90 / 6

mean = 15

from random import seed

from random import randrange

# zero rule algorithm for regression

def zero_rule_algorithm_regression(train, test):

output_values = [row[-1] for row in train]

prediction = sum(output_values) / float(len(output_values))

predicted = [prediction for i in range(len(test))]

return predicted

seed(1)

train = [[10], [15], [12], [15], [18], [20]] test = [[None], [None], [None], [None]]

predictions = zero_rule_algorithm_regression(train, test)

print(predictions)

[15.0, 15.0, 15.0, 15.0, 15.0, 15.0] 扩展

tags: 算法,train,values,output,algorithm,random,rule,zero,predicted,row,prediction

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。