⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winsvmdlg.cpp

📁 实现SVM的分类可以选择SVM类型
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	double total_error = 0;
	double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
	double *target = Malloc(double,prob.l);

	svm_cross_validation(&prob,&param,nr_fold,target);
	if(param.svm_type == EPSILON_SVR ||
	   param.svm_type == NU_SVR)
	{
		for(i=0;i<prob.l;i++)
		{
			double y = prob.y[i];
			double v = target[i];
			total_error += (v-y)*(v-y);
			sumv += v;
			sumy += y;
			sumvv += v*v;
			sumyy += y*y;
			sumvy += v*y;
		}
		//printf("Cross Validation Mean squared error = %g\n",total_error/prob.l);
		//printf("Cross Validation Squared correlation coefficient = %g\n",
		//	((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
		//	((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
		//	);

		CString str;
		str.Format("交叉验证均方误差值是 %g\n",total_error/prob.l);
		info+=str;//MessageBox(str);
		str.Format("交叉验证均值与平方相关系数是 %g\n",
			((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
			((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
			);
		info+=str;//MessageBox(str);
	}
	else
	{
		for(i=0;i<prob.l;i++)
			if(target[i] == prob.y[i])
				++total_correct;
		CString str;
		str.Format("交叉验证正确率 = %g%%\n",100.0*total_correct/prob.l);
		info+=str;//MessageBox(str);
	}
	free(target);

}

void CwinsvmDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(IDOK == AfxMessageBox(_T("要退出程序吗?"),MB_OKCANCEL))
	{
	  /*  svm_destroy_param(&param);
	    free(prob.y);
	    free(prob.x);
	    free(x_space);
		*/
		CDHtmlDialog::OnClose();
	}
}


void CwinsvmDlg::OnBnClickedButton4()//暂停
{
	if(m_mythread)
		SuspendThread(m_mythread->m_hThread);
	// TODO: 在此添加控件通知处理程序代码
    GetDlgItem(IDC_BUTTON4)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON5)->EnableWindow();
}

void CwinsvmDlg::OnBnClickedButton5()//继续
{
	if(m_mythread)
		ResumeThread(m_mythread->m_hThread);
	// TODO: 在此添加控件通知处理程序代码
    GetDlgItem(IDC_BUTTON4)->EnableWindow();
	GetDlgItem(IDC_BUTTON5)->EnableWindow(false);
}

void CwinsvmDlg::OnBnClickedButton6()//停止
{
	if( m_mythread && GetExitCodeThread( m_mythread->m_hThread, &dwCode ) )
	{
		if( dwCode == STILL_ACTIVE )
		{
			TerminateThread( m_mythread->m_hThread, 0 );
			CloseHandle( m_mythread->m_hThread );
			m_mythread = NULL;
		}
	}	
	AfxMessageBox( "怎么这么不耐烦!" );/*AfxThrowUserException( );*/
    GetDlgItem(IDC_BUTTON4)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON5)->EnableWindow(false);
    GetDlgItem(IDC_BUTTON6)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON1)->EnableWindow();
}

void CwinsvmDlg::OnSize(UINT nType, int cx, int cy)
{
	CDHtmlDialog::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	if(nType == SIZE_MINIMIZED)
	{
	   sprintf(m_NotifyIconData.szTip,"winsvm2007");
	   m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
	   m_NotifyIconData.hIcon = m_hIcon;
	   m_NotifyIconData.hWnd =  GetSafeHwnd();
	   m_NotifyIconData.uCallbackMessage = UM_NOTIFY;
	   m_NotifyIconData.uFlags = NIF_MESSAGE|NIF_TIP|NIF_ICON;
	   m_NotifyIconData.uID = IDIC_NOTIFY;
	   Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);//在状态栏加个图标,接受鼠标消息
	   ShowWindow(SW_HIDE); // 隐藏窗口 
 	}
	else
	{
	//	Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
	}

}

