def predict(self, features, method, parameters=[], labelValidation=[], classifier=[]):
""" To compute the model parameters given the feature matrix and the
labels of a training dataset
INPUTS:
parameters: 1D np.array, the parameters of the model
features -- 2D np.array, feature matrix containung the
features extracting from the dataset
labelValidation -- 1D np.array, the labels used for validation
OUTPUTS:
predictedData: 1D np.array, the predicted labels
"""
# Number of 3D MRI images in the dataset:
nbSamples = features.shape[0]
if method in ["Random Forest", "AdaBoost", "Bagging", "Gradient Boosting"]:
predictedData = classifier.predict_proba( features )[:,1]
#print(predictedData)
elif method == "SVM":
predictions = classifier.predict( features )
predictedData = np.zeros(len(predictions))
# We change the values predicted by the support vector machine
# classifier: 0-->0.1 and 1-->0.9. This allows reducing the
# logistic score:
for i in range (len(predictions)):
if predictions[i] == 0:
predictedData[i] = 0.1
else:
predictedData[i] = 0.9
elif method in ["Multi SVM", "Multi Random Forest"]:
predictedData = classifier.predict( features )
else:
# The first column of the feature matrix should be the bias:
bias = np.ones([nbSamples,1])
features = np.concatenate((bias, features), axis=1)
# Prediction of the model for the given dataset:
predictedData = features.dot( parameters )
# Computation of the error of the predicted data:
if len(labelValidation) > 0:
if method in ["Random Forest", "SVM", "AdaBoost", "Bagging", "Gradient Boosting"]:
error = log_loss(labelValidation, predictedData)
elif method in ["Multi Random Forest", "Multi SVM"]:
predictionsMultiple = np.zeros([len(labelValidation), 3])
truePrediction = np.zeros([len(labelValidation), 3])
for i in range(len(labelValidation)):
predictionsMultiple[i,:] = self.classToLabels[\
predictedData[i],:]
# True labels:
truePrediction[i,:] = self.classToLabels[\
labelValidation[i],:]
# Score function:
error = np.mean(np.abs(predictionsMultiple - truePrediction))
else:
error = (np.mean((predictedData - labelValidation)**2))
return predictedData, error
else:
return predictedData
modelParameters = data2Predict.buildClassifier(featureMatrix, \
label, method = "RidgeCV")
# Prediction of the data using the model parameters:
MSESelf = data2Predict.predict(featureMatrix,modelParameters ,\
labelValidation = label)