Test questions:
Code Description:
1. In main I used an input file to represent the input, which should be removed when testing
2. The following functions are the calculation of the predicted values, the calculation of costfunction, the implementation of the Luo series regression
3. Specifically similar to linear regression, can refer to the gradient descent of linear regression
The code is as follows:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include < Vector> #include <iostream>using namespace std;double hypothesis (vector<double> &feature,vector <DOUBLE>&W) {double sum=0.0; for (int i=0;i<feature.size (); i++) {sum+=w[i]*feature[i]; } return 1/(1+exp (-sum));} Double cost_fuction (vector<vector<double> > &feature_sample,vector<double> &w,vector< double>&lable) {double sum=0.0; for (int i=0;i<lable.size (); i++) {Sum+=-lable[i]*log (hypothesis (FEATURE_SAMPLE[I],W))-(1-lable[i]) *log (1- Hypothesis (feature_sample[i],w)); } return Sum/lable.size ();} void Logic_regression (vector<vector<double> >&feature_sample,vector<double> &lable, Vector<double> &w,double a) {vector<double> delta_w; for (int j=0;j<feature_sample[0].size (); j + +) {double sum=0.0; for (int i=0;i<lable.size (); i++) { sum+= (hypothesis (FEATURE_SAMPLE[I],W)-lable[i]) *feature_sample[i][j]; } delta_w.push_back (Sum/lable.size () *a); } for (int i=0;i<w.size (); i++) {w[i]-=delta_w[i]; } cout<<cost_fuction (Feature_sample,w,lable) <<endl;} int main () {freopen ("In.txt", "R", stdin); int feature_num,training_num,t; Double A; cin>>feature_num>>training_num>>a>>t; Vector<vector<double> >feature_sample; vector<double> tem; Vector<double> lable; Vector<double> W; Double m; for (int i=0;i<training_num;i++) {tem.clear (); Tem.push_back (1); for (int j=0;j<feature_num;j++) {cin>>m; Tem.push_back (m); } cin>>m; Lable.push_back (m); Feature_sample.push_back (TEM); } for (int i=0;i<=feature_num;i++) w.push_back (0); while (t--) logic_regression (feature_sample,lable,w,a); for (int i=0;i<=feature_num;i++) cout<<w[i]<< ""; return 0;}
Operation Result solution diagram
C + + implements logistic regression code