LRESULT CwinsvmDlg::OnMyNotify(WPARAM wParam, LPARAM lParam)//改过
{
	WINDOWPLACEMENT wndPlace;
	GetWindowPlacement(&wndPlace);
	if(lParam == WM_LBUTTONUP)
	{
		WINDOWPLACEMENT wndPlace;
		GetWindowPlacement(&wndPlace);
		if(wndPlace.showCmd == SW_SHOWMINIMIZED)
		{
	      ShowWindow(SW_SHOW);//SW_RESTORE);
	      ShowWindow(SW_RESTORE);
	      SetActiveWindow(); 
		}
		else
		{
			ShowWindow(SW_SHOWMINIMIZED);
		}
	}
	else if(lParam == WM_RBUTTONUP && wndPlace.showCmd == SW_SHOWMINIMIZED)
	{

		CMenu PopMenu;
		PopMenu.CreatePopupMenu();
	    PopMenu.AppendMenu(MF_STRING, IDM_RESTORE,  _T("恢复(&R)"));
	    PopMenu.AppendMenu(MF_STRING, IDOK,      _T("关闭(&C)")); 
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			PopMenu.AppendMenu(MF_SEPARATOR);
			PopMenu.AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
		POINT pt; 
        GetCursorPos(&pt);   
		PopMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, this); 
		//TRACE("%d,%d, %d\n",lParam, HIWORD(wParam), LOWORD(wParam));
	}
	return LRESULT();
}

void CwinsvmDlg::OnMyRestore()
{
	ShowWindow(SW_SHOW); 
	ShowWindow(SW_RESTORE);
	SetActiveWindow();
}

void CwinsvmDlg::OnAbout()
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CwinsvmDlg::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类
	if(IDOK == AfxMessageBox(_T("要退出程序吗?"),MB_OKCANCEL))
	{
		CDHtmlDialog::OnOK();
	}
}

//预测
void CwinsvmDlg::OnBnClickedButton2()
{
		//线程运行
	if( m_mythread && GetExitCodeThread( m_mythread->m_hThread, &dwCode ) )
	{
		if( dwCode == STILL_ACTIVE )
		{
			MessageBox("嘻嘻,训练还没结束呢!");
			return;
		}
	}
	m_yuce=new Cyuce;
	if(m_yuce->DoModal()==IDOK)
	{
        char* line;
        int max_line_len = 1024;
        
        max_nr_attr = 64;
        char input0[1024],output0[1024],model0[1024];


	    FILE *input, *output;
	    strcpy(input0, m_yuce->m_input);
	    strcpy(model0, m_yuce->m_model);
		strcpy(output0, m_yuce->m_output);
        predict_probability = m_yuce->predict_probability;

	    input = fopen(input0,"r");//预测数据文件
	    if(input == NULL)
	    {
		    //fprintf(stderr,"can't open input file %s\n",argv[i]);
		    //exit(1);
		    CString strs;
		    strs.Format("不能打开文件 %s\n",input0);
		    MessageBox(strs);
		    return;
	    }

	    output = fopen(output0,"w");//预测结果文件保存地址
	    if(output == NULL)
	    {
		    //fprintf(stderr,"can't open output file %s\n",argv[i+2]);
		    //exit(1);
		    CString strs;
		    strs.Format("不能保存文件 %s\n",output0);
		    MessageBox(strs);
		    return;
	    }

	    if((modely=svm_load_model(model0))==0)//模型文件地址选择
	    {
		    //fprintf(stderr,"can't open model file %s\n",argv[i+1]);
		    //exit(1);
		    CString strs;
		    strs.Format("不能打开文件 %s\n",model0);
		    MessageBox(strs);
	    }
	    line = (char *) malloc(max_line_len*sizeof(char));
	    x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node));
	    if(predict_probability)
		    if(svm_check_probability_model(modely)==0)
		    {
			    //fprintf(stderr,"Model does not support probabiliy estimates\n");
			    //exit(1);
                MessageBox("训练好的model文件不支持概率预报");
				return;
		    }
	    predict(input,output);
	    svm_destroy_model(modely);
	    free(line);
	    free(x);
	    fclose(input);
	    fclose(output);

		delete m_yuce;
		m_yuce=NULL;
		return;
	};
	delete m_yuce;
	m_yuce=NULL;
}

