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.classification.bayes;
025
026 import java.util.HashSet;
027 import java.util.Set;
028
029 import org.jdmp.core.algorithm.classification.AbstractClassifier;
030 import org.jdmp.core.algorithm.classification.Classifier;
031 import org.jdmp.core.algorithm.estimator.DensityEstimator;
032 import org.jdmp.core.algorithm.estimator.DiscreteDensityEstimator;
033 import org.jdmp.core.dataset.RegressionDataSet;
034 import org.ujmp.core.Matrix;
035 import org.ujmp.core.MatrixFactory;
036 import org.ujmp.core.calculation.Calculation.Ret;
037 import org.ujmp.core.exceptions.MatrixException;
038
039 public class NaiveBayesClassifierTest extends AbstractClassifier {
040
041 private static final long serialVersionUID = -6511197233753905836L;
042
043 private DensityEstimator[][] dists = null;
044
045 private DensityEstimator classDists = null;
046
047 private int labelCount = 0;
048
049 // private double[][][] conditionals = null;
050
051 // private double[] classDist = null;
052
053 public NaiveBayesClassifierTest() {
054 super();
055 }
056
057 @Override
058 public Matrix predict(Matrix input, Matrix sampleWeight) throws Exception {
059 double[] logs = new double[labelCount];
060 long featureCount = input.getColumnCount();
061 // for all classes
062 for (int labelId = 0; labelId < labelCount; labelId++) {
063 // for all features
064 logs[labelId] = Math.log(classDists.getProbability(labelId));
065 for (int featureId = 0; featureId < featureCount; featureId++) {
066 int val = (int) input.getAsDouble(0, featureId);
067 // logs[labelId] +=
068 // Math.log(dists[featureId][labelId].getProbability(val));
069 logs[labelId] += dists[featureId][labelId].getProbability(val);
070 }
071 }
072 double[] probs = logToProbs(logs);
073 Matrix m = MatrixFactory.zeros(1, 2);
074 m.setAsDouble(probs[0], 0, 1);
075 m.setAsDouble(probs[1], 0, 0);
076 return m;
077 // Matrix r=MatrixFactory.fromArray(probs).transpose();
078 }
079
080 /**
081 * @todo Put in common class!
082 * @param logs
083 * @return
084 */
085 public static double[] logToProbs(double[] logs) {
086 double[] probs = new double[logs.length];
087 double sum = 0.0;
088 for (int i = 0; i < probs.length; i++) {
089 probs[i] = Math.exp(logs[i]);
090
091 sum += probs[i];
092 }
093 for (int i = 0; i < probs.length; i++) {
094 probs[i] = probs[i] / sum;
095 }
096 return probs;
097 }
098
099 @Override
100 public void reset() throws Exception {
101 // TODO Auto-generated method stub
102
103 }
104
105 @Override
106 public void train(RegressionDataSet dataSet) throws Exception {
107 // labelCount = ((ClassificationDataSet) dataSet).getClassCount();
108 // TODO: hack!!!
109 labelCount = 2;
110 int sampleCount = dataSet.getSamples().getSize();
111 int featureCount = dataSet.getFeatureCount();
112 Matrix featureMatrix = dataSet.getInputMatrix();
113 Matrix labelMatrix = dataSet.getTargetMatrix();
114 Matrix maxMatrix = MatrixFactory.zeros(1, featureCount);
115
116 for (int featureId = 0; featureId < featureCount; featureId++) {
117 Matrix feature = featureMatrix.selectColumns(Ret.LINK, featureId);
118 double max = 0;
119 for (long[] c : feature.availableCoordinates()) {
120 double v = feature.getAsDouble(c);
121 if (v > max) {
122 max = v;
123 }
124 }
125 maxMatrix.setAsDouble(max, 0, featureId);
126 }
127
128 this.dists = new DensityEstimator[featureCount][labelCount];
129 this.classDists = new DiscreteDensityEstimator(labelCount, true);
130
131 for (int featureId = 0; featureId < featureCount; featureId++) {
132 for (int labelId = 0; labelId < labelCount; labelId++) {
133 dists[featureId][labelId] = new DiscreteDensityEstimator((int) maxMatrix
134 .getAsDouble(0, featureId) + 1, true);
135 }
136 }
137
138 // go over all samples and count
139
140 // for (int sampleId = 0; sampleId < sampleCount; sampleId++) {
141 // if (sampleId % 1000 == 0) {
142 // System.out.println("Training on Sample " + sampleId);
143 // }
144 // int outputVal = (int) labelMatrix.getAsDouble(sampleId, 0);
145 // for (int featureId = 0; featureId < featureCount; featureId++) {
146 // int inputVal = (int) featureMatrix.getAsDouble(sampleId, featureId);
147 // dists[featureId][outputVal].addValue(inputVal);
148 // }
149 // classDists.addValue(outputVal);
150 // }
151
152 for (int sampleId = 0; sampleId < sampleCount; sampleId++) {
153 int outputVal = (int) labelMatrix.getAsDouble(sampleId, 0);
154 classDists.addValue(outputVal);
155 }
156
157 for (int featureId = 0; featureId < featureCount; featureId++) {
158 if (featureId % 1000 == 0) {
159 System.out.println("Training on Feature " + featureId);
160 }
161
162 Matrix feature = featureMatrix.selectColumns(Ret.LINK, featureId);
163 Set<Integer> nonZeros = new HashSet<Integer>();
164 for (long[] c : feature.availableCoordinates()) {
165 nonZeros.add((int) c[ROW]);
166 }
167 for (long[] c : labelMatrix.availableCoordinates()) {
168 nonZeros.add((int) c[ROW]);
169 }
170
171 // for (int sampleId = 0; sampleId < sampleCount; sampleId++) {
172 // int outputVal = (int) labelMatrix.getAsDouble(sampleId, 0);
173 // int inputVal = (int) featureMatrix.getAsDouble(sampleId,
174 // featureId);
175 // dists[featureId][outputVal].addValue(inputVal);
176 // }
177
178 for (int sampleId : nonZeros) {
179 int outputVal = (int) labelMatrix.getAsDouble(sampleId, 0);
180 int inputVal = (int) featureMatrix.getAsDouble(sampleId, featureId);
181 dists[featureId][outputVal].addValue(inputVal);
182 }
183 dists[featureId][0].addValue(0, sampleCount - nonZeros.size());
184
185 }
186
187 }
188
189 @Override
190 public void train(Matrix input, Matrix sampleWeight, Matrix targetOutput) throws Exception {
191 throw new MatrixException("sample by sample learning is not supported");
192 }
193
194 public Classifier emptyCopy() {
195 return new NaiveBayesClassifierTest();
196 }
197
198 }