阅读 8

非线性分类模型,stata亚组分析森林图

LDF

QDF

python的实现

输入numpy as NP # importmatplotlib.pyplotaspltimportmathimporttensorflowastffromsklearn.preprocessingimportstandardscalerfromsklearn.de comcomsklearn nimporttrain_test_split,cross_val_score,GridSearchCV #分区数据交叉检查网搜索class LDFandQDF(x_train,y_train classB ) :x是训练数据集,其中labelx1=NP.array ([ x_train [ I ] foriinrange (len ) x _ train ) x )用于y训练ify_train (class a ) )标签(forIinrange(len(x_train ) (ify _ train ) I ) classB ) )找到classB的数据)预先确定求平均值的mju1=NP.mean ) x1,axis=0) # mju为ndarray型mju2=NP.mean ) x2, axis=0) #协方差covx1=NP.cov(x1.t ) covx2=NP.cov ) x2.t ) #协方差矩阵公式cov1_det=NP.LinaLG.det ) covx1) co v2 _ co 伪逆invx1=NP.LinaLG.pinv(covx1) else: # )协方差矩阵可逆invx1=NP.LinaLG.inv ) covx1) if cov2_det==0: #第二类# 使用协方差矩阵伪逆invx2=NP.LinaLG.pinv(covx2 ) else: # )协方差矩阵可逆invx2=NP.LinaLG.inv ) covx1) #记录类型self.class a=class 先验概率self.Pw2=Pw2 self.mju1=mju1 #第一类classA平均值# self.covX1=covX1 #协方差self.inv_covX1=invX1 #协方差的逆self.co v1 \ 预测协方差矩阵方程self.mju2=mju2 #第二类classb # self.cov x2=cov x2 self.inv _ cov x2=inv x2 self.def predict (self ),method ) W1=-(1/2) self.inv_covx1W2=-(1/2) self.inv self.mju1) W2=NP.dot(self.inv_covX2, self.mju2) if method==' qdf ' : if self.co v1 _ det==03360 #将对数项设置为w10=-(1/2) *NP.dot(NP.dot ) self.self

w10 = -(1 / 2) * np.dot(np.dot(self.mju1.T, self.inv_covX1), self.mju1) - (1 / 2) * math.log( self.cov1_det) + math.log(self.Pw1) if self.cov2_det == 0: w20 = -(1 / 2) * np.dot(np.dot(self.mju2.T, self.inv_covX2), self.mju2) + math.log(self.Pw2) else: w20 = -(1 / 2) * np.dot(np.dot(self.mju2.T, self.inv_covX2), self.mju2) - (1 / 2) * math.log( self.cov2_det) + math.log(self.Pw2) for i in range(len(self.x_test_)): g1x = np.dot(np.dot(self.x_test_[i].T, W1), self.x_test_[i]) + np.dot(w1.T, self.x_test_[i]) + w10 g2x = np.dot(np.dot(self.x_test_[i].T, W2), self.x_test_[i]) + np.dot(w2.T, self.x_test_[i]) + w20 if (g1x - g2x) > 0: predict_class.append(self.classa) else: predict_class.append(self.classb) if method == 'LDF': w10 = -(1 / 2) * np.dot(np.dot(self.mju1.T, self.inv_covX1), self.mju1) + math.log(self.Pw1) w20 = -(1 / 2) * np.dot(np.dot(self.mju2.T, self.inv_covX2), self.mju2) + math.log(self.Pw2) for i in range(len(self.x_test_)): g1x = np.dot(w1.T, self.x_test_[i]) + w10 g2x = np.dot(w2.T, self.x_test_[i]) + w20 if (g1x - g2x) > 0: predict_class.append(self.classa) else: predict_class.append(self.classb) return predict_class def split_test_data(self, xtest, ytest): x_test_ = np.array( [xtest[i] for i in range(len(xtest)) if (ytest[i] == self.classa or ytest[i] == self.classb)]) y_test_ = np.array( [ytest[i] for i in range(len(ytest)) if (ytest[i] == self.classa or ytest[i] == self.classb)]) self.x_test_ = x_test_ self.y_test_ = y_test_ # 计算判别函数分类精度 def analysis_accuracy(self, y_predict): count = 0 nums = len(self.y_test_) for i in range(nums): if y_predict[i] == self.y_test_[i]: count += 1 precise = count / nums # 显示信息 print("Function type:", self.method) print("classes:", self.classa, self.classb) print("Numbers of test samples:", nums) print("Numbers of predict correct samples:", count) print("Test precise:", precise) return precise # 挑出对应类别的测试集if '__main__' == __name__: # 产生分类数据 mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() # 读取mnist数据集,已经划分好了训练集和测试集 y_train = y_train.astype(np.uint8) # 将标签转换成整数 x_train_new = x_train.reshape(len(x_train), -1) # 将数据集和测试集的图像像素展开,变成784维特征 x_test_new = x_test.reshape(len(x_test), -1) # #PCA降维,但是结果不是很理想,本以为会大幅改善LDF的分类性能,但是没啥用 # # features = np.append(x_train_new, x_test_new, axis=0) # labels = np.append(y_train, y_test, axis=0) # # features_ = StandardScaler().fit_transform(features) # # pca = PCA(n_components=0.95, whiten=True) # features_pca = pca.fit_transform(features_) # # x_train_pca, x_test_pca, y_train_pca, y_test_pca = train_test_split(features_pca, labels, test_size=0.2, ) # # print(features_pca.shape) # print(x_train_pca.shape, y_train_pca.shape) # ldfqdf = LDFandQDF() # 调用上面写的类 # ldfqdf.train(x_train_pca, y_train_pca, 0, 5) # ldfqdf.split_test_data(x_test_pca, y_test_pca) # # predic_result1 = ldfqdf.predict('LDF') # ldfqdf.analysis_accuracy(predic_result1) # # predic_result2 = ldfqdf.predict('QDF') # ldfqdf.analysis_accuracy(predic_result2) # 不降维,用原数据直接算 ldfqdf = LDFandQDF() # 调用上面写的类 ldfqdf.train(x_train_new, y_train, 1, 9) ldfqdf.split_test_data(x_test_new, y_test) predic_result1 = ldfqdf.predict('LDF') ldfqdf.analysis_accuracy(predic_result1) predic_result2 = ldfqdf.predict('QDF') ldfqdf.analysis_accuracy(predic_result2)


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