void CwinsvmDlg::predict(FILE *input, FILE *output)
{
	int correct = 0;
	int total = 0;
	double error = 0;
	double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;

	int svm_type=svm_get_svm_type(modely);
	int nr_class=svm_get_nr_class(modely);
	int *labels=(int *) malloc(nr_class*sizeof(int));
	double *prob_estimates=NULL;
	int j;
    CString str;//添加
	if(predict_probability)
	{
		if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
		{
			//printf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model));
		    str.Format("测试数据的样本: 目标值 = 预测值 + z,\nz: 拉普拉斯分布 e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(modely));
		    info+=str;
		}
		else
		{
			svm_get_labels(modely,labels);
			prob_estimates = (double *) malloc(nr_class*sizeof(double));
			fprintf(output,"labels");		
			for(j=0;j<nr_class;j++)
				fprintf(output," %d",labels[j]);
			fprintf(output,"\n");
		}
	}
	while(1)
	{
		int i = 0;
		int c;
		double target,v;

		if (fscanf(input,"%lf",&target)==EOF)
			break;

		while(1)
		{
			if(i>=max_nr_attr-1)	// need one more for index = -1
			{
				max_nr_attr *= 2;
				x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
			}

			do {
				c = getc(input);
				if(c=='\n' || c==EOF) goto out2;
			} while(isspace(c));
			ungetc(c,input);
			fscanf(input,"%d:%lf",&x[i].index,&x[i].value);
			++i;
		}	

out2:
		x[i++].index = -1;

		if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
		{
			v = svm_predict_probability(modely,x,prob_estimates);
			fprintf(output,"%g ",v);
			for(j=0;j<nr_class;j++)
				fprintf(output,"%g ",prob_estimates[j]);
			fprintf(output,"\n");
		}
		else
		{
			v = svm_predict(modely,x);
			fprintf(output,"%g\n",v);
		}

		if(v == target)
			++correct;
		error += (v-target)*(v-target);
		sumv += v;
		sumy += target;
		sumvv += v*v;
		sumyy += target*target;
		sumvy += v*target;
		++total;
	}
	//printf("Accuracy = %g%% (%d/%d) (classification)\n",
	//	(double)correct/total*100,correct,total);
	//printf("Mean squared error = %g (regression)\n",error/total);
	//printf("Squared correlation coefficient = %g (regression)\n",
	//	((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
	//	((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))
	//	);
	str.Format("精确度 = %g%% (%d/%d) (分类)\n",
		(double)correct/total*100,correct,total);
	info+=str;
	str.Format("均方误差 = %g (回归)\n",error/total);
	info+=str;
	str.Format("平方相关系数 = %g (回归)\n",
		((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
		((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))
		);
	info+=str;

	if(predict_probability)
	{
		free(prob_estimates);
		free(labels);
	}
    MessageBox(info);
	info.Empty();

}

void CwinsvmDlg::OnBnClickedButton3()
{
	if (m_guyihua==NULL)
	{
	    m_guyihua=new Cguyihua;
		m_guyihua->Create(IDD_DIALOG3,this);
		m_guyihua->ShowWindow(1);
	}
	else
	{
	    delete m_guyihua;
	    m_guyihua=NULL;
	}

	//if(m_guyihua->DoModal()==IDOK)
	//{	   
	//	//MessageBox(info);
	//	delete m_guyihua;
	//	m_guyihua=NULL;
	//}
	//delete m_guyihua;
	//m_guyihua=NULL;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -