def setUp(self): logging.basicConfig(stream=sys.stdout, level=logging.INFO) dataDir = PathDefaults.getDataDir() + "infoDiffusion/" #dataDir = "/home/charanpal/Private/Postdoc/Code/APGL/data/" matFileName = dataDir + "EgoAlterTransmissions.mat" self.svmEgoSimulator = SvmEgoSimulator(matFileName)
class SvmEgoSimulatorTest(unittest.TestCase): def setUp(self): logging.basicConfig(stream=sys.stdout, level=logging.INFO) dataDir = PathDefaults.getDataDir() + "infoDiffusion/" #dataDir = "/home/charanpal/Private/Postdoc/Code/APGL/data/" matFileName = dataDir + "EgoAlterTransmissions.mat" self.svmEgoSimulator = SvmEgoSimulator(matFileName) def testSampleExamples(self): self.svmEgoSimulator.sampleExamples(100) self.assertEquals(self.svmEgoSimulator.examplesList.getNumSampledExamples(), 100) def testModelSelection(self): Cs = [1.0, 2.0] kernel = "linear" kernelParams = [0.0] errorCosts = [0.1, 0.2] folds = 5 sampleSize = 1000 CVal, kernelParamVal, errorCost, error = self.svmEgoSimulator.modelSelection(Cs, kernel, kernelParams, errorCosts, folds, sampleSize) self.assertTrue(CVal in Cs) self.assertTrue(kernelParamVal in kernelParams) self.assertTrue(errorCost in errorCosts) self.assertTrue(error >= 0.0 and error < 1.0) def testEvaluateClassifier(self): CVal = 1.0 kernel = "linear" kernelParamVal = 0.0 errorCost = 0.5 folds = 6 sampleSize = 1000 invert = False (means, vars) = self.svmEgoSimulator.evaluateClassifier(CVal, kernel, kernelParamVal, errorCost, folds, sampleSize, invert) def testTrainClassifier(self): CVal = 1.0 kernel = "linear" kernelParamVal = 0.0 errorCost = 0.5 folds = 6 sampleSize = 1000 self.svmEgoSimulator.trainClassifier(CVal, kernel, kernelParamVal, errorCost, sampleSize) def testGenerateRandomGraph(self): egoFileName = PathDefaults.getDataDir() + "infoDiffusion/EgoData.csv" alterFileName = PathDefaults.getDataDir() + "infoDiffusion/AlterData.csv" numVertices = 1000 infoProb = 0.1 p = 0.1 neighbours = 10 generator = SmallWorldGenerator(p, neighbours) graph = SparseGraph(VertexList(numVertices, 0)) graph = generator.generate(graph) self.svmEgoSimulator.generateRandomGraph(egoFileName, alterFileName, infoProb, graph) def testRunSimulation(self): egoFileName = PathDefaults.getDataDir() + "infoDiffusion/EgoData.csv" alterFileName = PathDefaults.getDataDir() + "infoDiffusion/AlterData.csv" numVertices = 1000 infoProb = 0.1 p = 0.1 neighbours = 10 generator = SmallWorldGenerator(p, neighbours) graph = SparseGraph(VertexList(numVertices, 0)) graph = generator.generate(graph) CVal = 1.0 kernel = "linear" kernelParamVal = 0.0 errorCost = 0.5 folds = 6 sampleSize = 1000 maxIterations = 5 self.svmEgoSimulator.trainClassifier(CVal, kernel, kernelParamVal, errorCost, sampleSize) self.svmEgoSimulator.generateRandomGraph(egoFileName, alterFileName, infoProb, graph) self.svmEgoSimulator.runSimulation(maxIterations)
import logging import sys import numpy import random from apgl.egograph.InfoExperiment import InfoExperiment from apgl.egograph.SvmEgoSimulator import SvmEgoSimulator logging.basicConfig(stream=sys.stdout, level=logging.INFO) numpy.random.seed(21) random.seed(21) examplesFileName = SvmInfoExperiment.getExamplesFileName() folds = 5 cvSampleSize = SvmInfoExperiment.getNumCVExamples() sampleSize = 75000 svmEgoSimulator = SvmEgoSimulator(examplesFileName) classifier = svmEgoSimulator.getClassifier() #classifier.setTermination(0.1) C, kernel, kernelParamVal, errorCost = SvmInfoExperiment.loadSvmParams(SvmInfoExperiment.getSvmParamsFileName()) logging.info("Training SVM with C=" + str(C) + ", " + kernel + " kernel" + ", param=" + str(kernelParamVal) + ", sampleSize=" + str(sampleSize) + ", errorCost="+str(errorCost)) svmEgoSimulator.sampleExamples(cvSampleSize) svmEgoSimulator.evaluateClassifier(C, kernel, kernelParamVal, errorCost, folds, sampleSize, True)
examplesFileName = SvmInfoExperiment.getExamplesFileName() egoFileName = "../../data/EgoData.csv" alterFileName = "../../data/AlterData.csv" numVertices = SvmInfoExperiment.getNumVertices() numVertices = 10000 infoProb = 0.1 graphTypes = ["SmallWorld", "ErdosRenyi"] ps = [0.1, 0.003] ks = [15, 15] svmParamsFile = SvmInfoExperiment.getSvmParamsFileName() sampleSize = SvmInfoExperiment.getNumSimulationExamples() simulator = SvmEgoSimulator(examplesFileName) CVal, kernel, kernelParamVal, errorCost = SvmInfoExperiment.loadSvmParams(svmParamsFile) classifier = simulator.trainClassifier(CVal, kernel, kernelParamVal, errorCost, sampleSize) preprocessor = simulator.getPreprocessor() maxIterations = 5 eCsvReader = EgoCsvReader() egoQuestionIds = eCsvReader.getEgoQuestionIds() pajekWriter = PajekWriter() simpleGraphWriter = SimpleGraphWriter() vertexWriter = CsvVertexWriter() for i in range(len(graphTypes)): graphType = graphTypes[i]
import sys import numpy import random from apgl.egograph.InfoExperiment import InfoExperiment from apgl.egograph.SvmEgoSimulator import SvmEgoSimulator from apgl.io.EgoCsvReader import EgoCsvReader logging.basicConfig(stream=sys.stdout, level=logging.INFO) numpy.random.seed(21) random.seed(21) examplesFileName = SvmInfoExperiment.getExamplesFileName() sampleSize = 86755 svmEgoSimulator = SvmEgoSimulator(examplesFileName) preprocessor = svmEgoSimulator.getPreProcessor() centerValues = preprocessor.getCentreVector() svmParamsFileName = SvmInfoExperiment.getSvmParamsFileName() + "Linear.mat" logging.info("Using SVM params from file " + svmParamsFileName) C, kernel, kernelParamVal, errorCost = SvmInfoExperiment.loadSvmParams(svmParamsFileName) svmEgoSimulator.trainClassifier(C, kernel, kernelParamVal, errorCost, sampleSize) weights, b = svmEgoSimulator.getWeights() numpy.set_printoptions(precision=3) #Print the weights then their sorted values by indices and then value sortedWeightsInds = numpy.flipud(numpy.argsort(abs(weights)))
k = 15 generator = SmallWorldGenerator(p, k) """ #A second set of parameters p = float(30)/numVertices generator = ErdosRenyiGenerator(p) simulationRepetitions = 5 maxIterations = 3 sampleSize = SvmInfoExperiment.getNumSimulationExamples() #sampleSize = 1000 svmParamsFile = SvmInfoExperiment.getSvmParamsFileName() CVal, kernel, kernelParamVal, errorCost = SvmInfoExperiment.loadSvmParams(svmParamsFile) simulator = SvmEgoSimulator(examplesFileName) simulator.trainClassifier(CVal, kernel, kernelParamVal, errorCost, sampleSize) egoCsvReader = EgoCsvReader() genderIndex = egoCsvReader.genderIndex ageIndex = egoCsvReader.ageIndex incomeIndex = egoCsvReader.incomeIndex townSizeIndex = egoCsvReader.townSizeIndex foodRiskIndex = egoCsvReader.foodRiskIndex experienceIndex = egoCsvReader.experienceIndex internetFreqIndex = egoCsvReader.internetFreqIndex peopleAtWorkIndex = egoCsvReader.peopleAtWorkIndex educationIndex = egoCsvReader.educationIndex professionIndices = numpy.zeros(egoCsvReader.numProfessions)