PCCs系数
package ai;
public class pccs {
public static void main(String[] args) {
double same[][]=new double[2][4];
same[0]=new double[]{1,2,3,4,6,6,7,7,8,8,9};
same[1]=new double[]{-1,-2,-3,-4,-6,-6,-7,-7,-8,-8,-9};//{1,2,3,4,6,6,7,7,8,0,9};
double ship=sameShip(same);
System.out.println(ship);
}
public static double []sum(double same[][]){
double samesSum[]={0,0};
double sumA=0;
double sumB=0;
for(double entity:same[0]){
sumA+=entity;
}
for(double entity:same[1]){
sumB+=entity;
}
samesSum[0]=sumA;
samesSum[1]=sumB;
return samesSum;
}
public static double []squareSum(double same[][]){
double samesSum[]={0,0};
double sumA=0;
double sumB=0;
for(double entity:same[0]){
sumA+=(entity*entity);
}
for(double entity:same[1]){
sumB+=(entity*entity);
}
samesSum[0]=sumA;
samesSum[1]=sumB;
return samesSum;
}
public static double joinSum(double same[][]){
double joinSum=0;
for(int i=0;i<same[0].length;i++){
joinSum+=same[0][i]*same[1][i];
}
return joinSum;
}
public static double sameShip(double same[][]){
int num_ratings=same[0].length;
double product_sum= joinSum(same);
double sumTwo[]=sum(same);
double squareSumEntity[]=squareSum(same);
double user1_squared_sum=squareSumEntity[0];
double user2_squared_sum=squareSumEntity[1];
double Sxy=product_sum-(sumTwo[0]*sumTwo[1]/num_ratings);
double Sxx = user1_squared_sum - (sumTwo[0]*sumTwo[0]) / num_ratings;
double Syy = user2_squared_sum - (sumTwo[1]*sumTwo[1]) / num_ratings;
if (Sxx * Syy == 0)
return 0;
return Sxy / (Math.sqrt(Sxx * Syy));
}
}