Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testModel():
global testGroundTruth, predictionResult
# Initialize FileDataSource to retrieve the test data from a .csv file
testDataSource = FileDataSource(
testDatasetFileName,
DataSourceIface.notAllocateNumericTable,
DataSourceIface.doDictionaryFromContext
)
# Create Numeric Tables for testing data and labels
testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
mergedData = MergedNumericTable(testData, testGroundTruth)
# Retrieve the data from input file
testDataSource.loadDataBlock(mergedData)
# Create algorithm objects for LogitBoost prediction with the default method
algorithm = prediction.Batch(nClasses)
# Pass the testing data set and trained model to the algorithm
algorithm.input.setTable(classifier.prediction.data, testData)
algorithm.input.setModel(classifier.prediction.model, model)
# Compute prediction results and retrieve algorithm results
# (Result class from classifier.prediction)
predictionResult = algorithm.compute()
def testModel():
global trainingResult, predictionResult
# Initialize FileDataSource to retrieve the test 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)
# Load the data from the data 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)
def testModel():
global predictionResult, groundTruthLabels
# Initialize FileDataSource to retrieve the test data from a .csv file
testDataSource = FileDataSource(
testDatasetFileName, DataSourceIface.doAllocateNumericTable,
DataSourceIface.doDictionaryFromContext
)
# Create Numeric Tables for testing data and labels
testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
groundTruthLabels = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
mergedData = MergedNumericTable(testData, groundTruthLabels)
# Retrieve the data from input file
testDataSource.loadDataBlock(mergedData)
# Create an algorithm object to predict multi-class SVM values
algorithm = multi_class_classifier.prediction.Batch(nClasses,fptype=np.float64)
algorithm.parameter.training = training
algorithm.parameter.prediction = prediction
# Pass a testing data set and the trained model to the algorithm
algorithm.input.setTable(classifier.prediction.data, testData)
algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
import os
import sys
import daal.algorithms.distributions as distributions
import daal.algorithms.distributions.bernoulli as bernoulli
from daal.algorithms.engines.mt19937 import Batch_Float64DefaultDense_create as create
from daal.data_management import HomogenNumericTable, NumericTableIface
utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
if utils_folder not in sys.path:
sys.path.insert(0, utils_folder)
from utils import printNumericTable
if __name__ == "__main__":
# Create input table to fill with random numbers
dataTable = HomogenNumericTable(1, 10, NumericTableIface.doAllocate)
# Create the algorithm
bernoulli = bernoulli.Batch(0.5)
# Set the algorithm input
bernoulli.input.set(distributions.tableToFill, dataTable)
# Set the Mersenne Twister engine to the distribution
bernoulli.parameter.engine = create(777)
# Perform computations
bernoulli.compute()
# Print the results
printNumericTable(dataTable, "Bernoulli distribution output:")
def trainModel():
global trainingResult
# Initialize FileDataSource to retrieve the input data from a .csv file
trainDataSource = FileDataSource(
trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
DataSourceIface.doDictionaryFromContext
)
# Create Numeric Tables for training data and labels
trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
mergedData = MergedNumericTable(trainData, trainGroundTruth)
# Retrieve the data from the input file
trainDataSource.loadDataBlock(mergedData)
# Create an algorithm object to train the SVM model
algorithm = svm.training.Batch()
algorithm.parameter.kernel = kernel
algorithm.parameter.cacheSize = 600000000
# Pass a training data set and dependent values to the algorithm
algorithm.input.set(classifier.training.data, trainData)
algorithm.input.set(classifier.training.labels, trainGroundTruth)
# Build the SVM model and get the algorithm results
def testModel():
global testGroundTruth, predictionResult
# Initialize FileDataSource to retrieve the test data from a .csv file
testDataSource = FileDataSource(
testDatasetFileName,
DataSourceIface.notAllocateNumericTable,
DataSourceIface.doDictionaryFromContext
)
# Create Numeric Tables for testing data and labels
testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
mergedData = MergedNumericTable(testData, testGroundTruth)
# Retrieve the data from input file
testDataSource.loadDataBlock(mergedData)
# Create algorithm objects for decision tree classification prediction with the default method
algorithm = prediction.Batch()
# Pass the testing data set and trained model to the algorithm
#print("Number of columns: {}".format(testData.getNumberOfColumns()))
algorithm.input.setTable(classifier.prediction.data, testData)
algorithm.input.setModel(classifier.prediction.model, model)
# Compute prediction results and retrieve algorithm results
# (Result class from classifier.prediction)
# Retrieve the data from the input file
dataSource.loadDataBlock(mergedData)
nVectors = data.getNumberOfRows()
mseObjectiveFunction = optimization_solver.mse.Batch(nVectors)
mseObjectiveFunction.input.set(optimization_solver.mse.data, data)
mseObjectiveFunction.input.set(optimization_solver.mse.dependentVariables, dependentVariables)
# Create objects to compute the Adagrad result using the default method
adagradAlgorithm = optimization_solver.adagrad.Batch(mseObjectiveFunction)
# Set input objects for the the Adagrad algorithm
adagradAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(startPoint))
adagradAlgorithm.parameter.learningRate = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, learningRate)
adagradAlgorithm.parameter.nIterations = halfNIterations
adagradAlgorithm.parameter.accuracyThreshold = accuracyThreshold
adagradAlgorithm.parameter.batchSize = batchSize
adagradAlgorithm.parameter.optionalResultRequired = True
# Compute the Adagrad result
# Result class from daal.algorithms.optimization_solver.iterative_solver
res = adagradAlgorithm.compute()
# Print computed the Adagrad result
printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Minimum after first compute():")
printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")
adagradAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, res.getResult(optimization_solver.iterative_solver.minimum))
adagradAlgorithm.input.setInput(optimization_solver.iterative_solver.optionalArgument, res.getResult(optimization_solver.iterative_solver.optionalResult))
def trainModel():
global trainingResult
# Initialize FileDataSource to retrieve the input data from a .csv file
trainDataSource = FileDataSource(
trainDatasetFileName,
DataSourceIface.notAllocateNumericTable,
DataSourceIface.doDictionaryFromContext
)
# Create Numeric Tables for training data and labels
trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
mergedData = MergedNumericTable(trainData, trainGroundTruth)
# Retrieve the data from the input file
trainDataSource.loadDataBlock(mergedData)
# Create an algorithm object to train the stump model
algorithm = training.Batch()
# Pass a training data set and dependent values to the algorithm
algorithm.input.set(classifier.training.data, trainData)
algorithm.input.set(classifier.training.labels, trainGroundTruth)
# Compute and retrieve the algorithm results
trainingResult = algorithm.compute()
def testModel():
global trainingResult, predictionResult
# Initialize FileDataSource to retrieve the test 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(1, 0, NumericTableIface.doNotAllocate)
mergedData = MergedNumericTable(testData, testGroundTruth)
# Load the data from the data file
testDataSource.loadDataBlock(mergedData)
# Create algorithm objects for KD-tree based kNN prediction with the default method
algorithm = prediction.Batch()
# Pass the testing data set and trained model to the algorithm
algorithm.input.setTable(classifier.prediction.data, testData)
algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
# Compute prediction results
predictionResult = algorithm.compute()
printNumericTables(
testGroundTruth, predictionResult.get(classifier.prediction.prediction),
# Input data set parameters
datasetFileName = "../data/batch/basic_statistics.csv"
data = np.array([(7.0, 3.0, 6.0, 2.0),
(1.0, 3.0, 0.0, 2.0),
(9.0, 2.0, 6.0, 2.0),
(3.0, 4.0, 7.0, 2.0),])
# Initialize FileDataSource to retrieve the input data from a .csv file
dataSource = FileDataSource(datasetFileName, DataSourceIface.doAllocateNumericTable)
dataSource.createDictionaryFromContext()
dataSource.loadDataBlock()
table = dataSource.getNumericTable()
# Get basic statistics from the table. They were calculated inside DataSource for each column.
min = table.basicStatistics.get(NumericTableIface.minimum)
max = table.basicStatistics.get(NumericTableIface.maximum)
sum = table.basicStatistics.get(NumericTableIface.sum)
sumSquares = table.basicStatistics.get(NumericTableIface.sumSquares)
# Print calculated basic statistics
printNumericTable(table, "Basic statistics of table:")
printNumericTable(min, "Minimum:")
printNumericTable(max, "Maximum:")
printNumericTable(sum, "Sum:")
printNumericTable(sumSquares, "SumSquares:")
# Create NumericTable with the same data. But in this case basic statistics are not calculated.
dataTable = HomogenNumericTable(data)
# Set basic statistics in the new NumericTable
dataTable.basicStatistics.set(NumericTableIface.minimum, min);