⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 artianomalyg.java

📁 自己编写的一个小算法 用于weka中
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
								                  instance1=(Instance)(d.copy());
								                  
								                  Double vf=new Double(d.value(i));
                                  								
								   	              if(attribute_Value[i]==null)
								   	              {
								   	  	                  double v=0;
								   	  	                  flag_value++;
								   	  	                  
								   	  	                  /*随机选择一个属性值赋予新的实例*/
								   	  	                  if(((values[ii][0].doubleValue()*100)%10!=0)||((values[ii][1].doubleValue()*100)%10!=0))
								   	  	                  {    
								   	  	                  	  							   	  	                  	  					   	  	                  	   
								   	  	                  	   int flag=0;
								   	  	                  	   for(int a=0;a<numInstances;a++)
								   	  	                  	   {
								   	  	                  	   	     v=((int)(Math.random()*((values[ii][0].doubleValue()-values[ii][1].doubleValue())*100+1))+values[ii][1].doubleValue()*100)/100;
                                           					
                                           				   if(v-vf<1e-6)
										  	                                  continue;/*有可能出现找不到满足条件的v*/
										  	                             else
										  	                             {      
										                                      flag=1;/*flag作为标记变量,当值为1时说明已经找到v*/
										                                      break;  			   	  	                 
								   	  	                             }
								   	  	                       }
								   	  	                       
								   	  	                       if(flag==1)
								   	  	                       {
								   	  	                            instance1.setValue(attributes[i],v);
								   	  	                            instance1.setClassValue(className);
								   	  	                            artianomalyNum++;
								   	  	                            finalInsts.add(instance1);
								   	  	                            artiInsts.add(instance1);
								   	  	                            								   	  	                            								   	  	                            
								   	  	                       }
								   	  	                       
								   	  	                  }
								   	  	                  else if(((values[ii][0].doubleValue()*10)%10!=0)||((values[ii][1].doubleValue()*10)%10!=0))
								   	  	                  { 
								   	  	                  	   
								   	  	                  	   
								   	  	                  	   int flag=0;
								   	  	                  	   for(int a=0;a<numInstances;a++)
								   	  	                  	   {
								   	  	                  	    	   v=((int)(Math.random()*((values[ii][0].doubleValue()-values[ii][1].doubleValue())*10+1))+values[ii][1].doubleValue()*10)/10;
	                                                   
	                                                   if(v-vf<1e-6)
	                                                         continue;
	                                                   else
	                                                   {
	                                                   	     flag=1;
	                                                         break;
	                                                   }
	                                             } 
	                                             
	                                             if(flag==1)
	                                             {
								                                     instance1.setValue(attributes[i],v);
								                                     instance1.setClassValue(className);
								                                     artianomalyNum++;
								                                     finalInsts.add(instance1);
								                                     artiInsts.add(instance1);
								                               }
								                          }
								   	  	                  else
								   	  	                  {

                                                                                       
                                               int flag=0; 
 								   	  	                  	   for(int a=0;a<numInstances;a++)
								   	  	                  	   {
								   	  	                  	        v=(int)(Math.random()*(values[ii][0].doubleValue()-values[ii][1].doubleValue()+1))+values[ii][1].doubleValue();
								   	  	                 
								   	  	                            if(v-vf<1e-6)
								   	  	                                  continue;
								   	  	                            else
								   	  	                            {
								   	  	                            	    flag=1;
								   	  	                                  break;
								   	  	                            }
								   	  	                       }
								   	  	                       
								   	  	                       if(flag==1)
								   	  	                       {
								                                    instance1.setValue(attributes[i],v);
								                                    instance1.setClassValue(className);
								                                    artianomalyNum++;
								                                    finalInsts.add(instance1);
								                                    artiInsts.add(instance1);
								                               }
								                          }
								                          
								                          if(flag_value==1)
								                          {
								   	  	                       ii++;//需要修改,放在这里不合适.
								   	  	                  }
								   	              }
								   	              else
								   	              {
								   	              	
								   	  	                  String v=new String(" ");
								   	  	    
								   	  	                  int flag=0;
								   	                      for(int a=0;a<numInstances;a++)
								   	                      {
								   	                               int s=(int)(Math.random()*attribute_Value[i].length);
								   	                               
								   	                               									                                 
									                                 v=new String(attribute_Value[i][s]);
									              
									                                 	                                  
									                                 if(v.equals(instances.instance(n).value(i))||(s==vf.intValue()))
									                                 //后者比较的是下标,新取出的值的下标与原来实例取值的下标比较
										  	                                  continue;
										  	                           else
										  	                           {       
										                                      flag=1;
										                                      break; 
										                               }      
								                          }/*新选出来的特征值v*/
								                          
								                          if(flag==1)
								                          {
								                          	       instance1.setValue(attributes[i],v);
								                          	       instance1.setClassValue(className);
								                          	       artianomalyNum++;
								                                   finalInsts.add(instance1);
								                                   artiInsts.add(instance1);
								                          }
								                        								          	           
								                  }							  
                  			 }
                   			  
					               for(n++;(n<num.length)&&(num[n].intValue()<=0);n++);   
					         }                    
             
             }
              
             System.out.println("the number for anomaly examples is:"+artianomalyNum);
             return artiInsts;
				 
			  }


