def test_linear_regression_simple(): # calculate beta coefficients x = np.array([0., 2., 3.]).reshape(3, 1) nt_x = nt_y = HomogenNumericTable(x) lr_alg = linear_training.Batch(method=linear_training.qrDense) lr_alg.input.set(linear_training.data, nt_x) lr_alg.input.set(linear_training.dependentVariables, nt_y) result = lr_alg.compute() model = result.get(linear_training.model) beta_coeff = model.getBeta() np_beta_coeff = getNumpyArray(beta_coeff) res_beta_coeff = np.array([0, 1]).reshape(1, 2) assert_almost_equal(res_beta_coeff, np_beta_coeff) # predict lr_alg_predict = linear_prediction.Batch() lr_alg_predict.input.setModel(linear_prediction.model, model) lr_alg_predict.input.setTable(linear_prediction.data, nt_x) result = lr_alg_predict.compute() np_predict = getNumpyArray(result.get(linear_prediction.prediction)) assert_array_almost_equal(x, np_predict)
def testModel(): # 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(nFeatures, 0, NumericTableIface.doNotAllocate) testGroundTruth = HomogenNumericTable(nDependentVariables, 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 multiple linear 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 multiple linear regression and retrieve the algorithm results predictionResult = algorithm.compute() printNumericTable( predictionResult.get(prediction.prediction), "Linear Regression prediction results: (first 10 rows):", 10) printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
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 linear 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 linearRegressionPredict = prediction.Batch(method=prediction.defaultDense) # Pass the test data to the algorithm parts_list = testData.collect() for key, (h_table1, _) in parts_list: deserialized_h_table1 = deserializeNumericTable(h_table1) linearRegressionPredict.input.setTable(prediction.data, deserialized_h_table1) linearRegressionPredict.input.setModel(prediction.model, model) # Compute and retrieve the prediction results predictionResult = linearRegressionPredict.compute() return predictionResult.get(prediction.prediction)
def get_daal_prediction(x=np.array([1, 2, 3]), y=np.array([1, 2, 3])): ntX = HomogenNumericTable(x) ntY = HomogenNumericTable(y) lr_train = linear_training.Batch() lr_train.input.set(linear_training.data, ntX) lr_train.input.set(linear_training.dependentVariables, ntY) result = lr_train.compute() model = result.get(linear_training.model) lr_predict = linear_prediction.Batch() lr_predict.input.setModel(linear_prediction.model, model) lr_predict.input.setTable(linear_prediction.data, ntX) result = lr_predict.compute() np_predicted = getNumpyArray(result.get(linear_prediction.prediction)) # assert the same as the initial dependent variable assert_array_almost_equal(y, np_predicted) return np_predicted
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() linear_prediction_algorithm = \ linear_prediction.Batch() # set input linear_prediction_algorithm.input.setModel(linear_prediction.model, self.model) linear_prediction_algorithm.input.setTable(linear_prediction.data, Data) # TODO #if 'intercept' in self.parameters: # linear_prediction_algorithm.parameter.interceptFlag = True res = linear_prediction_algorithm.compute() return getNumpyArray(res.get(linear_prediction.prediction))
def predictResults(data, model): algorithm = prediction.Batch() algorithm.input.setTable(prediction.data, data) algorithm.input.setModel(prediction.model, model) return algorithm.compute()