def get_daal_prediction(x=np.arange(10).reshape(10,1), y=np.arange(10).reshape(10,1)):

        ntX = HomogenNumericTable(x)
        ntY = HomogenNumericTable(y)

        ridge_training_algorithm = ridge_training.Batch()
        ridge_training_algorithm.input.set(ridge_training.data, ntX)
        ridge_training_algorithm.input.set(ridge_training.dependentVariables, ntY)

        # set parameter
        alpha = 0.0
        alpha_nt = HomogenNumericTable(np.array([alpha], ndmin=2))
        ridge_training_algorithm.parameter.ridgeParameters = alpha_nt

        result = ridge_training_algorithm.compute()
        model = result.get(ridge_training.model)

        ridge_prediction_algorithm = ridge_prediction.Batch()
        ridge_prediction_algorithm.input.setModel(ridge_prediction.model, model)
        ridge_prediction_algorithm.input.setTable(ridge_prediction.data, ntX)
        result = ridge_prediction_algorithm.compute()

        np_predicted = getNumpyArray(result.get(ridge_prediction.prediction))
        # assert the same as the initial dependent variable
        assert_array_almost_equal(y, np_predicted)
        return np_predicted
def testModel(trainingResult):

    # Initialize FileDataSource to retrieve the input data from a .csv file
    testDataSource = FileDataSource(testDatasetFileName,
                                    DataSourceIface.doAllocateNumericTable,
                                    DataSourceIface.doDictionaryFromContext)

    # Create Numeric Tables for testing data and ground truth values
    testData = HomogenNumericTable(NUM_FEATURES, 0,
                                   NumericTableIface.doNotAllocate)
    testGroundTruth = HomogenNumericTable(NUM_DEPENDENT_VARS, 0,
                                          NumericTableIface.doNotAllocate)
    mergedData = MergedNumericTable(testData, testGroundTruth)

    # Retrieve the data from an input file
    testDataSource.loadDataBlock(mergedData)

    # Create an algorithm object to predict values of ridge regression
    algorithm = prediction.Batch()

    # Pass a testing data set and the trained model to the algorithm
    algorithm.input.setTable(prediction.data, testData)
    algorithm.input.setModel(prediction.model,
                             trainingResult.get(training.model))

    # Predict values of ridge regression
    res = algorithm.compute()

    # Retrieve the algorithm results
    printNumericTable(res.get(prediction.prediction),
                      "Ridge Regression prediction results: (first 10 rows):",
                      10)
    printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
Example #3
0
    def predict(self, trainingResult, testData):
        algorithm = prediction.Batch(fptype=self.dtype)
        # Pass a testing data set and the trained model to the algorithm
        algorithm.input.setTable(prediction.data, testData)
        algorithm.input.setModel(prediction.model,
                                 trainingResult.get(training.model))

        # Predict values of multiple Ridge regression and retrieve the algorithm results
        predictionResult = algorithm.compute()
        return (predictionResult.get(prediction.prediction))
def testModel(testData, model):

    # Create algorithm objects to predict values of multiple linear regression with the default method
    ridgeRegressionPredict = prediction.Batch()

    # Pass the test data to the algorithm
    parts_list = testData.collect()
    for key, (h_table1, _) in parts_list:
        deserialized_h_table1 = deserializeNumericTable(h_table1)
        ridgeRegressionPredict.input.setTable(prediction.data,
                                              deserialized_h_table1)

    ridgeRegressionPredict.input.setModel(prediction.model, model)

    # Compute and retrieve the prediction results
    predictionResult = ridgeRegressionPredict.compute()

    return predictionResult.get(prediction.prediction)
Example #5
0
    def predict(self, X):
        '''
        Make prediction for X - unseen data using a trained model
        :param X:new data
        intercept: from parameters, a boolean indicating
        if calculate Beta0 (intercept)
        '''

        Data = IInput.HomogenousDaalData(X).getNumericTable()
        ridge_prediction_algorithm = \
            ridge_prediction.Batch()
        # set input
        ridge_prediction_algorithm.input.setModel(ridge_prediction.model,
                                                  self.model)
        ridge_prediction_algorithm.input.setTable(ridge_prediction.data, Data)

        if 'intercept' in self.parameters:
            beta_coeff = self.get_beta()
            np_beta = getNumpyArray(beta_coeff)
            self.intercept_ = [np_beta[0, 0]]
        # calculate
        res = ridge_prediction_algorithm.compute()
        return getNumpyArray(res.get(ridge_prediction.prediction))