# -*- coding: utf-8 -*- """ Created on Wed Nov 15 08:18:35 2017 @author: Miguel Almeida """ import numpy as np from sklearn.tree import DecisionTreeClassifier import matplotlib.pyplot as plt def computeError(hyp, hyps_ws, features, classes): net_pred = np.zeros(size) preds = 0 for ix in range(len(hyps)): pred_n = hyps[ix].predict(features) preds = preds+pred_n*hyps_ws[ix] net_pred[preds<0]=-1 net_pred[preds>=0]=1 errors = np.sum((net_pred != classes).astype(int)) return errors matrix = np.loadtxt("T6-data.txt", delimiter ="\t") #shuffle np.random.shuffle(matrix) #normalizar os dados means = np.mean(matrix[:,:-1], axis=0) devs = np.std(matrix[:,:-1], axis=0) matrix[:,:2] = (matrix[:,:-1]-means)/devs #split data do train classes = matrix[:,-1]*2-1 features = matrix[:,:-1] weigths = np.ones([features.shape[0]]) size = features.shape[0] hyps = [] hyps_ws = [] for ix in range(size): weigths[ix]= 1/float(size) max_hyp= 20 nerrs = [] #Computar o erro for ix in range(max_hyp): stump = DecisionTreeClassifier(max_depth=1) stump.fit(features,classes,sample_weight=weigths) pred = stump.predict(features) errs = (pred != classes).astype(int) err = np.sum(errs*weigths) alpha = np.log((1-err)/err) weigths = weigths*np.exp(alpha*errs) weigths = weigths/np.sum(weigths) hyps.append(stump) hyps_ws.append(alpha) nerrs.append(computeError(hyps, hyps_ws, features, classes)) fig = plt.plot(range(1, max_hyp +1), nerrs, color="red", linewidth=1.0)