統計學習中感知機的C++代碼

來源:互聯網
上載者:User

標籤:

感知機是古老的統計學習方法,主要應用於二類線性可分資料,策略是在給定的超平面上對誤差點進行糾正,從而保證所有的點都是正確可分的。

用到的方法是隨機梯度下降法,由於是線性可分的,可保證最終在有限步內收斂。具體可參考李航的《統計學習方法》

#include<iostream>#include<algorithm>#include<vector>#include<fstream>using namespace std;typedef vector<double> feature;typedef int label;class PercepMachine{private:    vector<feature> dataset;    vector<label> labelset;    double learningrate;    double vector_multi (const feature &x, const feature &y)    {        double sum = 0.0;        for (int i = 0; i != x.size(); ++i)        {            sum += x[i] * y[i];        }        return sum;    }    feature vector_multi(double x, const feature &y)    {        feature temp;        for (int i = 0; i != y.size(); ++i)        {            temp.push_back(x*y[i]);        }        return temp;    }    feature vector_add(const feature &x, const feature &y)    {        feature temp(0);        for (int i = 0; i != x.size(); ++i)        {            temp.push_back(x[i] + y[i]);        }        return temp;    }public:    feature w;    double b;    PercepMachine(vector<feature> &traindata, vector<label> &trainlabel, feature &startw, double startb, double rate) :dataset(traindata), labelset(trainlabel), w(startw), b(startb), learningrate(rate){}    void calculate_percep();};void PercepMachine::calculate_percep(){    vector<int> flag(dataset.size(), 1);    while (find(flag.begin(), flag.end(), 1) != flag.end())    {        for (int i = 0; i != dataset.size(); ++i)        {            double multi = vector_multi(dataset[i], w);            if ((multi + b)*labelset[i] <= 0)//有誤分類點            {                flag[i] = 1;                w = vector_add(w, vector_multi(learningrate*labelset[i], dataset[i]));                b = b + learningrate*labelset[i];            }            else            {                flag[i] = 0;            }        }    }}int main(){    ifstream  fin("data.txt");    if (!fin)    {        cout << "can not open the file data.txt" << endl;        exit(1);    }    /* input the dataSet 假設是平面資料,儲存在txt檔案中3列多行,最後一列儲存類別資訊1或-1*/    int feature_dimension = 2;    vector<feature> traindata;    vector<label> trainlabel;    while (!fin.eof())    {        feature temp_data;        double temp;        for (int i = 0; i < feature_dimension; ++i)        {            fin >> temp;            temp_data.push_back(temp);        }        traindata.push_back(temp_data);        label mylabel;        fin >> mylabel;        trainlabel.push_back(mylabel);    }    feature startw(2,1);    double startb = 1.0;    double rate = 0.5;    PercepMachine permachine(traindata, trainlabel, startw, startb, rate);    permachine.calculate_percep();    cout << "w=" << "("<<permachine.w[0] << " " << permachine.w[1]<<")" << endl;    cout << "b=" << permachine.b << endl;    return 0;}

 

統計學習中感知機的C++代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.