Question
QUESTIONS: Find the maximum and minimum values for each behavior: 1 The dataset needs to be normalized 3 Calculate similarity between customers. Ensure that the
QUESTIONS:
Find the maximum and minimum values for each behavior:
1
The dataset needs to be normalized
3
Calculate similarity between customers.
Ensure that the algorithm inperforms faster
this algorithm is not working for all of the requirements can anyone help?
public class SimilarCustomer {
public int findMostSimilarCustomer(double[][] customerBehaviorMatrix, int currentCustomerIndex) {
//1. find the min max values
double[][] minMaxValuesMatrix = new double[2][customerBehaviorMatrix[0].length];
//assign min and max values to return matrix
for (int j = 0; j < minMaxValuesMatrix[0].length; j++) {
minMaxValuesMatrix[0][j] = Double.MAX_VALUE;
minMaxValuesMatrix[1][j] = Double.MIN_VALUE;
}
//for each column find min and max
for (int i = 0; i < customerBehaviorMatrix.length; i++) {
for (int j = 0; j < customerBehaviorMatrix[i].length; j++) {
//check min
if (customerBehaviorMatrix[i][j] < minMaxValuesMatrix[0][j]) {
//store min in the corresponding return matrix
minMaxValuesMatrix[0][j] = customerBehaviorMatrix[i][j];
}
//check max
if (customerBehaviorMatrix[i][j] > minMaxValuesMatrix[1][j]) {
//store max in the corresponding return matrix
minMaxValuesMatrix[1][j] = customerBehaviorMatrix[i][j];
}
}
}
//2. Normalize the dataset
double[][] normalizedCustomerbehavior =
new double[customerBehaviorMatrix.length][customerBehaviorMatrix[0].length];
// for each entry normalize value and store in return matrix
for (int j = 0; j < normalizedCustomerbehavior[0].length; j++) {
double min = minMaxValuesMatrix[0][j];
double max = minMaxValuesMatrix[1][j];
for (int i = 0; i < normalizedCustomerbehavior.length; i++) {
double value = customerBehaviorMatrix[i][j];
//Assign the normalized value
if ((max-min)==0) {
normalizedCustomerbehavior[i][j] = 0;
} else {
normalizedCustomerbehavior[i][j] = (value - min)/(max-min);
}
}
}
//step 3 and 4 combined. find similar (other) customer; min distance
int returnIndex = -1;
double distance, mindistance = Double.MAX_VALUE;
for (int i = 0; i < normalizedCustomerbehavior.length; i++) {
if (currentCustomerIndex != i) {
//define sum of squares
double sum = 0.0;
for (int j = 0; j < normalizedCustomerbehavior[i].length; j++) {
//calculate difference
double difference = (normalizedCustomerbehavior[currentCustomerIndex][j]-normalizedCustomerbehavior[i][j]);
//square the difference
double squareOfDifference = difference*difference;
//add to the sum
sum += squareOfDifference;
}
//add square root of sum as similarity
distance = Math.sqrt(sum);
// compare distance
if(distance < mindistance)
{
returnIndex = i;
mindistance = distance;
}
}
}
return returnIndex;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started