# learnNoInputs.py - Learning ignoring all input features
# AIFCA Python3 code Version 0.9.3 Documentation at http://aipython.org
# Download the zip file and read aipython.pdf for documentation
# Artificial Intelligence: Foundations of Computational Agents http://artint.info
# Copyright David L Poole and Alan K Mackworth 2017-2021.
# This work is licensed under a Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# See: http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en
from learnProblem import squared_error, absolute_error, log_loss, mean
import math, random, statistics
import utilities # argmax for (element,value) pairs
class Predict(object):
"""The class of prediction methods for a list of numbers
Please make the doc strings the same length, because they are used in tables.
Note that we don't need self argument, as we are creating Predict objects,
To use call Predict.laplace(data) etc."""
def mean(data):
"mean "
return mean(data)
def bounded_mean(data, bound=0.01):
"bounded mean"
return min(max(mean(data),bound),1-bound)
def laplace(data):
"Laplace " # for Boolean (or 0/1 data only)
return mean(data, isum=1, icount=2)
def mode(data):
"mode "
counts = {}
for e in data:
if e in counts:
counts[e] += 1
else:
counts[e] = 1
return utilities.argmaxe(counts.items())
def median(data):
"median "
return statistics.median(data)
all = [mean, bounded_mean, laplace, mode, median]
def evaluate(train_size, predictor, error_measure, num_samples=10000, test_size=10 ):
"""return the average error when
train_size is the number of training examples
predictor(training) -> [0,1]
error_measure(prediction,actual) -> non-negative reals
"""
error = 0
for sample in range(num_samples):
prob = random.random()
training = [1 if random.random()