from network import Resnet, get_scheduler, init_net from dataload import loadData from Util import save_networks, load_networks, evaluate import torch import torch.nn as nn from torch.utils.tensorboard import SummaryWriter import torchvision if __name__ == '__main__': opt = gather_options() print_options(opt) device = torch.device('cuda:{}'.format( opt.gpu_ids[0])) if opt.gpu_ids else torch.device('cpu') trainloader, testloader = loadData(opt) dataset_size = len(trainloader) print('#training images = %d' % dataset_size) net = Resnet(opt.input_nc, num_classes=opt.num_classes, norm=opt.norm, nl=opt.nl) net = init_net(net, init_type='normal', gpu_ids=[0]) if opt.continue_train: load_networks(opt, net) criterion = nn.CrossEntropyLoss().to(device) optimizer = torch.optim.SGD(net.parameters(), lr=opt.lr, momentum=0.9) scheduler = get_scheduler(optimizer, opt)
value = self._classify(self._tree, X[i]) print str(i + 1) + "-th sample is classfied as:", value result.append(value) return np.array(result) def show(self, outpdf): if self._tree == None: pass # plot the tree using matplotlib import treePlotter treePlotter.createPlot(self._tree, outpdf) if __name__ == "__main__": trainfile = r"data\train.txt" testfile = r"data\test.txt" import sys sys.path.append(r"F:\CSU\Github\MachineLearning\lib") import dataload as dload train_x, train_y = dload.loadData(trainfile) test_x, test_y = dload.loadData(testfile) clf = DecitionTree(criteria="C4.5") clf.fit(train_x, train_y) result = clf.predict(test_x) outpdf = r"tree.pdf" clf.show(outpdf)
def calculateSFS(features_number): A, B = loadData(filePath) #wczytuje z podziałem na klasy, tworzę macierze z odpowiednim A = numpy.array(A, dtype=float) B = numpy.array(B, dtype=float) #dla klas tworzę macierz ze średnimi AMeans = numpy.mean(A, axis=0) BMeans = numpy.mean(B, axis=0) maxFisher = -1.0 bestIndexes = [] bestIndex = -1 NumberOfFeatures = features_number # dla jednej cechy for index in range(0, 64): #obliczam średnie AMean = AMeans[index] BMean = BMeans[index] #odejmuję średnie AValues = A[:, index] - AMean BValues = B[:, index] - BMean #obliczam odchylenie standardowe odchylenieA = math.sqrt((sum([i**2 for i in AValues])) / A.shape[0]) odchylenieB = math.sqrt((sum([i**2 for i in BValues])) / B.shape[0]) #oblicza współczynnik Fishera według wzoru fisher = abs(AMean - BMean) / (odchylenieA + odchylenieB) if fisher > maxFisher: maxFisher = fisher bestIndex = index bestIndexes.append(bestIndex) #przypadek wielu cech if (NumberOfFeatures > 1): while len(bestIndexes) < NumberOfFeatures: for index in range(0, 64): if index in bestIndexes: continue AMean = [] BMean = [] AValues = [] BValues = [] combinacjacech = bestIndexes.copy() combinacjacech.append(index) for cecha in combinacjacech: AValues.append(A[:, cecha] - AMeans[cecha]) BValues.append(B[:, cecha] - BMeans[cecha]) #licze srednia AMean.append(AMeans[cecha]) BMean.append(BMeans[cecha]) #liczę kowariancję, a następnie według wzoru wyliczam cechy ACovariance = (1 / A.shape[0]) * numpy.dot( numpy.array(AValues, dtype=float), numpy.transpose(numpy.array(AValues, dtype=float))) BCovariance = (1 / B.shape[0]) * numpy.dot( numpy.array(BValues, dtype=float), numpy.transpose(numpy.array(BValues, dtype=float))) fisher = numpy.linalg.norm(numpy.array(AMean, dtype=float) - numpy.array(BMean, dtype=float)) / \ numpy.linalg.det(ACovariance + BCovariance) if fisher > maxFisher: maxFisher = fisher bestIndex = index bestIndexes.append(bestIndex) return bestIndexes #print(calculateSFS(4))
else: result = [] for i in range(X.shape[0]): value = self._classify(self._tree, X[i]) print str(i + 1) + "-th sample is classfied as:", value result.append(value) return np.array(result) def show(self, outpdf): if self._tree == None: pass #plot the tree using matplotlib import treePlotter treePlotter.createPlot(self._tree, outpdf) if __name__ == "__main__": trainfile = r"data\train.txt" testfile = r"data\test.txt" import sys sys.path.append(r"F:\CSU\Github\MachineLearning\lib") import dataload as dload train_x, train_y = dload.loadData(trainfile) test_x, test_y = dload.loadData(testfile) clf = DecitionTree(criteria="C4.5") clf.fit(train_x, train_y) result = clf.predict(test_x) outpdf = r"tree.pdf" clf.show(outpdf)
import matplotlib.pyplot as plt import numpy as np import pandas as pd import apselect as aps import dataload as ld import devicediff as dif import gaprocess as ga import initsolution as inis import locselect as locs import settings as st import utils as ut # Load Data data, apMap = ld.loadData() # Calculate RSSI Gain devdiff = dif.calculateDeviceDiff(data) ut.saveNPtoFile( st.MIDFILE_DIR + "devicediff_%d_%d.txt" % (st.BUILDINGID, st.FLOORID), devdiff) # Simplify Data # selectedAP = aps.selectAPs(data) # selectedLoc = locs.selectLocs(data) # sData = data.values[selectedLoc, :][:, selectedAP] # msrInfo = data.values[selectedLoc, -9:] # mgData = np.hstack((sData, msrInfo)) # apMap = apMap[selectedAP] # ut.saveNPtoFile(st.MIDFILE_DIR+"simpledata_%d_%d.txt" %