From b6e486158d5ae64244a314e7a73e8c7b01e67e5f Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 25 Apr 2019 15:27:09 +0200 Subject: [PATCH] WeekOverview analysis working --- .gitignore | 3 +- .../java/de/hsel/spm/baudas/WekaBeispiel.java | 116 ------------------ .../de/hsel/spm/baudas/analysis/Analysis.java | 6 +- .../spm/baudas/analysis/WeekOverview.java | 68 +++++----- .../{ => Analysis}/WeekOverviewTest.java | 10 +- .../de/hsel/spm/baudas/WekaBeispiel.class | Bin 3103 -> 3091 bytes .../hsel/spm/baudas/analysis/Analysis.class | Bin 1068 -> 1059 bytes .../spm/baudas/analysis/WeekOverview.class | Bin 2807 -> 2663 bytes .../de/hsel/spm/baudas/WeekOverviewTest.class | Bin 2032 -> 2146 bytes 9 files changed, 39 insertions(+), 164 deletions(-) delete mode 100644 src/main/java/de/hsel/spm/baudas/WekaBeispiel.java rename src/test/java/de/hsel/spm/baudas/{ => Analysis}/WeekOverviewTest.java (63%) diff --git a/.gitignore b/.gitignore index f85356d..21399b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.iml .idea -.project \ No newline at end of file +.project +target \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/WekaBeispiel.java b/src/main/java/de/hsel/spm/baudas/WekaBeispiel.java deleted file mode 100644 index 47daa20..0000000 --- a/src/main/java/de/hsel/spm/baudas/WekaBeispiel.java +++ /dev/null @@ -1,116 +0,0 @@ -package de.hsel.spm.baudas; - -/* Beispielprogramm, um WeKa in eclipse zu verwenden. - - Die Rohdaten liegen im CSV-Format vor. - - Die Rohdaten enthalten die folgenden 26 Attribute, - 0..9 Kundendaten und Einkaufsverhalten - 10 Einkaufssumme - 11..25 gekaufte Waren - - Hier sind nur ein paar Beispiele zu sehen, wie man die Analysen verwenden kann. - Weitere Einstellungen (falls n�tig) selbst recherchieren! - */ - -import java.io.*; - -import weka.associations.Apriori; -import weka.clusterers.SimpleKMeans; -import weka.core.Instances; -import weka.core.converters.CSVLoader; -import weka.filters.Filter; -import weka.filters.unsupervised.attribute.NumericCleaner; -import weka.filters.unsupervised.attribute.NumericToNominal; - -public class WekaBeispiel { - - void findCluster(Instances daten, int anz) throws Exception { - - SimpleKMeans model = new SimpleKMeans(); - model.setNumClusters(anz); - - model.buildClusterer(daten); - System.out.println(model); - - } - - void makeApriori(Instances daten) throws Exception { - - // umwandeln in gekauft / nicht gekauft (0/1) - NumericCleaner nc = new NumericCleaner(); - nc.setMaxThreshold(1.0); // Schwellwert auf 1 setzen - nc.setMaxDefault(1.0); // alles ueber Schwellwert durch 1 ersetzen - nc.setInputFormat(daten); - daten = Filter.useFilter(daten, nc); // Filter anwenden. - - // Die Daten als nominale und nicht als numerische Daten setzen - NumericToNominal num2nom = new NumericToNominal(); - num2nom.setAttributeIndices("first-last"); - num2nom.setInputFormat(daten); - daten = Filter.useFilter(daten, num2nom); - - Apriori model = new Apriori(); - - model.buildAssociations(daten); - - System.out.println(model); - - } - - public static void main(String[] args) throws Exception { - - // Eigenen Dateipfad eintragen, nicht meinen nehmen ;-) - String path = "/home/julian/IntellijProjects/Softwareprojektmanagement/src/test/resources/"; - String roh = path + "kd100.csv"; - - Instances alleDaten, nurWaren, nurKunden; - - - WekaBeispiel dt = new WekaBeispiel(); - - - // CSV-Datei laden - CSVLoader loader = new CSVLoader(); - loader.setSource(new File(roh)); - alleDaten = loader.getDataSet(); - - // 0 durch ? ersetzen, um f�r die Auswertung nur die Waren zu - // ber�cksichtigen, die gekauft wurden - NumericCleaner nc = new NumericCleaner(); - nc.setMinThreshold(1.0); // Schwellwert auf 1 setzen - nc.setMinDefault(Double.NaN); // alles unter Schwellwert durch ? ersetzen - nc.setInputFormat(alleDaten); - alleDaten = Filter.useFilter(alleDaten, nc); // Filter anwenden. Wichtig: alleDaten = - - - - // Clusteranalyse mit 5 Clustern �ber alle Daten - dt.findCluster(alleDaten, 5); - - // Assoziationsanalyse �ber alle Daten - Sinn zweifelhaft ;) - dt.makeApriori(alleDaten); - - - // Kundendaten rausnehmen, nur Warenk�rbe stehen lassen - nurWaren = new Instances(alleDaten); - for (int i = 0; i < 11; i++) { - nurWaren.deleteAttributeAt(0); // ein einzelnes Attribut rausnehmen - } - // Assoziationsanalyse der gekauften Waren - dt.makeApriori(nurWaren); - - - - // Waren rausnehmen, nur Kundendaten stehen lassen - nurKunden = new Instances(alleDaten); - for (int i = 0; i < 16; i++) { - nurKunden.deleteAttributeAt(10); // ein einzelnes Attribut rausnehmen - } - - // Clusteranalyse mit 3 Clustern �ber die Kundendaten - dt.findCluster(nurKunden, 3); - - } - -} diff --git a/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java b/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java index b8bc054..3f8f86a 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java @@ -8,7 +8,7 @@ import java.io.File; import java.io.IOException; /** - * base interface for analysis + * base interface for analysis. * * @author Johannes Theiner * @version 0.1 @@ -19,7 +19,7 @@ public interface Analysis { /** - * loads data from csv file + * loads data from csv file. * * @param file File to analyze * @return loaded data in weka format @@ -37,7 +37,7 @@ public interface Analysis { } /** - * result of the analysis + * result of the analysis. * * @return result as T */ diff --git a/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java b/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java index 081f4f9..2499a39 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java @@ -1,73 +1,63 @@ package de.hsel.spm.baudas.analysis; -import org.jetbrains.annotations.Nullable; import weka.core.Instance; import weka.core.Instances; -import weka.filters.Filter; -import weka.filters.unsupervised.attribute.Remove; import java.io.File; +import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; /** - * Week Overview Analysis + * Week Overview Analysis. * * @author Julian Hinxlage * @version 0.1 * @since 0.1 **/ -public class WeekOverview implements Analysis> { +public class WeekOverview implements Analysis>> { private Instances instances; - private Map result; + private Map> result; public WeekOverview(File file) { - result = new HashMap(); + result = new HashMap<>(); instances = load(file); } @Override - public Map getResult() { + public Map> getResult() { - int[] keepIndexes = new int[]{5, 10}; - int dayIndex = 0; - int amountIndex = 1; + int dayIndex = 5; + int amountIndex = 10; + + int startArticles = 11; + int endArticles = 25; - try { - //Remove all other attributes - Remove remove = new Remove(); - remove.setAttributeIndicesArray(keepIndexes); - remove.setInvertSelection(true); - remove.setInputFormat(instances); - instances = Filter.useFilter(instances, remove); - } catch (Exception ex) { - ex.printStackTrace(); - } + for (int i = 0; i < instances.numInstances(); i++) { + Instance instance = instances.get(i); + double amount = instance.value(amountIndex); + String day = instance.stringValue(dayIndex); - for (int i = 0; i < instances.attribute(dayIndex).numValues(); i++) { - String day = instances.attribute(dayIndex).value(i); - - System.out.println(day); - - - for (int j = 0; j < instances.attribute(amountIndex).numValues(); j++) { - String amount = instances.attribute(amountIndex).value(j); - - System.out.println(amount); - - int x = Integer.parseInt(amount); - - if(!result.containsKey(day)){ - result.put(day,0); - } - result.put(day,result.get(day) + x); - + int count = 0; + for (int j = startArticles; j < endArticles; j++) { + count += (int) instance.value(j); } + + if (!result.containsKey(day)) { + result.put(day, new AbstractMap.SimpleEntry<>(0.0, 0)); + } + result.put(day, + new AbstractMap.SimpleEntry<>( + result.get(day).getKey() + amount, + result.get(day).getValue() + count + ) + ); } + return result; } } diff --git a/src/test/java/de/hsel/spm/baudas/WeekOverviewTest.java b/src/test/java/de/hsel/spm/baudas/Analysis/WeekOverviewTest.java similarity index 63% rename from src/test/java/de/hsel/spm/baudas/WeekOverviewTest.java rename to src/test/java/de/hsel/spm/baudas/Analysis/WeekOverviewTest.java index 9e285f5..bc9265e 100644 --- a/src/test/java/de/hsel/spm/baudas/WeekOverviewTest.java +++ b/src/test/java/de/hsel/spm/baudas/Analysis/WeekOverviewTest.java @@ -1,4 +1,4 @@ -package de.hsel.spm.baudas; +package de.hsel.spm.baudas.Analysis; import de.hsel.spm.baudas.analysis.WeekOverview; import org.junit.jupiter.api.Test; @@ -17,13 +17,13 @@ import java.util.Map; public class WeekOverviewTest { @Test - public void test(){ + public void test() { WeekOverview overview = new WeekOverview(new File(getClass().getClassLoader().getResource("kd100.csv").getFile())); - Map result = overview.getResult(); + Map> result = overview.getResult(); - for(Map.Entry i : result.entrySet()){ - System.out.println(i.getKey() + " = " + i.getValue()); + for (Map.Entry> i : result.entrySet()) { + System.out.println(i.getKey() + " = " + i.getValue().getKey() + "€ " + i.getValue().getValue()); } } diff --git a/target/classes/de/hsel/spm/baudas/WekaBeispiel.class b/target/classes/de/hsel/spm/baudas/WekaBeispiel.class index f5ed3d560410ae79cd14221cf29a06956c96abdb..f12d6daf1334b6484931f6b076102fe17fe02d82 100644 GIT binary patch delta 446 zcmXYtOHY(B6o#M9d_zBKgCr=BKqj~{VG?ecoe9RcAnpl)L6qA7!f;nWR0fs%O`w9x zMTEb=jsLQ3#j(2~m= z-WpnI)3h5xISc6!bPCdjF1j^6hCEUs??ihAX-%J@KoSbbg!F3$3`G(bF&HxBwVq+k zh@n(Uj~e2l?+s;qkYG$OE@dV(A9J$*eXD+mF)2x(1fMlu3<;(LS z-K*eX^e0=cIf$!mHyLCe{o=PGx39Chs;Z)6bjiGUafz0fa1AdjeZ}Y=@z9E BQZfJl delta 479 zcmYk2Nl#Qk5P-ktJ*Y>gHN-LD9n8d=CL|0dlX%mZn0V8RK@=Sw77=(3>Nw->sGyF! zLn}IN;Q9x+k?7fY^?z{kU_7W@oaj`4)wOk1%BPEIla?u_h0L;%Qo?D~6~&}5W65bjjxz~og>#l(wR@>1oDcdS zHKA_VqOC7j@~SUdwsJ|0%Ze-7=BjY5tMNOG8jd=yYf(dSL%3<_I zqds4Z)958_e<04ASH2Qwn_u(B&^%D$=rhgbh0a3P{vlxq;jnN-7&h>SP5;8~tS~4XS`*D# Vll56Sc7?6P7wB&l(`rf8B{=oDu_^HXHe&2U}ex?XV7G4(Aucw#>l8W zxr*^QuMUGQ4}%_qJ`aNdgW=>nCJ8np23?@miR)B0Z(wp_v|?vxkYooM!N?BOF9kH8 g4akvZU|?ZnkYSJovgLqcObqfs3}GoSD1t?l0J4J-mjD0& delta 171 zcmZ3?v4%tI)W2Q(7#J8#7|hri)VLTJ8Pq|928hsPXVBteU}ex|XV771(A}uz#>fR^ z=m8Z1WhNIgKG)D^FyLV@WH91kFlI1eWDw3uEKAhS%-8qycdbZHEhx#%&*Nb*WiZ&> zz?8{oCCSbp#m*qjz{t)Z!ypT^l?`Z%90LOjBZEAH0+6i;6k}pg0%8bDnL!0Cq6z?c CU>9fr diff --git a/target/classes/de/hsel/spm/baudas/analysis/WeekOverview.class b/target/classes/de/hsel/spm/baudas/analysis/WeekOverview.class index 161304aba26b8223d0781ecc5c3d5ff3f7eb0b3d..e2882cddeefe962d3cfd1442a20e5283a20cc4e5 100644 GIT binary patch literal 2663 zcmb_eOK%fb6#nkmw>B)7VVZw5J-`#F1o5zshd`+RHy;zdIgd8zRI4TRwB|x#xWM-0z%o z$3Oq~{SN?M#%DUZaYVyW9SV-bu@=X4ikuw1s^d6@r8r?=KTb+9V&D{B(=e)|6R+#& z!k7*dXQlcLDaJLNGthxIHJq0=Z^_Bq8ZJoNixS(?Frgu@p&$@<+_G=EdAlr-7`|-J zSebmuvopDxx=$eH+2u;n7ib%vw&tx&#dnIC6V@D;;-k)_Yxxz=7ElknzIRo?%(<@Z z9V%MovP8Ym?9^YK7AqUG0k62prw`qfzD+zAP;EGmMSh2Ach1Y1&btiRN)$FWCrq~nL|{X<=ldO zMZl=KhqG4Z+w%uK-^mwcehk|!)Mu@uzLlS0asxs%*cvVgSPzHan4YJU#m?Fu!I?%T zrtQ4XvtMsGXsCQ_at6aJg%Q?j^{EFe3r`;{RlK|{YmLR%SQgu5qnOx?NiNiz=9pEU zBDW@g!$&g(08?A&jtj6RDxb{leQ+V0CjGwL4W{ z>oQw41RQpuWH>TcArHHs%>Gdbfi26pHU|6P8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ETN`;K(vIW(UjaI)T_|>mWzl)yoCNV7}rj627#Bv=m>?Deh62YCsZWnRfO%$KO;~2+QSlGr9zXS7l z66ev2Z)kM^PvH_RZ_x5r>i(eZpS&TNDWZvf*VEr)#D6y;)NSSNJdW^xz-|1e?g9qT zjqPZF2cNgMBg!ae>3d-CzcX?lb85kFjK7@>w=r@*cVWH-yrJFV zgH#Q>Gz=P!~Re;3-$4*bT# diff --git a/target/test-classes/de/hsel/spm/baudas/WeekOverviewTest.class b/target/test-classes/de/hsel/spm/baudas/WeekOverviewTest.class index c8b72a6daf257e77b09e50a5c22582cf4d1b37c1..27d29b557ca6bcb30026ac75338be55b2144288d 100644 GIT binary patch delta 707 zcmaKpOHWf#6otQYTY8|J#6kc|c@-<72owZGtTGT2L_k!0B8mlSdD&{A_!f2M#H8N2 ziHV65C&Z+RF~+eY{Q*w+1&$q{`xFyr-ka>T*V=pEz0SEmw`~N1UpGH~2D*9Tr=DKT zAx%uuT(D`hi-A&(GgvC+6E+NKhD#XXq-NAlIlZL>IHfsl$(drtEI(_>xnj;+yx`{| zmo(#=%WKanluEcSRh#=-g&L2gvr~(S*zkOOB_3Ujr;^dZ)N&%3$i({ORZO}+RI}}@ z`A?@Ca4NkMnk#OrGv#R@?B<-qBUhQ!TrMCBrgVH|z=eRt&eff$Bww#$uB@OquukeJA zgbfeKTK-Vs-FW&|gh$M3Rt=AtQ$%v#Bkw+VJ~?%ps;a%~ z1Qi8YP~E+O_!$om;AZ?BHJS$5OnA%#DnY+ M^otsxi`~AGKZ3b-tpET3 delta 644 zcmXYt%Tg0j5Qe`qksOm8C3qu%LNp?Xk_1H%F$-aV7f>`HDxgjRVn_^_V7!C4c4N&G zxN=Qd6{}oWt}UO!CvdG*XrIJcboYOmzkB9q_*Y}(`}UWwKtFGT9AZdwPBSdy`$*^b z7#Bla;&O;9jN3AyxfETtY&WO-9Dwx zdmmJ9TW+Vpn?2&E_ZE)hRbqSghSJues~3I~jjF9g1%6g6j5m`y8M2B%exW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