35 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			35 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 
								 | 
							
								var data = load('test-data-iris.json')
							 | 
						||
| 
								 | 
							
								var t0=time()
							 | 
						||
| 
								 | 
							
								var target = "species";
							 | 
						||
| 
								 | 
							
								var features = ["length", "width","petal_length","petal_width"];
							 | 
						||
| 
								 | 
							
								var datac = ml.preprocess(data,'xy',{features:features,target:target});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var model = ml.learn({
							 | 
						||
| 
								 | 
							
								 algorithm:ml.ML.ID3,
							 | 
						||
| 
								 | 
							
								 data:data,
							 | 
						||
| 
								 | 
							
								 target:target,
							 | 
						||
| 
								 | 
							
								 features:features
							 | 
						||
| 
								 | 
							
								})
							 | 
						||
| 
								 | 
							
								var t1=time()
							 | 
						||
| 
								 | 
							
								var result = ml.classify(model,datac.x).map(function (r,i) { 
							 | 
						||
| 
								 | 
							
								  return {value:r,y:datac.y[i]}} )
							 | 
						||
| 
								 | 
							
								var t2=time()
							 | 
						||
| 
								 | 
							
								var correct=0,wrong=0;
							 | 
						||
| 
								 | 
							
								result.forEach(function (r) { if (r.value==r.y) correct++; else wrong++ });
							 | 
						||
| 
								 | 
							
								print('Training Data Test: Correct='+correct+', wrong='+wrong);
							 | 
						||
| 
								 | 
							
								print(toJSON(model).length)
							 | 
						||
| 
								 | 
							
								print(t1-t0,t2-t1)
							 | 
						||
| 
								 | 
							
								datac.x = ml.noise(datac.x,
							 | 
						||
| 
								 | 
							
								  {length:0.2,
							 | 
						||
| 
								 | 
							
								   width:0.1,
							 | 
						||
| 
								 | 
							
								   petal_length:0.05,
							 | 
						||
| 
								 | 
							
								   petal_width:0.01
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// print(datac.x)
							 | 
						||
| 
								 | 
							
								var result = ml.classify(model,datac.x).map(function (r,i) { 
							 | 
						||
| 
								 | 
							
								  return {value:r,y:datac.y[i]}} )
							 | 
						||
| 
								 | 
							
								correct=0,wrong=0;
							 | 
						||
| 
								 | 
							
								result.forEach(function (r) { if (r.value==r.y) correct++; else wrong++ });
							 | 
						||
| 
								 | 
							
								print('Test Data Test (Noise): Correct='+correct+', wrong='+wrong);
							 |