29 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			29 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 
								 | 
							
								var x = [[0, 0, 0], [0, 1, 1], [1, 1, 0], [2, 2, 2], [1, 2, 2], [2, 1, 2]];
							 | 
						||
| 
								 | 
							
								var y = ['A', 'A', 'B', 'B', 'C', 'C'];
							 | 
						||
| 
								 | 
							
								var model = ml.learn({
							 | 
						||
| 
								 | 
							
								    algorithm:ml.ML.SVM,
							 | 
						||
| 
								 | 
							
								    x:x, 
							 | 
						||
| 
								 | 
							
								    y:y,
							 | 
						||
| 
								 | 
							
								    threshold:false,  // no threshold function on output; highest value of svms is winner
							 | 
						||
| 
								 | 
							
								    labels:['A','B','C'],  // multi-SVM
							 | 
						||
| 
								 | 
							
								    C : 15.0, // default : 1.0. C in SVM.
							 | 
						||
| 
								 | 
							
								    tol : 1e-5, // default : 1e-4. Higher tolerance --> Higher precision
							 | 
						||
| 
								 | 
							
								    max_passes : 200, // default : 20. Higher max_passes --> Higher precision
							 | 
						||
| 
								 | 
							
								    alpha_tol : 1e-5, // default : 1e-5. Higher alpha_tolerance --> Higher precision
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    kernel : { type: 'rbf', sigma: 0.5 } // { type: "polynomial", c: 1, d: 5}
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								print(toJSON(model).length+' Bytes')
							 | 
						||
| 
								 | 
							
								print(model)
							 | 
						||
| 
								 | 
							
								print(model.svms[0])
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var test_data =[[0, 1.2, 0],
							 | 
						||
| 
								 | 
							
								                [2.1, 2, 3],
							 | 
						||
| 
								 | 
							
								                [2.1,1.1,2.0]
							 | 
						||
| 
								 | 
							
								];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								print(ml.classify(model,x))
							 | 
						||
| 
								 | 
							
								print(ml.classify(model,x.map(function (row) { return row.map(function (col) { return col+random(-0.3,0.3,0.001) })})))
							 | 
						||
| 
								 | 
							
								print(ml.classify(model,test_data))
							 | 
						||
| 
								 | 
							
								print(ml.stats.utils.best(ml.classify(model,[1,2,3])))
							 |