from sklearn import svm, metrics

import random
import re


def split(rows):

    data = []
    labels = []

    for row in rows:

        data.append(row[0:4])
        labels.append(row[4])

    return (data, labels)


def calculate_score(train, test):

    train_data, train_label = split(train)
    test_data, test_label = split(test)

    classifier = svm.SVC()
    classifier.fit(train_data, train_label)

    predict = classifier.predict(test_data)
    return metrics.accuracy_score(test_label, predict)


def to_number(n):
    return float(n) if re.match(r"^[0-9\.]+$", n) else n


def to_columm(line):
    return list(map(to_number, line.strip().split(",")))


lines = open("iris.csv", "r", encoding="utf-8").read().split("\n")

csv = list(map(to_columm, lines))
del csv[0]

random.shuffle(csv)

k = 5

csv_k = [[] for i in range(k)]
scores = []

for i in range(len(csv)):
    csv_k[i % k].append(csv[i])

for test in csv_k:

    train = []

    for data in csv_k:

        if test != data:
            train += data

    score = calculate_score(train, test)
    scores.append(score)

print("score = ", scores)
print("avg = ", sum(scores) / len(scores))

[((45, 0, 45, 19), 'random.shuffle', 'random.shuffle', ({(45, 15, 45, 18): 'csv'}, {}), '(csv)', False, 'import random\n'), ((25, 17, 25, 26), 'sklearn.svm.SVC', 'svm.SVC', ({}, {}), '()', False, 'from sklearn import svm, metrics\n'), ((29, 11, 29, 54), 'sklearn.metrics.accuracy_score', 'metrics.accuracy_score', ({(29, 34, 29, 44): 'test_label', (29, 46, 29, 53): 'predict'}, {}), '(test_label, predict)', False, 'from sklearn import svm, metrics\n'), ((33, 23, 33, 49), 're.match', 're.match', ({(33, 32, 33, 45): '"""^[0-9\\\\.]+$"""', (33, 47, 33, 48): 'n'}, {}), "('^[0-9\\\\.]+$', n)", False, 'import re\n')]