001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.core.algorithm.relationmining;
025
026 import java.io.File;
027 import java.util.ArrayList;
028 import java.util.Collection;
029 import java.util.HashSet;
030 import java.util.Iterator;
031 import java.util.List;
032 import java.util.Set;
033
034 import org.jdmp.core.dataset.RelationalDataSet;
035 import org.jdmp.core.sample.RelationalSample;
036 import org.jdmp.core.sample.Sample;
037 import org.jdmp.core.sample.SampleFactory;
038 import org.ujmp.core.Matrix;
039 import org.ujmp.core.MatrixFactory;
040 import org.ujmp.core.enums.FileFormat;
041 import org.ujmp.core.mapmatrix.DefaultMapMatrix;
042 import org.ujmp.core.mapmatrix.MapMatrix;
043 import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix2D;
044
045 public class MarketBasketAnalysis extends AbstractRelationMiner {
046
047 private final MapMatrix product1ToIds = new DefaultMapMatrix();
048
049 private final MapMatrix product2ToIds = new DefaultMapMatrix();
050
051 private int minSupport = 10;
052
053 public RelationalDataSet calculate(RelationalDataSet dataSet) throws Exception {
054 product1ToIds.setLabel("Product 1 Ids");
055 product2ToIds.setLabel("Product 2 Ids");
056
057 Matrix product1Count = new CountMatrix(product1ToIds);
058 product1Count.setLabel("Product 1 Count");
059
060 Matrix product2Count = new CountMatrix(product2ToIds);
061 product2Count.setLabel("Product 2 Count");
062
063 for (int r = 0; r < dataSet.getSamples().getSize(); r++) {
064
065 if (r % 1000 == 0) {
066 System.out.println(r + " of " + dataSet.getSamples().getSize());
067 }
068 RelationalSample s = (RelationalSample) dataSet.getSamples().getElementAt(r);
069 Collection<?> products = s.getObjects();
070 if (products.size() != 0) {
071 addProduct1Count(products, r);
072 addProduct2Count(products, r);
073 }
074 }
075
076 return calculateP(minSupport);
077 }
078
079 public static void main(String[] args) throws Exception {
080 Matrix data = MatrixFactory.linkToFile(FileFormat.CSV, new File(
081 "/home/arndt/muenchen/totale2.txt"));
082 // data.showGUI();
083
084 RelationalDataSet orig = new RelationalDataSet();
085
086 // for (int r = 0; r < 10000; r++) {
087 for (int r = 0; r < data.getRowCount(); r++) {
088
089 if (r % 1000 == 0) {
090 System.out.println(r + " of " + data.getRowCount());
091 }
092
093 Collection<?> products = getProductsInLine(data, r);
094 if (products.size() != 0) {
095 RelationalSample s = SampleFactory.relationalSample(products);
096 orig.getSamples().add(s);
097 }
098 }
099
100 MarketBasketAnalysis mba = new MarketBasketAnalysis();
101 orig.showGUI();
102 RelationalDataSet ds = mba.calculate(orig);
103 ds.showGUI();
104 }
105
106 private RelationalDataSet calculateP(int minSupport) throws Exception {
107 RelationalDataSet ds = new RelationalDataSet();
108
109 for (Object o : product2ToIds.keySet()) {
110 Set<String> set = (Set<String>) (o);
111 int count = ((List) product2ToIds.get(set)).size();
112 if (count >= minSupport) {
113
114 System.out.println("===========================");
115 System.out.println(set + ": " + count);
116 Iterator it = set.iterator();
117 String prod1 = (String) it.next();
118 String prod2 = (String) it.next();
119
120 double p1 = getP(prod1, prod2);
121 double p2 = getP(prod2, prod1);
122
123 System.out.println(prod1 + " => " + prod2 + ": " + p1);
124 System.out.println(prod2 + " => " + prod1 + ": " + p2);
125
126 RelationalSample s12 = SampleFactory.relationalSample(prod1 + " => " + prod2);
127 s12.addObject(prod1);
128 s12.addObject(prod2);
129 s12.setMatrix(Sample.COUNT, MatrixFactory.linkToValue(count));
130 s12.setMatrix(Sample.PROBABILITY, MatrixFactory.linkToValue(p1));
131 s12.setObject("From", prod1);
132 s12.setObject("To", prod2);
133 ds.getSamples().add(s12);
134
135 RelationalSample s21 = SampleFactory.relationalSample(prod2 + " => " + prod1);
136 s21.addObject(prod1);
137 s21.addObject(prod2);
138 s21.setMatrix(Sample.COUNT, MatrixFactory.linkToValue(count));
139 s21.setMatrix(Sample.PROBABILITY, MatrixFactory.linkToValue(p2));
140 s21.setObject("From", prod2);
141 s21.setObject("To", prod1);
142 ds.getSamples().add(s21);
143
144 }
145 }
146
147 return ds;
148 }
149
150 private int getCount(String p1, String p2) {
151 Set<String> set = new HashSet<String>(2);
152 set.add(p1.intern());
153 set.add(p2.intern());
154 List<Integer> ids = (List<Integer>) product2ToIds.get(set);
155
156 if (ids != null) {
157 return ids.size();
158 } else {
159 return 0;
160 }
161 }
162
163 private double getP(String p1, String p2) {
164 List<Integer> idsSingle = (List<Integer>) product1ToIds.get(p1.intern());
165 int countTotal = 0;
166 if (idsSingle != null) {
167 countTotal = idsSingle.size();
168 }
169 int countPairwise = getCount(p1, p2);
170 return (double) countPairwise / countTotal;
171
172 }
173
174 private void addProduct1Count(Collection<?> products, int row) {
175 // products must occur only once in set
176 products = new HashSet<Object>(products);
177
178 // skip sets with only one product
179 if (products.size() < 2) {
180 return;
181 }
182
183 for (Object s : products) {
184 List<Integer> ids = (List<Integer>) product1ToIds.get(s);
185 if (ids == null) {
186 ids = new ArrayList<Integer>(1);
187 }
188 ids.add(row);
189 product1ToIds.put(s, ids);
190 }
191 }
192
193 private List<Set<?>> getPerm2(Collection<?> products) {
194 List<?> productList = new ArrayList<Object>(products);
195 List<Set<?>> permList = new ArrayList<Set<?>>();
196
197 for (int i1 = 0; i1 < productList.size(); i1++) {
198 for (int i2 = i1 + 1; i2 < productList.size(); i2++) {
199 Set<Object> product2Set = new HashSet<Object>(2);
200 product2Set.add(productList.get(i1));
201 product2Set.add(productList.get(i2));
202 permList.add(product2Set);
203 }
204 }
205
206 return permList;
207 }
208
209 private void addProduct2Count(Collection<?> products, int row) {
210 // products must occur only once in set
211 products = new HashSet<Object>(products);
212
213 if (products.size() < 2) {
214 return;
215 }
216
217 List<Object> productList = new ArrayList<Object>(products);
218
219 List<Set<?>> perm2 = getPerm2(products);
220
221 for (Collection<?> s : perm2) {
222 List<Integer> ids = (List<Integer>) product2ToIds.get(s);
223 if (ids == null) {
224 ids = new ArrayList<Integer>(1);
225 }
226 ids.add(row);
227 product2ToIds.put(s, ids);
228 }
229 }
230
231 private static Collection<?> getProductsInLine(Matrix data, long row) {
232 Set<Object> products = new HashSet<Object>();
233 for (int c = 1; c < data.getColumnCount(); c++) {
234 String prod = data.getAsString(row, c);
235 if (prod != null) {
236 prod = prod.trim().intern();
237 if (prod.length() != 0) {
238 products.add(prod);
239 }
240 }
241 }
242 return products;
243 }
244
245 public int getMinSupport() {
246 return minSupport;
247 }
248
249 public void setMinSupport(int minSupport) {
250 this.minSupport = minSupport;
251 }
252
253 }
254
255 class CountMatrix extends AbstractDenseObjectMatrix2D {
256 private static final long serialVersionUID = -3985951298755499721L;
257
258 Matrix matrix = null;
259
260 public CountMatrix(Matrix source) {
261 this.matrix = source;
262 }
263
264 public Object getObject(int row, int column) {
265 return getObject((long) row, (long) column);
266 }
267
268 @Override
269 public Object getObject(long row, long column) {
270 Object o = matrix.getAsObject(row, column);
271 if (column == 0) {
272 return o;
273 } else {
274 return ((Collection) o).size();
275 }
276 }
277
278 @Override
279 public void setObject(Object value, long row, long column) {
280 }
281
282 @Override
283 public void setObject(Object value, int row, int column) {
284 }
285
286 @Override
287 public long[] getSize() {
288 return matrix.getSize();
289 }
290
291 }