Robot Learning Cornerstone Machine Learn Cornerstone (machines learining foundations) Job 2 q16-18 C + + implementation

Source: Internet
Author: User

Hello everyone, I am mac Jiang, today and everyone to share the coursera-ntu-machine learning Cornerstone (Machines learning Foundations)-Job 2 q16-18 C + + implementation. Although there are many great gods in many blogs have given the implementation of Phython, but given the C + + implementation of the article is significantly less, here for everyone to provide a C + + implementation of the idea! Although my code can get the correct answer, but there may be some ideas or details are wrong, if you bo friends found, please timely message correction, thank you! Again, Bo owners to provide the implementation of the code is not to let you pass the test, but for students who have difficulties in learning to provide a solution, I hope that my article on your study has some help!

The source of this article: http://blog.csdn.net/a1015553840/article/details/51023193


1. Question 16th

(1) Test instructions: This problem is said to be "positive and negative rays", this classification method in the teacher's classroom has been analyzed, MH (N) =2n. We take a number of points in the interval [ -1,1] (17 is titled 20), these 20 points [ -1,1] are divided into 21 intervals, Theta can take any one of 21 intervals, and the value of S can be 1 or 1, a total of 21*2=42 combinations. Calculate h (x) for these 42 kinds of hyphothesis, calculate the difference between him and y, namely e_in, choose the minimum e_in of these 42 combinations, take this hyphothesis as the best theory, pass him e_out.

(2) Analysis: From the first problem we know that the calculation method after adding noise is this equation. For this problem we add 20% noise, so lambda = 0.8, we only ask Mu on it. The definition of MU is the difference between H (x) and F (x), which is the error rate. F (x) =s (x) =sign (x) is already given, H (x) =s*sign (X-theta) is also given in the title, so we need to discuss it according to S and Theta classifications

1.S = 1, theta > 0: Error rate is THETA/2

2.s=1,theta < 0; Error rate is |THETA|/2

3.s=-1,theta > 0: Error rate is (2-theta)/2

4.s=-1,theta <0: Error Rate is (2-| Theta |)/2

In summary, S=1 error rate is |theta|/2;s =-1, Error rate is (2-|theta|)/2

Use a formula to write: Mu = (s+1)/2 * (|THETA|/2)-(S-1)/2 * ((2-|theta|) /2)

Last e_out = mu * lambda + (1-LAMBDA) * (1-MU), lambda = 0.8,mu brought in to get answers

(3) Answer: 0.5+0.3*s* (|theta|-1)


2.17th, 18 questions

(1) Test instructions:

The 17th question means that in [ -1,1] take 20 points, separated into 21 intervals as the theta of the range of values, each classification has 42 hyphothesis, enumerate all the possible conditions to find the smallest hyphothesis e_in, record the smallest e_in

The 18th question means to calculate the e_out by the formula of the 16th question on the basis of the best hyphothesis of the 17 questions.

(2) Implementation code

#include <iostream> #include <stdlib.h> #include <vector> #include <algorithm> #include < math.h>using namespace std; #define DATASIZE 20//define the number of interior points for [ -1,1]//Training sample struct struct record{double x;int y;};/ /hyphothesis structure, S +1 or -1,theta 21 interval value in 20 points struct Hyphothesis{int s;double theta;};/ /sign function int sign (double x) {if (x <= 0) Return-1;else return 1;} Randomly generates an X of datasize points within [ -1,1] and calculates the corresponding yvoid Getranddata (vector<record> &trainingdata) {int i;for (i = 0; i < DataSize; i++) {Record temp;temp.x = 2.0 * rand ()/double (Rand_max)-1.0;temp.y = sign (temp.x); Trainingdata.push_back (temp);}} Add noise, which reverses the Y-value sign of the 20% point. Here 20% can be obtained by random method [0,1] number, if less than 0.2 think this point plus noise void getnoise (vector<record> &trainingdata) {int i;for (i = 0; i < DataSize; i++) {Double randnum = rand ()/double (rand_max); if (Randnum < 0.2) Trainingdata[i].y =-1 * TRAININGDATA[I].Y;}} Your own defined comparison method for Sortbool Mycompare (record &v1, record &v2) {return v1.x < v2.x;} The MAXSIZE = 20 samples are sorted by x, which calls the own sort function directly, and the thirdA parameter is your own definition of the comparison method (C + + does not know the record, do not know how to compare, we want to define mycompare tell him) void Sorttrainingdata (Vector<record> & Trainingdata) {sort (Trainingdata.begin (), Trainingdata.end (), mycompare);} The corresponding error rate for the given input collection and the specified hyphothesis calculation double calculateerror (vector<record> &trainingdata,hyphothesis &h) { int I;int error = 0;for (i = 0; i < datasize; i++) {int temp = H.S * SIGN (Trainingdata[i].x-h.theta); if (temp! = Trainin GDATA[I].Y) error++;} Return error/double (datasize);} Since s = 1 Or-1,theta has a value of 21, in total 42 hyphothesis, we calculated 42 of the smallest of the e_in, and recorded at this time the smallest error and corresponding hyphothesisdouble e_in (Vector<record  > &trainingdata,hyphothesis &besth) {hyphothesis temp;double min_errorrate = 1.0;int i;//s = 1 o'clock for (i = 0; i < datasize+1; i++) {Temp.s = 1;if (i = = 0) Temp.theta = trainingdata[0].x-1.0;//theta value, theta less than the minimum else if (i = = datasize) Temp.theta = Train ingdata[datasize-1].x + 1.0;//theta value between two points else Temp.theta = (trainingdata[i-1].x + trainingdata[i].x)/2.0;//theta value greater than Maximum double errorrate = Calculateerror (Trainingdata,temp);//If this hyphothesis error is smaller, replace if (Errorrate < min_errorrate) {besth = Temp;min_errorrate = Errorrate;}} s =-1 o'clock for (i = 0; i < datasize+1; i++) {Temp.s = -1;if (i = = 0) Temp.theta = Trainingdata[0].x-1.0;else if (i = = Datasiz E) Temp.theta = trainingdata[datasize-1].x + 1.0;else Temp.theta = (trainingdata[i-1].x + trainingdata[i].x)/2.0;doubl E errorrate = Calculateerror (trainingdata,temp); if (Errorrate < min_errorrate) {besth = Temp;min_errorrate = ErrorRate ;}} return min_errorrate;} Using the formula obtained in 16 to calculate the e_out, note that the floating-point number is calculated with fabs instead of absdouble e_out (hyphothesis &besth) {return 0.5 + 0.3 * DOUBLE (BESTH.S) * ( Double) (Fabs (Besth.theta)-1.0);} void Main () {int i;double totale_inrate = 0.0;double totale_outrate = 0.0;int seed[5000];//due to averaging 5,000 times, So there are 5,000 seeds for the random number int j;for (j = 0; J < X, J + +) {Seed[j] = rand ();//These 5,000 seeds are generated by a random number of}for (i = 0; i <; i + +) {s    Rand (Seed[i]);//each time a seed is taken, then the sequence of random numbers produced is different vector<record> trainingdata; Getranddata (trainingdata);//randomly generate training samples GetnoisE (Trainingdata);//Add Noise sorttrainingdata (trainingdata);//Sample sort Hyphothesis Besth = {0,0}; Double min_errorrate = e_in (trainingdata,besth);//Calculate the optimal hyphothesis and record the minimum error rate cout<< "mininum e_in:" <<min_ Errorrate<<endl;totale_inrate + = min_errorrate;totale_outrate + e_out (besth);cout<< "E_out:" <<E_ Out (BESTH) <<endl;//use the best hyphothesis to get e_out}cout<< "average e_in:" <<totalE_inRate/5000< <endl;//average e_incout<< "e_out:" <<totale_outrate/5000<<endl;//average e_out}


The source of this article: http://blog.csdn.net/a1015553840/article/details/51023193





Robot Learning Cornerstone Machine Learn Cornerstone (machines learining foundations) Job 2 q16-18 C + + implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.