Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/LibSVM/PrecomputedKernel.cs @ 2215

Last change on this file since 2215 was 1819, checked in by mkommend, 16 years ago

created new project for LibSVM source files (ticket #619)

File size: 3.0 KB
RevLine 
[1806]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    /// <remarks>
26    /// Class encapsulating a precomputed kernel, where each position indicates the similarity score for two items in the training data.
27    /// </remarks>
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        /// Constructs a <see cref="Problem"/> object using the labels provided.  If a label is set to "0" that item is ignored.
48        /// </summary>
49        /// <param name="rowLabels">The labels for the row items</param>
50        /// <param name="columnLabels">The labels for the column items</param>
51        /// <returns>A <see cref="Problem"/> object</returns>
52        public Problem Compute(double[] rowLabels, double[] columnLabels)
53        {
54            List<Node[]> X = new List<Node[]>();
55            List<double> Y = new List<double>();
56            int maxIndex = 0;
57            for (int i = 0; i < columnLabels.Length; i++)
58                if (columnLabels[i] != 0)
59                    maxIndex++;
60            maxIndex++;
61            for (int r = 0; r < _rows; r++)
62            {
63                if (rowLabels[r] == 0)
64                    continue;
65                List<Node> nodes = new List<Node>();
66                nodes.Add(new Node(0, X.Count + 1));
67                for (int c = 0; c < _columns; c++)
68                {
69                    if (columnLabels[c] == 0)
70                        continue;
71                    double value = _similarities[r, c];
72                    nodes.Add(new Node(nodes.Count, value));
73                }
74                X.Add(nodes.ToArray());
75                Y.Add(rowLabels[r]);
76            }
77            return new Problem(X.Count, Y.ToArray(), X.ToArray(), maxIndex);
78        }
79
80    }
81}
Note: See TracBrowser for help on using the repository browser.