test_chol

// 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <mat.h>  
#include <iostream>
#include <iomanip>
#include <Eigen/Dense>
#include <Eigen/LU>
#include <Eigen/Cholesky>
using Eigen::MatrixXd;
using Eigen::VectorXd;
using namespace std;
using namespace Eigen;
void chol(MatrixXd P,double c,MatrixXd &A)
{

LLT<MatrixXd> lltOfP(P);
MatrixXd L=lltOfP.matrixL();        //产生下三角矩阵
A<<c*L;


}
void mat_read(MatrixXd &P,MatrixXd &c,MatrixXd &A)
{


MATFile *pmatFileP = NULL,*pmatFilec = NULL,*pmatFileA = NULL;     
mxArray *pMxArrayP = NULL,*pMxArrayc = NULL,*pMxArrayA = NULL;    
double *inputP,*inputc,*outputA; 




pmatFileP = matOpen("inputP.mat","r");    
pMxArrayP = matGetVariable(pmatFileP,"inputP");    
inputP = (double*) mxGetData(pMxArrayP);  
mwSize M_P = mxGetM(pMxArrayP);  
mwSize N_P = mxGetN(pMxArrayP);    
MatrixXd tempP(M_P,N_P);    
for (int i=0; i<M_P; i++)    
for (int j=0; j<N_P; j++)    
tempP(i,j) = inputP[M_P*j+i];   
P=tempP;
matClose(pmatFileP);    
mxFree(inputP);  
//cout<<"tempP"<<endl<<fixed<<setprecision(5)<<tempP<<endl;


pmatFilec = matOpen("inputc.mat","r");    
pMxArrayc = matGetVariable(pmatFilec,"inputc");    
inputc = (double*) mxGetData(pMxArrayc);    
mwSize M_c = mxGetM(pMxArrayc);  
mwSize N_c = mxGetN(pMxArrayc);    
MatrixXd tempc(M_c,N_c);    
for (int i=0; i<M_c; i++)    
for (int j=0; j<N_c; j++)    
tempc(i,j) = inputc[M_c*j+i];  
c=tempc;
matClose(pmatFilec);    
mxFree(inputc);  
//cout<<"tempc"<<endl<<fixed<<setprecision(5)<<tempc<<endl;


pmatFileA = matOpen("outputA.mat","r");    
pMxArrayA = matGetVariable(pmatFileA,"outputA");    
outputA = (double*) mxGetData(pMxArrayA);  
mwSize M_X = mxGetM(pMxArrayA);  
mwSize N_X = mxGetN(pMxArrayA);    
MatrixXd tempX(M_X,N_X);    
for (int i=0; i<M_X; i++)    
for (int j=0; j<N_X; j++)    
tempX(i,j) = outputA[M_X*j+i];  
A=tempX;
matClose(pmatFileA);    
mxFree(outputA);  
//cout<<"tempX"<<endl<<fixed<<setprecision(5)<<tempX<<endl;




}
void test_chol()
{
MatrixXd P,c,A;
mat_read(P,c,A);
int length=400;
int wideth=60;

MatrixXd inputP(3,3),outputA(3,3);
double inputc;
for(int ut=0;ut<length;++ut)
for(int uk=0;uk<wideth;++uk)
{
int index=ut*wideth+uk;
for(int i=0;i<3;++i)   /从P中提取输入数据
for(int j=0;j<3;++j)
{
inputP(i,j)=P(index*3+i,j);
}
inputc=c(index,0);      //从c中提取输入数据

chol(inputP,inputc,outputA);


/检验outputX与matlab中X的输出的差异,如果大于1e-5,报错
double varience;
double permissibleError=1e-18;
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
{


varience=outputA(i,j)-A(index*3+i,j);
if(varience>=permissibleError)
{
printf("sigmas call error!\n");
printf("(%d,%d)th call error\n",ut,uk);
printf("outputA(%d,%d) error\n",i,j);
printf("outputA=%.6f,X=%.6f,varience=%.6f\n",outputA(i,j),A(index*3+i,j),varience);
return ;
}
}
}
printf("sigmas call success!\n");
}


int _tmain(int argc, _TCHAR* argv[])
{
test_chol();
return 0;
}
以下是将 Logistic Regression 模型的评估指标改为 ROC 曲线的代码: ```python import seaborn as sns import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import roc_curve, auc df = pd.read_csv('heart.csv') # 查看特征相关性 corrmat = df.corr() top_corr_features = corrmat.index plt.figure(figsize=(16,16)) sns.heatmap(df[top_corr_features].corr(),annot=True,cmap="RdYlGn") plt.show() # 查看数据分布 sns.set_style('whitegrid') sns.countplot(x='target',data=df,palette='RdBu_r') plt.show() # 对数据进行 One-hot 编码和标准化 dataset = pd.get_dummies(df, columns=['sex', 'cp', 'fbs', 'restecg', 'exang', 'slope', 'ca', 'thal']) columns_to_scale = ['age', 'trestbps', 'chol', 'thalach', 'oldpeak'] scaler = StandardScaler() dataset[columns_to_scale] = scaler.fit_transform(dataset[columns_to_scale]) dataset.head() # 划分数据集 y = dataset['target'] X = dataset.drop(['target'], axis=1) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) # 训练模型 logreg = LogisticRegression() logreg.fit(X_train, y_train) # 评估模型 y_train_pred = logreg.predict_proba(X_train)[:, 1] y_test_pred = logreg.predict_proba(X_test)[:, 1] fpr_train, tpr_train, thresholds_train = roc_curve(y_train, y_train_pred) fpr_test, tpr_test, thresholds_test = roc_curve(y_test, y_test_pred) roc_auc_train = auc(fpr_train, tpr_train) roc_auc_test = auc(fpr_test, tpr_test) # 绘制 ROC 曲线 plt.figure() plt.plot(fpr_train, tpr_train, color='darkorange', lw=2, label='Train ROC curve (area = %0.2f)' % roc_auc_train) plt.plot(fpr_test, tpr_test, color='navy', lw=2, label='Test ROC curve (area = %0.2f)' % roc_auc_test) plt.plot([0, 1], [0, 1], color='black', lw=2, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristic') plt.legend(loc="lower right") plt.show() ``` 在这个代码中,我们使用 `roc_curve` 函数计算训练集和测试集的 FPR 和 TPR,然后使用 `auc` 函数计算 ROC 曲线下的面积。最后,我们使用 `matplotlib` 库绘制 ROC 曲线。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值