Skip to content

Instantly share code, notes, and snippets.

@javierhonduco
Created April 2, 2015 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javierhonduco/517b102fb6f3a3394c9b to your computer and use it in GitHub Desktop.
Save javierhonduco/517b102fb6f3a3394c9b to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.io.*;
public class Untitled{
static final String arff_path = "/Users/javierhonduco/Dropbox/Aprendizaje Automatico/datosConCabecera.arff";
static ArrayList<ArrayList> linelist;
/* Datos falsos para probar*/
static Double[] points = {2.7397667055569395, 1.8801694653495988, 0.08373404401878415, 2.409369587918457, 1.5133136092028532, 2.5732611337404476, 0.7725538745086804, 0.3079027861422743, 1.1080049786194923, 0.6737391008211959, 0.3829538176494317, 1.104528998694343, 0.036366324634571456, 0.7855395872766329, 2.464462078590649, 0.4156500589873049, 2.9783914962757723, 1.6147506936654668, 0.9536584816444263, 0.7736642255811188, 1.5417078664553692, 0.47450331779258803, 2.9378887386815693, 2.109978519056963, 2.5205199185530516, 1.579170341248749, 0.2387212532422005, 1.801507415428087, 2.913251119037251, 2.154295153586135, 2.331646076178221, 1.6969535989588835, 1.1774904411950473, 0.4443470392241329, 1.9909186416426872, 0.9559023123849442, 2.398211186704344, 2.7227382599992436, 0.5250000029522872, 2.0983171232846747, 0.9395375252785273, 2.304700877597957, 1.6163031585308387, 0.27121632459057876, 2.1178244601796994, 1.0948347888287118, 2.938674326932218, 2.1051908734344535, 2.968124086561309, 0.8186574394811879, 0.2193066866318394, 2.493622512617523, 0.05534850446600326, 0.261933873692597, 1.2086443922621615, 2.629582707304632, 1.4101478302591772, 2.2929560950839916, 2.630681071007838, 1.7193485084931321, 1.944599945850841, 0.4546215899377881, 2.678129576424621, 2.1320103462829083, 1.8618885302169088, 1.577562012601565, 1.5531072717326353, 1.9999123849705835, 2.868909950772844, 0.2835079447655845, 1.708154263640225, 0.8902822671330847, 1.331556433179054, 2.5123429674248894, 2.0287520525213107, 1.5780147354701621, 1.496876199692072, 1.11323781859112, 2.725373767961672, 1.7174608721769797, 1.7108440332852781, 2.80240333340814, 2.6555809327191873, 1.4884422041476135, 1.9123993633809762, 1.7489446467842704, 0.4490809089352631, 2.0768555846574603, 1.265775962043035, 0.6022292982003884, 1.0364758191724777, 2.8942549349920386, 1.8212319339346603, 0.9935842327026004, 2.7641817795241583, 0.4022874788623666, 2.0085313126912108, 1.895685528052933, 2.4383133104704, 1.3995291752306054, 2.266060007820117, 1.0963369616494156, 0.5422675189976208, 0.308397834246671, 2.3344080808299474, 0.5774086169408755, 2.9984521745816517, 2.4043928201055644, 1.0150377905124122, 2.495282316123699, 2.0467633829927623, 2.9493959901338878, 1.674985208760602, 2.6174924449321004, 1.2891696726474966, 1.8746270144679387, 2.995892417441795, 1.0499240887583883, 2.53494485702708, 2.9272712489361044, 2.2161640138942347, 2.9490468706934903, 2.028452033347999, 1.610715664147281, 1.2726177489527841, 1.7246437455802424, 2.5091366242896402, 0.08677358807322078, 0.005292833307594558, 2.9590381751683594, 0.23112280305761768, 2.7032073539070964, 2.361895048206562, 1.473029683643643, 0.7723783676333732, 1.8940291631442996, 2.4482330401685313, 0.6240867606625672, 1.8104182846344834, 0.7552628747842808, 2.394844682636714, 0.20676556372058885, 2.436358984076872, 2.4736139558475396, 0.7346533888898105, 2.3974796213695315, 1.4776163276821492, 1.5190934354330916, 1.3377428088028442, 0.7084428275448411, 1.455456034740236, 0.8641736703703256, 1.0045593697786785, 2.4274264594725983, 1.1564095506666467, 1.0981849134325015, 2.256655294321857, 2.602181346306742, 0.5274981871246823, 2.142289878544542, 1.1958988124038203, 1.6323255645226387, 2.00015080877276, 1.9232866144311629, 2.7010198646913746, 2.0987392150528543, 1.74863973946407, 1.7769174758900046, 2.729059742390664, 0.5365631624816568, 2.5250840707227327, 2.4091866020674897, 1.9724657438343556, 2.3135433250892463, 1.7228424881613198, 2.1672418275693204, 0.11018075327105126, 2.7126981583061527, 1.8421601477583955, 0.8311908596030241, 2.534080535146299, 1.6560996595021449, 1.2894837932541638, 1.5969375750132797, 1.967482796600566, 1.1910685432978037, 1.496269580922712, 1.4195531210122803, 0.7043839848962345, 1.9896660059245022, 1.3161569013451637, 2.455852417069606, 1.7839642251673937, 0.8327978119585608, 1.4299951266995445, 0.7841146813750972, 2.435180379887595, 1.0760566871191917, 2.092463128670878, 1.5301136758124139, 0.8311278967028148, 0.5214509782162329, 0.24460161180239726, 0.8775693234614539, 1.808494326843018, 1.741420488746169, 2.874065698325321, 2.123050593962165, 2.125930096037502, 1.7215648972702786, 0.4716267708356846, 1.0494369171302336, 1.7670288383799058, 1.3287650832491815, 1.905791950597073, 2.0307829598953053, 0.15715111035427254, 2.065238191812619, 1.5145089926856972, 2.367637513768562, 2.8672814303016674, 1.828241168030302, 0.7084677172434992, 0.08215207626619092, 0.2208085064585099, 0.07219919409932918, 2.54495537603419, 0.8540749338828356, 0.916484070024858, 0.5035640998918636, 2.2901216520214325, 2.7341646378328623, 2.599548967644642, 0.369080841013152, 1.0643068557858664, 0.674705410818418, 1.7984404551316857, 1.6431602544685122, 1.9226336942713513, 1.7472043475642671, 1.5712112056536207, 1.1964979064025951, 1.52582702228892, 2.5798143944429452, 2.5562465025011583, 0.39296370220913, 2.0145926949093154, 1.8991975523225373, 1.39625225476011, 0.4870182454331863, 0.7631291644982363, 2.9989290522899523, 0.7097454402152027, 0.055645767308307725, 2.5157116841953626, 2.616089578649631, 2.10872484416307, 2.414987290765906, 1.6359392605731022, 1.7574225190390846, 1.2939632937825274, 1.0320827464864797, 2.1008288434329208, 1.9642457815720489, 1.861922167500723, 2.93843987044014, 1.01032989246781, 0.6942091775006023, 2.6032118007050253, 2.712442830568044, 2.881070454208995, 0.0005445044229105411, 0.3735295908011266, 0.9544650777754, 0.6007870207149224, 0.44836660425840047, 2.8497291016516364, 2.1459759105152565, 2.88445941452661, 2.5855657789686406, 0.3603480395267066, 2.03342465518229, 1.4047431838984143, 0.5784840146268264, 2.5057773944341766, 2.223020347958361, 1.8174842115022534, 1.5829090026143389, 1.0710925988286941, 2.3933786277735494, 1.4885378989063476, 2.9388149576128497, 1.9462631113365378, 2.5788500721434398, 2.733202470106564, 2.37956884646584, 0.4364192278079144, 1.1096832649452437, 1.2177733753442344, 2.7135751114270166, 2.141407761033413, 0.13508975794559352, 0.2405028363278191, 0.7191190642847756, 1.5875181655465367, 2.866683449318628, 2.285299490503369, 2.9154614399796106, 0.6112104942412896, 0.8831391269144451, 1.2825472955171362, 1.894646793792438, 2.8290577653354267, 2.01135255694985, 0.733941333068459, 1.484496231600906, 0.3537861647126156, 2.3572742429033906, 2.290360318020056, 2.1931869734752283, 1.5926298548196662, 2.6745366265914687, 2.3023427826987266, 1.5528252304080796, 2.7988648965277325, 1.001132720017326, 2.9077664455253096, 1.3760524270969707, 2.135139151762283, 2.8723581542883796, 2.238138788991524, 0.3046958146092381, 0.8290339512010558, 2.471584468393674, 2.271411371031732, 1.9485419460227988, 1.5453724209740427, 1.53160125679586, 0.7695511745569479, 2.499392803683684, 1.303458197018097, 1.6786851988574767, 0.3848604889580497, 0.9183713561288122, 0.4817203018117556, 1.0289611470590723, 2.263711651026419, 0.9756165593630269, 1.2046406453244174, 1.9840859371849036, 1.822142866345611, 1.5538061498810125, 2.0242057571435126, 1.4081546574234496, 0.31037509504364624, 1.965668468013365, 0.14205278005762212, 0.6909406688362052, 2.67627708681023, 1.9509349787426462, 0.9110450947935625, 0.38371658169614986, 0.6446457517583538, 0.38753002747341236, 1.1043109630376282, 1.2690311521732514, 2.4713628579141, 1.4034527642339496, 1.2689674169291645, 2.8198530529112147, 0.29542876240097404, 2.604636533556793, 2.398762666800981, 1.2110615706491674, 1.5633057724542931, 1.6929409454098523, 1.3041673477339433, 0.00995203830518021, 1.6486460948510033, 0.6606428360182304, 1.2573868955117176, 1.211690021737231, 0.6316014683382444, 0.23293689512114124, 1.0130907106260567, 1.203751690740221, 1.7864290487769479, 0.4835413080299463, 2.930045933653868, 0.5708061845455944, 2.403842796571888, 2.365389164834634, 1.7160197311119745, 2.2459680103736943, 0.6927392256632049, 1.9967996426565127, 1.87905736937986, 2.112990002841052, 2.137504815104072, 2.262524695595959, 2.377504019172395};
public static void euclideanDistance(Double[] points){
/* Calcula la distancia euclídea de los puntos dados
a la matriz bidimensional de puntos parseados.
Asume que han sido generados con this.parseFile() */
if(linelist==null || (points.length != linelist.get(0).size())){
System.out.println("\t=> ERROR in points array");
System.exit(-1);
}
ArrayList<Double> distances = new ArrayList<Double>();
for(ArrayList<Double> instancia: linelist){
int counter = 0;
Double distancia_calculada = 0.0;
for(Double punto: instancia){
distancia_calculada += Math.pow(punto-points[counter], 2);
counter++;
}
distances.add(Math.sqrt(distancia_calculada));
}
Double minumun = -Double.MIN_VALUE;
for(Double euclideas: distances){
if(euclideas > minumun){
minumun = euclideas;
}
}
System.out.println(minumun);
}
public static void parseFile(String file_path){
/* Lee el arrff suministrado ignorando las líneas
que comienzan con @ y parsea los números como floats.
Lo almacena en un vector de vectores de floats */
linelist = new ArrayList<>();
File file = new File(file_path);
BufferedReader reader = null;
int sumup = 0;
Double lines = 0.0;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
if(text.startsWith("@")){
continue;
}
ArrayList<Double> data_list = new ArrayList<Double>();
for(String dato: text.split(",")){
data_list.add(Double.parseDouble(dato));
sumup++;
}
linelist.add(data_list);
lines++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
System.out.println("Fatal error"); }
}
Double elements_per_row = sumup/lines;
if(elements_per_row != elements_per_row.intValue()){
/* Filas con diff # de elementos => :( */
System.out.println("\t=> ERROR in arff file!");
System.exit(-1);
}
}
public static void main(String args[]){
parseFile(arff_path);
euclideanDistance(points);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment