LinearClassifier.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "classifier/LinearClassifier.h"
00012 #include "base/Parameter.h"
00013 
00014 using namespace shogun;
00015 
00016 CLinearClassifier::CLinearClassifier()
00017 : CClassifier(), w_dim(0), w(NULL), bias(0), features(NULL)
00018 {
00019 
00020     m_parameters->add_vector(&w, &w_dim, "w", "Parameter vector w.");
00021     m_parameters->add(&bias, "bias", "Bias b.");
00022     m_parameters->add((CSGObject**) &features, "features", "Feature object.");
00023 
00024 }
00025 
00026 CLinearClassifier::~CLinearClassifier()
00027 {
00028     delete[] w;
00029     SG_UNREF(features);
00030 }
00031 
00032 bool CLinearClassifier::load(FILE* srcfile)
00033 {
00034     SG_SET_LOCALE_C;
00035     SG_RESET_LOCALE;
00036     return false;
00037 }
00038 
00039 bool CLinearClassifier::save(FILE* dstfile)
00040 {
00041     SG_SET_LOCALE_C;
00042     SG_RESET_LOCALE;
00043     return false;
00044 }
00045 
00046 CLabels* CLinearClassifier::classify()
00047 {
00048     if (features)
00049     {
00050         int32_t num=features->get_num_vectors();
00051         ASSERT(num>0);
00052         ASSERT(w_dim==features->get_dim_feature_space());
00053 
00054         float64_t* out=new float64_t[num];
00055         features->dense_dot_range(out, 0, num, NULL, w, w_dim, bias);
00056 
00057         CLabels* output=new CLabels(num);
00058         output->set_labels(out, num);
00059 
00060         delete[] out;
00061 
00062         return output;
00063     }
00064 
00065     return NULL;
00066 }
00067 
00068 CLabels* CLinearClassifier::classify(CFeatures* data)
00069 {
00070     if (!data)
00071         SG_ERROR("No features specified\n");
00072     if (!data->has_property(FP_DOT))
00073         SG_ERROR("Specified features are not of type CDotFeatures\n");
00074     set_features((CDotFeatures*) data);
00075     return classify();
00076 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation