Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/LibSVM/PrecomputedKernel.cs @ 4272

Last change on this file since 4272 was 2415, checked in by gkronber, 15 years ago

Updated LibSVM project to latest version. #774

File size: 4.5 KB
Line 
1/*
2 * SVM.NET Library
3 * Copyright (C) 2008 Matthew Johnson
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20using System;
21using System.Collections.Generic;
22
23namespace SVM
24{
25    /// <summary>
26    /// Class encapsulating a precomputed kernel, where each position indicates the similarity score for two items in the training data.
27    /// </summary>
28    [Serializable]
29    public class PrecomputedKernel
30    {
31        private float[,] _similarities;
32        private int _rows;
33        private int _columns;
34
35        /// <summary>
36        /// Constructor.
37        /// </summary>
38        /// <param name="similarities">The similarity scores between all items in the training data</param>
39        public PrecomputedKernel(float[,] similarities)
40        {
41            _similarities = similarities;
42            _rows = _similarities.GetLength(0);
43            _columns = _similarities.GetLength(1);
44        }
45
46        /// <summary>
47        /// Constructor.
48        /// </summary>
49        /// <param name="nodes">Nodes for self-similarity analysis</param>
50        /// <param name="param">Parameters to use when computing similarities</param>
51        public PrecomputedKernel(List<Node[]> nodes, Parameter param)
52        {
53            _rows = nodes.Count;
54            _columns = _rows;
55            _similarities = new float[_rows, _columns];
56            for (int r = 0; r < _rows; r++)
57            {
58                for (int c = 0; c < r; c++)
59                    _similarities[r, c] = _similarities[c, r];
60                _similarities[r, r] = 1;
61                for (int c = r + 1; c < _columns; c++)
62                    _similarities[r, c] = (float)Kernel.KernelFunction(nodes[r], nodes[c], param);
63            }
64        }
65
66        /// <summary>
67        /// Constructor.
68        /// </summary>
69        /// <param name="rows">Nodes to use as the rows of the matrix</param>
70        /// <param name="columns">Nodes to use as the columns of the matrix</param>
71        /// <param name="param">Parameters to use when compute similarities</param>
72        public PrecomputedKernel(List<Node[]> rows, List<Node[]> columns, Parameter param)
73        {
74            _rows = rows.Count;
75            _columns = columns.Count;
76            _similarities = new float[_rows, _columns];
77            for (int r = 0; r < _rows; r++)
78                for (int c = 0; c < _columns; c++)
79                    _similarities[r, c] = (float)Kernel.KernelFunction(rows[r], columns[c], param);
80        }
81
82        /// <summary>
83        /// Constructs a <see cref="Problem"/> object using the labels provided.  If a label is set to "0" that item is ignored.
84        /// </summary>
85        /// <param name="rowLabels">The labels for the row items</param>
86        /// <param name="columnLabels">The labels for the column items</param>
87        /// <returns>A <see cref="Problem"/> object</returns>
88        public Problem Compute(double[] rowLabels, double[] columnLabels)
89        {
90            List<Node[]> X = new List<Node[]>();
91            List<double> Y = new List<double>();
92            int maxIndex = 0;
93            for (int i = 0; i < columnLabels.Length; i++)
94                if (columnLabels[i] != 0)
95                    maxIndex++;
96            maxIndex++;
97            for (int r = 0; r < _rows; r++)
98            {
99                if (rowLabels[r] == 0)
100                    continue;
101                List<Node> nodes = new List<Node>();
102                nodes.Add(new Node(0, X.Count + 1));
103                for (int c = 0; c < _columns; c++)
104                {
105                    if (columnLabels[c] == 0)
106                        continue;
107                    double value = _similarities[r, c];
108                    nodes.Add(new Node(nodes.Count, value));
109                }
110                X.Add(nodes.ToArray());
111                Y.Add(rowLabels[r]);
112            }
113            return new Problem(X.Count, Y.ToArray(), X.ToArray(), maxIndex);
114        }
115
116    }
117}
Note: See TracBrowser for help on using the repository browser.