1.[文件]
Relief算法程序.txt ~ 6KB
下载(44)
package com.relief.algorithm;
import java.util.Random;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Relief {
public Relief(){
}
public double[][] matrix;
public int length;
public int width;
public double[] weight;
//样本抽样次数
public int m;
//样本数量
public int k;
public int n_vars;
/**
* relief算法
*/
public void relief(){
matrix = new double[length][width];
//将样本数据赋值到matrix中
//权重值全部置为0
weight = new double[n_vars];
for(int i = 0; i < n_vars; i++){
weight[i] = 0.0;
}
//属性的最大值和最小值
double[] max = new double[n_vars];
double[] min = new double[n_vars];
for(int i = 0; i < width; i++){
for(int j = 0; j < length; j++){
double d = matrix[j][i];
if(d > max[i]){
max[i] = d;
}
if(d < min[i]){
min[i] = d;
}
}
}
//随机抽样m次
for(int i = 0; i < m; i++){
//随机抽取样本R
Random random = new Random();
int R_index = random.nextInt(width);
double[] R = new double[width];
for(int index = 0; index < width; index++){
R[index] = matrix[R_index][index];
}
//计算出距离样本R最近的样本和最远的样本
double maxvalue = 0.0;
double minvalue = 0.0;
int maxrow = 0;
int minrow = 0;
double distince = 0.0;
for(int len = 0; len < length; len++){
if(len != R_index){
for(int wid = 0; wid < width; wid++){
distince += Math.pow(R[wid]-matrix[len][wid], 2);
}
distince = Math.sqrt(distince);
if(len == 0){
maxvalue = distince;
minvalue = distince;
}
if(distince > maxvalue){
maxvalue = distince;
maxrow = len;
}
if(distince < minvalue){
minvalue = distince;
minrow = len;
}
}
}
int H_index = minrow;
double[] H = new double[width];
for (int index = 0; index < width; index++) {
H[index] = matrix[H_index][index];
}
int M_index = maxrow;
double[] M = new double[width];
for(int index = 0; index < width; index++){
M[index] = matrix[M_index][index];
}
//relief计算权重
for(int j = 0; j < n_vars; j++){
weight[j] = weight[j]-(Math.abs(R[j]-H[j])/(max[j]-min[j]))/m + (Math.abs(R[j]-M[j])/(max[j]-min[j]))/m;
}
}
for(int i = 0; i < width; i++){
System.out.println(weight[i]);
}
}
//有t个不同类
public int t;
/**
* reliefF算法
*/
public void reliefF(){
matrix = new double[length][width];
//将样本数据赋值到matrix中
//权重值全部置为0
weight = new double[n_vars];
for(int i = 0; i < n_vars; i++){
weight[i] = 0.0;
}
//属性的最大值和最小值
double[] max = new double[n_vars];
double[] min = new double[n_vars];
for(int i = 0; i < width; i++){
for(int j = 0; j < length; j++){
double d = matrix[j][i];
if(d > max[i]){
max[i] = d;
}
if(d < min[i]){
min[i] = d;
}
}
}
//随机抽样m次
Map map = new HashMap();
double[][] H = new double[k][width];
double[][] M = new double[k][width];
List lstM = new ArrayList();
double[] R = new double[width];
for(int i = 0; i < m; i++){
//随机抽取样本R
Random random = new Random();
int R_index = random.nextInt(width);
for(int index = 0; index < width; index++){
R[index] = matrix[R_index][index];
}
//计算所有数据到样本的距离
for(int len = 0; len < length; len++){
double distince = 0.0;
int row = 0;
if(len != R_index){
for(int wid = 0; wid < width; wid++){
distince += Math.pow(R[wid]-matrix[len][wid], 2);
}
distince = Math.sqrt(distince);
row = len;
map.put(distince, row);
}
}
//对样本距离排序
Set set = map.keySet();
List lst = new ArrayList(set);
Collections.sort(lst);
//获取距离最近的k个样本作为H
for(int a = 0; a < k; a++){
double d = lst.get(a);
int r = map.get(d);
for(int b = 0; b < width; b++){
H[a][b] = matrix[r][b];
}
}
//假设有t个不同分类,在每个不同类中获取k个样本
//在数据中随机获取k个样本作为M(C)
for(int a = 0; a < t; a++){
double[] RD = new double[width];
//随机抽取样本
Random rd = new Random();
int id = rd.nextInt(width);
while(true){
if(id != R_index){
break;
}
else{
id = rd.nextInt(width);
}
}
for(int index = 0; index < width; index++){
R[index] = matrix[R_index][index];
}
//计算所有数据到样本的距离
Map maprd = new HashMap();
for(int len = 0; len < length; len++){
double distince = 0.0;
int row = 0;
if(len != R_index){
for(int wid = 0; wid < width; wid++){
distince += Math.pow(R[wid]-matrix[len][wid], 2);
}
distince = Math.sqrt(distince);
row = len;
maprd.put(distince, row);
}
}
//对样本距离排序
Set setrd = map.keySet();
List lstrd = new ArrayList(set);
Collections.sort(lst);
//获取不同类距离最近的k个样本作为
for(int q = 0; q < k; q++){
double d = lst.get(q);
int r = map.get(d);
for(int p = 0; p < width; p++){
M[q][p] = matrix[r][p];
}
}
lstM.add(M);
}
}
//计算H的diff
double[] tmpval1 = new double[n_vars];
double[] tmpval2 = new double[n_vars];
for(int i = 0; i < n_vars; i++){
for(int j = 0; j < k; j++){
tmpval1[i] += Math.abs(R[i]-H[j][i])/(max[i]-min[i]);
}
}
//计算不同类的diff
double[] value = new double[n_vars];
for(double[][] d : lstM){
for(int i = 0; i < n_vars; i++){
for(int j = 0; j < k; j++){
value[i] += Math.abs(R[i]-M[j][i])/(max[i]-min[i]);
}
}
}
for(int i = 0; i < n_vars; i++){
tmpval2[i] += value[i]/t;
}
//计算权重
for(int j = 0; j < n_vars; j++){
weight[j] = weight[j]- tmpval1[j]/(m*k) + tmpval2[j]/(m*k);
}
for(int i = 0; i < width; i++){
System.out.println(weight[i]);
}
}
}