/**
* Copyright (C) 2013-2017 Vasilis Vryniotis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datumbox.framework.core.machinelearning.featureselection;
import com.datumbox.framework.common.Configuration;
import com.datumbox.framework.common.concurrency.StreamMethods;
import com.datumbox.framework.core.common.dataobjects.Dataframe;
import com.datumbox.framework.core.common.dataobjects.Record;
import com.datumbox.framework.common.dataobjects.TypeInference;
import com.datumbox.framework.common.storage.interfaces.StorageEngine;
import com.datumbox.framework.common.storage.interfaces.StorageEngine.MapType;
import com.datumbox.framework.common.storage.interfaces.StorageEngine.StorageHint;
import com.datumbox.framework.core.machinelearning.common.abstracts.AbstractTrainer;
import com.datumbox.framework.core.machinelearning.common.abstracts.featureselectors.AbstractScoreBasedFeatureSelector;
import java.util.*;
import java.util.function.BiFunction;
/**
* Implementation of the TF-IDF Feature Selection algorithm. *
*
* References:
* http://en.wikipedia.org/wiki/Tf%E2%80%93idf
* https://gist.github.com/AloneRoad/1605037
* http://www.tfidf.com/
*
* @author Vasilis Vryniotis
*/
public class TFIDF extends AbstractScoreBasedFeatureSelector {
/** {@inheritDoc} */
public static class ModelParameters extends AbstractScoreBasedFeatureSelector.AbstractModelParameters {
private static final long serialVersionUID = 2L;
/**
* @param storageEngine
* @see AbstractTrainer.AbstractModelParameters#AbstractModelParameters(StorageEngine)
*/
protected ModelParameters(StorageEngine storageEngine) {
super(storageEngine);
}
}
/** {@inheritDoc} */
public static class TrainingParameters extends AbstractScoreBasedFeatureSelector.AbstractTrainingParameters {
private static final long serialVersionUID = 1L;
private boolean binarized = false;
/**
* Getter for the binarized flag; when it is set on the frequencies of the
* activated keywords are clipped to 1.
*
* @return
*/
public boolean isBinarized() {
return binarized;
}
/**
* Setter for the binarized flag; when it is set on the frequencies of the
* activated keywords are clipped to 1.
*
* @param binarized
*/
public void setBinarized(boolean binarized) {
this.binarized = binarized;
}
}
/**
* @param trainingParameters
* @param configuration
* @see AbstractTrainer#AbstractTrainer(AbstractTrainer.AbstractTrainingParameters, Configuration)
*/
protected TFIDF(TrainingParameters trainingParameters, Configuration configuration) {
super(trainingParameters, configuration);
}
/**
* @param storageName
* @param configuration
* @see AbstractTrainer#AbstractTrainer(String, Configuration)
*/
protected TFIDF(String storageName, Configuration configuration) {
super(storageName, configuration);
}
/** {@inheritDoc} */
@Override
public void fit(Dataframe trainingData) {
Set supportedXDataTypes = getSupportedXDataTypes();
for(TypeInference.DataType d : trainingData.getXDataTypes().values()) {
if(!supportedXDataTypes.contains(d)) {
throw new IllegalArgumentException("A DataType that is not supported by this method was detected in the Dataframe.");
}
}
super.fit(trainingData);
}
/** {@inheritDoc} */
@Override
protected void _fit(Dataframe trainingData) {
ModelParameters modelParameters = knowledgeBase.getModelParameters();
TrainingParameters trainingParameters = knowledgeBase.getTrainingParameters();
boolean binarized = trainingParameters.isBinarized();
int n = trainingData.size();
StorageEngine storageEngine = knowledgeBase.getStorageEngine();
Map
Ads help maintain this website.