Travel Tips
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
上市公司财务预警(神经网络)
#设置工作空间 getwd() setwd("/home/zty/Documents/R/上市公司财务预警") library(nnet) #导入数据集 data_bp <- read.csv("shuru.csv", fileEncoding = 'GBK') head(data_bp) #提取数据子集 data_bp_sub <- data_bp[,c("Datatype","ST","AvgROE","ROAEBIT","Opeprfrt","Currt","Qckrt","Opeincmgrrt", "Netprfgrrt","Netassgrrt","Invtrtrrat","ARTrat","Currat", "Totassrat","Casrtsale","Dbastrt","Ebitada")] #查看数据 head(data_bp_sub) table(data_bp_sub$ST) #数据变换 data_bp_sub <- within (data_bp_sub,{ ST_1 <- NA ST_1[ST == "是"] <- 1 ST_1[ST == "否"] <- 0}) #查看数据 table(data_bp_sub$ST,data_bp_sub$ST_1) #构建训练集与测试集 data_bp_sub_train <- data_bp_sub[which(data_bp_sub$Datatype == "训练集"),] data_bp_sub_test <- data_bp_sub[which(data_bp_sub$Datatype == "测试集"),] #查看数据类型 str(data_bp_sub_train) str(data_bp_sub_test) #改变数据类型 data_bp_sub_train$AvgROE <- as.numeric(data_bp_sub_train$AvgROE) data_bp_sub_train$Netprfgrrt <- as.numeric(data_bp_sub_train$Netprfgrrt) data_bp_sub_train$Netassgrrt <- as.numeric(data_bp_sub_train$Netassgrrt) data_bp_sub_train$Ebitada <- as.numeric(data_bp_sub_train$Ebitada) data_bp_sub_train$ST_1 <- as.factor(data_bp_sub_train$ST_1) data_bp_sub_test$AvgROE <- as.numeric(data_bp_sub_test$AvgROE) data_bp_sub_test$Netprfgrrt <- as.numeric(data_bp_sub_test$Netprfgrrt) data_bp_sub_test$Netassgrrt <- as.numeric(data_bp_sub_test$Netassgrrt) data_bp_sub_test$Ebitada <- as.numeric(data_bp_sub_test$Ebitada) data_bp_sub_test$ST_1 <- as.factor(data_bp_sub_test$ST_1) # 构建nnet神经网络 model1=nnet(ST_1~AvgROE+ROAEBIT+Opeprfrt+Currt+Qckrt+Opeincmgrrt+Netprfgrrt+Netassgrrt +Invtrtrrat+ARTrat+Currat+Totassrat+Casrtsale+Dbastrt+Ebitada, data=data_bp_sub_train,size=6,decay=5e-4,rang = 0.1,maxit=1000) model1 #构建混淆矩阵 confusion=table(data_bp_sub_test$ST_1,predict(model1,data_bp_sub_test[,3:17],type ="class")) confusion
#导入pandas库 import pandas as pd #导入数据集 net = pd.read_csv ( '/media/zty/aae84acb-4d27-47dc-b5a0-93a21d9f7cd9/zty/数据分析文件/262-结实-左手R右手Python数据挖掘机器学习/第五章/神经网络--python实现/shuru.csv',encoding='utf-8') net.head()
net_sub = net.loc[:, ['ROAEBIT', 'Dbastrt','Currt','Qckrt','Invtrtrrat','ARTrat','Currat', 'Totassrat','Casrtsale','Opeprfrt','Ebitada','AvgROE','Opeincmgrrt', 'Netprfgrrt','Netassgrrt','ST']] #条件赋值 #net_sub['ST_1'] = net_sub.ST.apply(lambda x: 1 if '是' in x else 0) #类型查看 net_sub.dtypes
net_sub_s = net_sub.drop('ST',axis = 1) #再查看数据集 net_sub_s.dtypes #net_sub_s.head()
from sklearn.model_selection import train_test_split array = net_sub_s.values array = net_sub_s.values nb_classes = 6 X =array[:,0:15] Y = array[:,14] test_size = 0.30 seed = 4 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_size, random_state=seed) # 一般形式:X_train,X_test, y_train, y_test =cross_validation.train_test_split(train_data,train_target,test_size= , random_state= ) #X_train:所要划分的样本特征集 #train_target:所要划分的样本结果 #test_size:样本占比,如果是整数的话就是样本的数量 #random_state:是随机数的种子。在需要重复试验的时候,保证得到一组一样的随机数。
#导入人工神经网络库 from sklearn.neural_network import MLPClassifier mlp = MLPClassifier(hidden_layer_sizes= ( 13,13,13 ) ,max_iter=500) mlp
mlp.fit(X_train.astype(int),Y_train.astype(int))
predictions = mlp.predict ( X_test ) from sklearn.metrics import classification_report,confusion_matrix import numpy as np print ( confusion_matrix ( np.rint(Y_test),predictions ) )
from sklearn import metrics print("tes_score:", mlp.score(np.rint(X_test), np.rint(Y_test))) y_pred = mlp.predict(X_test) print("查准率:",metrics.precision_score(np.rint(Y_test), np.rint(y_pred),average='weighted')) print("召回率:",metrics.recall_score(np.rint(Y_test), np.rint(y_pred),average='weighted')) print("F1_score:",metrics.f1_score(np.rint(Y_test), np.rint(y_pred),average='weighted'))
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Donec in ullamcorper quam. Aenean vel nibh eu magna gravida fermentum. Praesent eget nisi pulvinar, sollicitudin eros vitae, tristique odio.
December 4, 2020 at 3:12 pm
我是 s enim interduante quis metus. Duis porta ornare nulla ut bibendum
Rosie
6 minutes ago