def main(): np.random.seed(100) X, Y_ = data.sample_gauss_2d(2, 100) w, b = binlogreg_train(X, Y_) probabilities = binlogreg_classify(X, w,b) Y = np.where(probabilities >= .5, 1, 0) accuracy, recall, precision = data.eval_perf_binary(Y, Y_) AP = data.eval_AP(Y_[probabilities.argsort()]) print('Acc: {0}\nRecall: {1}\nPrecision: {2}\nAP: {3}\n'.format(accuracy, recall, precision, AP))
return w,b ''' Arguments X: data, np.array NxD w, b: logistic regression parameters Return values probs: a posteriori probabilities for c1, dimensions Nx1 ''' def binlogreg_classify(X,w,b): return data.sigmoid(np.dot(X, w) + b) np.random.seed(100) X,Y_ = data.sample_gauss_2d(2, 100) w,b = binlogreg_train(X, Y_, param_niter=0) probs = binlogreg_classify(X, w,b) Y = [] for prob in probs: if prob < 0.5: Y.append(False) else: Y.append(True) accuracy, recall, precision = data.eval_perf_binary(Y, Y_) AP = data.eval_AP(Y_[probs.argsort()]) #print (accuracy, recall, precision, AP)
# train the model w,b = binlogreg_train(X,Y_) # evaluate the model n the training dataset probs = binlogreg_classify(X,w,b) print(probs) #predicted labels Y = np.vstack([ 0 if (probs[i]<0.5) else 1 for i in range(Y_.shape[0])]) #evaluation metrics accuracy, recall, precision = data.eval_perf_binary(Y, Y_) print("Accuracy: ",accuracy) print("Precision: ",precision) print("Recall: ",recall) AP = data.eval_AP(np.vstack(Y_[probs.reshape(1,200).argsort()])) print("Average precision: ",AP) #graph the decision surface decfun = binlogreg_decfun(X,w,b) bbox=(np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(decfun, bbox, offset=0.5) # graph the data points data.graph_data(X, np.hstack(Y_), np.hstack(Y), special=[]) # show the plot plt.show()
if __name__ == "__main__": np.random.seed(100) # get the training dataset X, Y_ = data.sample_gauss(2, 20) # train the model w, b = binlogreg_train(X, Y_) # evaluate the model on the training dataset probs = np.array(binlogreg_classify(X, w, b)) Y = np.array([1 if p > 0.5 else 0 for p in probs]) # report performance accuracy, recall, precision = data.eval_perf_binary(Y, Y_) AP = data.eval_AP(Y_[np.argsort(probs)]) print(accuracy, recall, precision, AP) # graph the decision surface decfun = binlogreg_decfun(w, b) bbox = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(decfun, bbox, offset=0.5) # graph the data points #data.graph_data(X, Y_, Y, special=[]) # show the plot plt.show()
if __name__ == "__main__": # inicijaliziraj generatore slučajnih brojeva np.random.seed(100) tf.set_random_seed(100) # instanciraj podatke X i labele Yoh_ X, Y_ = data.sample_gmm_2d(6, 2, 10) Yoh_ = data.class_to_onehot(Y_) # izgradi graf: tflr = TFLogreg(X.shape[1], Yoh_.shape[1], 0.06, 1) # nauči parametre: tflr.train(X, Yoh_, 1000) # dohvati vjerojatnosti na skupu za učenje probs = tflr.eval(X) Y = np.argmax(probs, axis=1) # ispiši performansu (preciznost i odziv po razredima) accuracy, recall, precision = data.eval_perf_multi(Y, Y_) AP = data.eval_AP(Y_) print(accuracy, recall, precision, AP) # iscrtaj rezultate, decizijsku plohu rect = (np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(calc_class, rect, offset=0.5) data.graph_data(X, Y_, Y, special=[]) plt.show()
def classify(X): return logreg_classify(X, w, b) return classify if __name__=="__main__": np.random.seed(100) # get data # X,Y_ = sample_gmm_2d(4, 2, 30) X,Y_ = data.sample_gauss_2d(3, 100) weights, bias = logreg_train(X,Y_) # get the class predictions probs = logreg_classify(X, weights, bias) Y = np.argmax(probs, axis=1) # Y = myDummyDecision(X)>0.5 accuracy, recall, precision = data.eval_perf_multi(Y, Y_) AP = data.eval_AP(Y_[Y.argsort()]) print (accuracy, recall, precision, AP) # graph the decision surface decfun = logreg_decfun(weights, bias) rect=(np.min(X, axis=0), np.max(X, axis=0)) data.graph_surface(decfun, rect, offset=0.5) # graph the data points data.graph_data(X, Y_, Y, special=[]) plt.show()