def main(): iris = datasets.load_iris() x = iris.data[50:150] t = iris.target[50:150] for i in range(100): if t[i] == 2: t[i] = -1 skf = model_selection.StratifiedKFold(n_splits=10) avg_accuracy = 0 for train, test in skf.split(x, t): classifier = PerceptronClassifier(4, 0.1) classifier.learn(t[train], x[train]) accuracy = 0.0 for test_x, test_t in zip(x[test], t[test]): test_y = classifier.classify(test_x) if test_y == test_t: accuracy += 1 accuracy /= len(test) avg_accuracy += accuracy avg_accuracy /= 10 print 'average accuracy:', avg_accuracy * 100, '%'
class Perceptron: def __init__(self): iris = datasets.load_iris() x = iris.data[0:150] t = iris.target[0:150] for i in range(150): if t[i] == 0: t[i] = -1 if t[i] == 2: t[i] = 1 self.classifier = PerceptronClassifier(4, 0.1) self.classifier.learn(t, x) def predict(self, x): y = self.classifier.classify(x) if y == -1: return 'setosa' else: return None