//===========================================================================================
			  protected void classTovalue()
        {
      	       classvalue=new Double[numClass];
      	       for(int mq=0;mq<numClass;mq++)
      	       {
      	     	      classvalue[mq]=new Double(0.0+mq) ;
      	       }
      	    
        }
        
//===========================================================================================	         
//===========================================================================================
 /*关于各个类别的实例数目,以及产生出的finalInsts,
 外界可能会需要这些数据,而这些数据又是protected的.所以通过public的方法来将这些数据返回回来*/       
        public void getNumber()
        {
        	     for(int i=0;i<number.length;i++)
        	     {
        	     	       System.out.println(attri.elementAt(i)+":"+number[i]);
        	     }
        }

//===========================================================================================        
        
        public Instances getFinalInstances()
        {
        	     return finalInsts;
        }
 
 
//===========================================================================================             
//===========================================================================================        
        
        public void writeToFile(File f,Instances instns)
        {
        	     
        	     try
        	     {
        	          RandomAccessFile raf=new RandomAccessFile(f,"rw");
                    long len=raf.length();
                    
                    if(len<=0)
                    {
                    	   raf.seek(0);
                    }
                    else
                    {
                         raf.seek(len-1);
                    }
                    
                    for(int i=0;i<instns.numInstances();i++)
                    {
                         raf.write((new StringBuilder().append(instns.instance(i)).toString()).getBytes());  
                         raf.write("\n".getBytes());                           
                    }
                    
                    raf.close();
                }
                catch(Exception ex)
                {  }
        }    
//===========================================================================================        
         
        public void start_Generator(String artiName,String finalName,String className)/*对于外界,首先产生一个类的对象,通过对象调用该方法就可以了.
        外界不能直接调用summaryNum等方法,它们的访问权限为protected*/
        { 
        	    Instances artiAnomalyInsts;
        	   				      
				      System.out.println("starting summary examples' number for each class!"); 
				      summaryNum();//统计各个类别的实例数目summaryNum();
				      getNumber();
				      
				      System.out.println("Starting generate anomaly examples!");
				      artiAnomalyInsts=artiGenerator(className);//制造人工异常实例
				      
				      writeToFile(new File(artiName),artiAnomalyInsts);
				      
				      System.out.println("have writen to "+fileName+"\n"); 
				      
				      Instances FinalInsts=getFinalInstances();
				      writeToFile(new File(finalName),FinalInsts);
				      System.out.println("have writen to "+finalName+"\n"+"have finished!!");
				     
				}     
//===========================================================================================				            
        
        public static void main(String []args)
        {
        	
               try
               {       	
        	                   	     
        	            FileInputStream fis=new FileInputStream(new File(args[0]));
        	            InputStreamReader isr=new InputStreamReader(fis);
                      BufferedReader br=new BufferedReader(isr);    	     
        	  
                      Instances theInstances=new Instances(br);        	  
        	            ArtiAnomalyG aag=new ArtiAnomalyG(theInstances);
                      //args[0]是传进来的一个参数.表示产生出的人工异常实例所应赋的类别值.       	             	
        	            aag.start_Generator(args[1],args[2],args[3]);
        	            /*两个参数分别用来给定异常数据保存的文件名、异常数据及原有数据的保存文件名以及异常数据的类标识*/
               }
               catch(Exception ex)
               {
              	      ex.printStackTrace();
               }
        }
        						
	  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -