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