Ejemplo n.º 1
0
def analyzeId(c_id):
    startTime = time.time()
    count2 =0
    itr = 0
    if (c_id>0):
        network = NeuralNetwork(inputNodes = 5, hiddenNodes = 18, outputNodes = 1) #company id >0 for individual prediciton
        averageError = 1      
        while (averageError>0.01 and itr<2000):
        #for i in range(1000):
            count1 =0
            resultBackProp =[]
            while (count1<295):
                trainingData = getTrainingData(c_id,count1)
                resultBackProp.append(network.train(trainingData))
                count1 = count1 +1  
            averageError = sum(resultBackProp)/len(resultBackProp)
        print ("avgError = ", averageError)

        #==================================================================    
        # get data for testing
        
        testError =[]
        while (count2<95):
            testData = getTestData(c_id,count2)
            testError.append(network.accuracyTest(testData))
            count2 = count2 + 1
        
        testErrorAverage = sum(testError)/len(testError)
        
        #====================================================================

        # get data for tomorrow's prediction
        predictionData = getPredictionData(c_id)

        # get prediction result
        returnPrice = network.test(predictionData)

        # de-normalize and return predicted stock price
        final = InitialCalculation()
        predictedStockPrice = final.denormalizePrice(returnPrice)

        # create return object, including the accuracy of the testing
        returnData ={}
        returnData['price'] = predictedStockPrice
        #returnData['accuracy'] = accuracy
        returnData['error'] = testErrorAverage
        returnData['time'] = time.time() - startTime
        print(returnData)

    else:
        nep_network = NeuralNetwork(inputNodes = 8, hiddenNodes = 20, outputNodes = 1) #company id =0 for nepse prediciton
        averageError = 1      
        while (averageError>0.03 ):
        #for i in range(1000):
            count1 =0
            resultBackProp =[]
            #print(getTrainingData(c_id,count1))
            while (count1<95):
                trainingData = getTrainingData(c_id,count1)
                resultBackProp.append(nep_network.trainNepse(trainingData))
                count1 = count1 +1  
            averageError = sum(resultBackProp)/len(resultBackProp)
        print ("avgError = ", averageError)

        #==================================================================    
        # get data for testing
        # considering 50 test data, we nedd 45 loops 
        testError =[]
        while (count2<10):
            testData = getTestData(c_id,count2)
            testError.append(nep_network.accuracyTestNepse(testData))
            count2 = count2 + 1
        testErrorAverage = sum(testError)/len(testError)
        #print ("test error =", testError)
        #print("te average = ", testErrorAverage)
        #accuracy = (1- testErrorAverage)*100
        #====================================================================

        # get data for tomorrow's prediction
        predictionData = getPredictionData(c_id)

        # get prediction result
        returnPrice = nep_network.test(predictionData)

        # de-normalize and return predicted stock price
        final = InitialCalculation()
        predictedStockPrice = final.denormalizePriceNepse(returnPrice)

        # create return object, including the accuracy of the testing
        returnData ={}
        returnData['price'] = predictedStockPrice
        #returnData['accuracy'] = accuracy
        returnData['error'] = testErrorAverage
        returnData['time'] = time.time() - startTime
        print(returnData)