当前位置: 首页 > 工具软件 > ELKI > 使用案例 >

机器学习 ELKI 使用心得 DBSCAN

宰父宾实
2023-12-01
一下是我们使用elki 在自己的代码中调用DBSCAN 算法的一个例子
		int NumberofDatabase=2730*35;
		String file="E://RXM_DATA//weka_elki_data//HSV//Data_JD_20_1.arff";
		Pattern colSep = Pattern.compile("\\s*[,;\\s]\\s*");
		String quoteChars = "\"'";
		Pattern comment = Pattern.compile("^\\s*(#|//|;|@| ).*$");
		BitSet labelIndices = new BitSet();
		Parser parser=new NumberVectorLabelParser<>(colSep, quoteChars, comment, labelIndices, DoubleVector.FACTORY);
		DatabaseConnection dbc=new FileBasedDatabaseConnection(null, parser, file);
		Database db=new StaticArrayDatabase(dbc,null);
		db.initialize();
		EuclideanDistanceFunction distanceFunction=EuclideanDistanceFunction.STATIC;
	
		double  epsilon=0.1;
		int  minpts=50;
		int numberOfNeighbors=50;


		SharedNearestNeighborIndex.Factory<NumberVector, SharedNearestNeighborIndex<NumberVector>> indexFactory=new Factory(numberOfNeighbors, distanceFunction);
		SharedNearestNeighborSimilarityFunction<NumberVector> similarityFunction=new SharedNearestNeighborSimilarityFunction<NumberVector>(indexFactory);
		SNNClustering<NumberVector> snn=new SNNClustering(similarityFunction, epsilon, minpts);
		Clustering<Model> c=snn.run(db);
		Relation<NumberVector> relation = db.getRelation(TypeUtil.NUMBER_VECTOR_FIELD);
		DBIDRange ids = (DBIDRange) relation.getDBIDs();</span>

		int belong[] = new int[NumberofDatabase];
	
		int i = 0;
		for(Cluster<Model> clu : c.getAllClusters()) {
			System.out.println(clu.getNameAutomatic());
		  if(clu.getNameAutomatic().equals("Noise"))
		  {
			  for(DBIDIter it = clu.getIDs().iter(); it.valid(); it.advance()) {
				    final int offset = ids.getOffset(it);
				    belong[offset]=-1;  
				  }
			  
		  }
		  else
		  {
			  for(DBIDIter it = clu.getIDs().iter(); it.valid(); it.advance()) {
				    final int offset = ids.getOffset(it);
				    belong[offset]=i;  
				  }
				  ++i;
			  
		  }
		 
		}
		
		FileWriter file1=new FileWriter("C://Users//Administrator//Desktop//Work//PPM_03//LM//landmark_0.09_50.dat");
		for(int i1=0;i1<NumberofDatabase;i1++)
		{
			file1.write(belong[i1]+"\n");
			
		}
		file1.flush();
		file1.close();
		System.out.println("i="+i+"\n");

 类似资料: