【Machine Learning实验2】 Logistic Regression求解classification问题
生活随笔
收集整理的這篇文章主要介紹了
【Machine Learning实验2】 Logistic Regression求解classification问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??classification問題和regression問題類似,區別在于y值是一個離散值,例如binary classification,y值只取0或1。
?? ? ? ?方法來自Andrew Ng的Machine Learning課件的note1的PartII,Classification and logsitic regression.
?? ? ? ?實驗表明,通過多次迭代,能夠最大化Likehood,使得分類有效,實驗數據為人工構建,沒有實際物理意義,matrix的第一列為x0,取常數1,第二列為區分列,第三列,第四列為非區分列,最后對預測起到主導地位的參數是theta[0]和theta[1]。
#include "stdio.h" #include "math.h"double matrix[6][4]={{1,47,76,24}, //include x0=1{1,46,77,23},{1,48,74,22},{1,34,76,21},{1,35,75,24},{1,34,77,25},};double result[]={1,1,1,0,0,0}; double theta[]={1,1,1,1}; // include theta0double function_g(double x) {double ex = pow(2.718281828,x);return ex/(1+ex); } int main(void) {double likelyhood = 0.0;float sum=0.0;for(int j = 0;j<6;++j){double xi = 0.0;for(int k=0;k<4;++k){xi += matrix[j][k]*theta[k];}printf("sample %d,%f\n",j,function_g(xi));sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;}printf("%f\n",sum);for(int i =0 ;i<1000;++i){double error_sum=0.0;int j=i%6;{double h = 0.0;for(int k=0;k<4;++k){h += matrix[j][k]*theta[k];}error_sum = result[j]-function_g(h);for(int k=0;k<4;++k){theta[k] = theta[k]+0.001*(error_sum)*matrix[j][k];}}printf("theta now:%f,%f,%f,%f\n",theta[0],theta[1],theta[2],theta[3]);float sum=0.0;for(int j = 0;j<6;++j){double xi = 0.0;for(int k=0;k<4;++k){xi += matrix[j][k]*theta[k];}printf("sample output now: %d,%f\n",j,function_g(xi));sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;}printf("maximize the log likelihood now:%f\n",sum);printf("************************************\n");}return 0; }
from: http://blog.csdn.net/pennyliang/article/details/7045372
總結
以上是生活随笔為你收集整理的【Machine Learning实验2】 Logistic Regression求解classification问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Machine Learning实验1
- 下一篇: Machine Learning实验3】