/**
* 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.common.dataobjects;
import com.datumbox.framework.common.dataobjects.TypeInference;
import com.datumbox.framework.common.storage.interfaces.StorageEngine;
import com.datumbox.framework.common.utilities.RandomGenerator;
import org.apache.commons.math3.linear.OpenMapRealVector;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.RealVector;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
* The DataframeMatrix class is responsible for converting a Dataframe object to a
Matrix representation. Some of the methods on framework require working with
matrices and this class provides the tools to achieve the necessary conversions.
*
* @author Vasilis Vryniotis
*/
public class DataframeMatrix {
/**
* We create a single storage engine for all DataframeMatrix and MapRealMatrix objects. It is necessary to define it static
* and package protected to make it accessible to other classes such as the MapRealMatrix. This is because
* some methods of the RealMatrix require generating new object without passing the configuration file. The engine
* is created only once to avoid hurting performance. Thus the storageEngine is initialized once in a thread-safe manner.
*/
static StorageEngine storageEngine;
/**
* Keeps record how many usages were made on the storageEngine to avoid conflicting names.
*/
static final AtomicInteger storageId = new AtomicInteger();
private final RealMatrix X;
private final RealVector Y;
/**
* Getter for the X Matrix which contains the data of the Dataframe.
*
* @return
*/
public RealMatrix getX() {
return X;
}
/**
* Getter for the Y vector with the values of the response variables.
*
* @return
*/
public RealVector getY() {
return Y;
}
/**
* Private constructor which accepts as arguments the Y Vector with the values
* of the response variables, the X matrix with the actual data and a
* feature2ColumnId map which provides a mapping between the column name and
* their column id in the data matrix.
*
* @param Y
* @param X
*/
private DataframeMatrix(RealMatrix X, RealVector Y) {
//this constructor must be private because it is used only internally
this.Y = Y;
this.X = X;
}
/**
* Initializes the static storage engine if it's not already set.
*
* @param dataset
*/
private static void setStorageEngine(Dataframe dataset) {
//create a single storage engine for all the MapRealMatrixes
if (storageEngine == null) {
synchronized(DataframeMatrix.class) {
if (storageEngine == null) {
String storageName = "mdf" + RandomGenerator.getThreadLocalRandomUnseeded().nextLong();
storageEngine = dataset.configuration.getStorageConfiguration().createStorageEngine(storageName);
}
}
}
}
/**
* Method used to generate a training Dataframe to a DataframeMatrix and extracts its contents
to Matrixes. It populates the featureIdsReference map with the mappings
* between the feature names and the column ids of the matrix. Typically used
* to convert the training dataset.
*
* @param dataset
* @param addConstantColumn
* @param recordIdsReference
* @param featureIdsReference
* @return
*/
public static DataframeMatrix newInstance(Dataframe dataset, boolean addConstantColumn, Map recordIdsReference, Map
Ads help maintain this website.