/*
* SVM.NET Library
* Copyright (C) 2008 Matthew Johnson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Collections.Generic;
namespace SVM
{
///
/// Class encapsulating a precomputed kernel, where each position indicates the similarity score for two items in the training data.
///
[Serializable]
public class PrecomputedKernel
{
private float[,] _similarities;
private int _rows;
private int _columns;
///
/// Constructor.
///
/// The similarity scores between all items in the training data
public PrecomputedKernel(float[,] similarities)
{
_similarities = similarities;
_rows = _similarities.GetLength(0);
_columns = _similarities.GetLength(1);
}
///
/// Constructor.
///
/// Nodes for self-similarity analysis
/// Parameters to use when computing similarities
public PrecomputedKernel(List nodes, Parameter param)
{
_rows = nodes.Count;
_columns = _rows;
_similarities = new float[_rows, _columns];
for (int r = 0; r < _rows; r++)
{
for (int c = 0; c < r; c++)
_similarities[r, c] = _similarities[c, r];
_similarities[r, r] = 1;
for (int c = r + 1; c < _columns; c++)
_similarities[r, c] = (float)Kernel.KernelFunction(nodes[r], nodes[c], param);
}
}
///
/// Constructor.
///
/// Nodes to use as the rows of the matrix
/// Nodes to use as the columns of the matrix
/// Parameters to use when compute similarities
public PrecomputedKernel(List rows, List columns, Parameter param)
{
_rows = rows.Count;
_columns = columns.Count;
_similarities = new float[_rows, _columns];
for (int r = 0; r < _rows; r++)
for (int c = 0; c < _columns; c++)
_similarities[r, c] = (float)Kernel.KernelFunction(rows[r], columns[c], param);
}
///
/// Constructs a object using the labels provided. If a label is set to "0" that item is ignored.
///
/// The labels for the row items
/// The labels for the column items
/// A object
public Problem Compute(double[] rowLabels, double[] columnLabels)
{
List X = new List();
List Y = new List();
int maxIndex = 0;
for (int i = 0; i < columnLabels.Length; i++)
if (columnLabels[i] != 0)
maxIndex++;
maxIndex++;
for (int r = 0; r < _rows; r++)
{
if (rowLabels[r] == 0)
continue;
List nodes = new List();
nodes.Add(new Node(0, X.Count + 1));
for (int c = 0; c < _columns; c++)
{
if (columnLabels[c] == 0)
continue;
double value = _similarities[r, c];
nodes.Add(new Node(nodes.Count, value));
}
X.Add(nodes.ToArray());
Y.Add(rowLabels[r]);
}
return new Problem(X.Count, Y.ToArray(), X.ToArray(), maxIndex);
}
}
}