# -*- coding: utf-8 -*- import os from keras.optimizers import Adam from sklearn import preprocessing import scipy.io as scio from keras.utils.np_utils import to_categorical from keras.models import Model from keras.layers import Dense, Input from scipy.io import loadmat from keras.models import Sequential from keras.regularizers import l2 import pandas as pd import matplotlib.pyplot as plt from keras.models import Model from keras.layers import Dense, Input,Lambda,Dropout from keras.layers.normalization import BatchNormalization from scipy.io import loadmat from keras import backend as K import numpy as np from keras.losses import mean_squared_error,categorical_crossentropy from keras.callbacks import ModelCheckpoint from keras.models import load_model if __name__ == '__main__': myclasslabel= ['00','04','05'] classes=len(myclasslabel) #150 120 110 50 90 70 50 30 reg_par = 1e-8 train_num_pre=450 test_num_pre=750 save_dir = os.path.join(os.getcwd()) #model_name = 'te_trained_model.h5' #导入TE数据 for index in range(len(myclasslabel)): path0=r'D:\sxt\GraduatePrograms\Tieling\lu\TE_process\TE_process\d' path1=myclasslabel[index] path2='.dat' path3='_te.dat' path=path0+path1+path2 test_path=path0+path1+path3 D00 = pd.read_csv(path,header=None,sep='\s+') test_D00 = pd.read_csv(test_path,header=None,sep='\s+') D00=np.matrix(D00) D00=D00[29:29+train_num_pre,:] test_D00=np.matrix(test_D00) test_D00=test_D00[190:190+test_num_pre,:] if index==0: x_train_pre=D00 x_test_pre=test_D00 else: x_train_pre=np.vstack((x_train_pre,D00)) x_test_pre=np.vstack((x_test_pre,test_D00)) x_train=x_train_pre x_test=x_test_pre train_num=train_num_pre test_num=test_num_pre #以下产生one-hot 编码 c=np.matrix(np.eye(classes)[0]) train_y=np.repeat(c,train_num,axis=0) test_y=np.repeat(c,test_num,axis=0) for i in range(1,classes): c1=np.matrix(np.eye(classes)[i]) y1=np.repeat(c1,train_num,axis=0) y2=np.repeat(c1,test_num,axis=0) train_y=np.vstack((train_y,y1)) test_y=np.vstack((test_y,y2)) onecolumn_train_y=np.argmax(train_y,axis=1) onecolumn_test_y=np.argmax(test_y,axis=1) scaler_train = preprocessing.StandardScaler().fit(x_train) #数据标准化 x_train=scaler_train.transform(x_train) x_test=scaler_train.transform(x_test) #以上是数据处理 # # 第一个自编码器预训练 oridim=x_train.shape[1] # this is our input placeholder input_x = Input(shape=(oridim,),name='main_input') # encoder layers x1 = Dense(200, activation='relu', name='layer_x1',kernel_regularizer=l2(reg_par))(input_x) #200 x1 =BatchNormalization()(x1) x2 = Dense(150, activation='relu', name='layer_x2', kernel_regularizer=l2(reg_par))(x1) #150 x2 =BatchNormalization()(x2) x3 = Dense(50, activation='relu', name='layer_x3', kernel_regularizer=l2(reg_par))(x2) #100 x3 =BatchNormalization()(x3) encoded_output = Dense(classes, activation='softmax',name='layer_encoded')(x3) #这可以直接输出每个类别的概率,可以将encoded_output 或者 x3作为特征输入到som中可视化 # construct the autoencoder model autoencoder =Model(input=input_x, output=encoded_output) autoencoder.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy']) filepath='weights.best.hdf5' checkpoint = ModelCheckpoint(os.path.join(save_dir, filepath), monitor='val_accuracy', verbose=1, save_best_only=True, mode='max') # training history =autoencoder.fit(x_train, train_y, epochs=300, batch_size=32, validation_data=(x_test, test_y), shuffle=True, callbacks=[checkpoint])#用于打乱数据集,每次都会以不同的顺序返回 # print(history.history.keys()) # # Save model and weights # if not os.path.isdir(save_dir): # os.makedirs(save_dir) # model_path = os.path.join(save_dir, filepath) # autoencoder.save(model_path) # print('Saved trained model at %s ' % model_path) # Summarize history for accuracy plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.show() # Summarize history for loss plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.show() #导入最好模型 bestmodel=load_model(filepath) #bestmodel=autoencoder x_train_reduc= bestmodel.predict(x_train) x_test_reduc = bestmodel.predict(x_test) #计算每个类别的准确 #计算测试阶段 每一类的准确率 test_acc=np.ones(classes) for i in range (classes): test0=np.argmax(x_test_reduc[i*test_num:(i+1)*test_num,:],axis=1) test_acc[i]=np.mean(np.equal(test0,i)) acc_mean=np.mean(test_acc) #将最后一个隐藏层的特征送去可视化 输入SOM中,这里送入tsne可视化 x3_layer = Model(inputs=bestmodel.input, outputs=bestmodel.get_layer('layer_x3').output) x3_output = x3_layer.predict(x_train) #可以将该特征保存 然后用matlab的SOM进行可视化 x3_output_test = x3_layer.predict(x_test) #需要可视化的数据 x_train_psne=x3_output x_test_psne=x3_output_test mycolors =['red','green','blue','black','peru','yellow','purple','fuchsia','indigo','turquoise','grey','brown', 'slateblue','darkslategray','deeppink','violet','lime','mediumseagreen','darkolivegreen','olive','saddlebrown','teal'] from sklearn import manifold #降维 tsne = manifold.TSNE(n_components=2) embedding_train = tsne.fit_transform(x_train_psne) for i in range(classes): plt.scatter( embedding_train[i*train_num:(i+1)*train_num,0], embedding_train[i*train_num:(i+1)*train_num,1], c=mycolors[i]) plt.title("tsne-train") plt.show() embedding_test = tsne.fit_transform(x_test_reduc) for i in range(classes): plt.scatter( embedding_test [i*test_num:(i+1)*test_num,0], embedding_test[i*test_num:(i+1)*test_num,1], c=mycolors[i]) plt.title("tsne-test") plt.show() # scio.savemat('C:\\Users\\Administrator.000\\Desktop\\sae相关论文\\demoofpaper\\matlab画图专用\\LOW_embedded.mat', {'Lowtrain_feature': embedding_train,'Lowtest_feature':embedding_train}) # scio.savemat('C:\\Users\\Administrator.000\\Desktop\\sae相关论文\\demoofpaper\\matlab画图专用\\Label.mat', {'train_label': onecolumn_train_y,'test_label':onecolumn_train_y}) #
小项目
# -*- coding: utf-8 -*- #sql="select id,title,sid from 表 where [id] in (select MAX([id]) from 表 group by sid) order by id desc"#定时更新成分时间 可修改 #使用糖转化率作为分类目标,因此不必考虑原材料变化所带来的影响,一个模型即可不必分原材料建立多个模型 # print(f"result_0h: {result_0h}") from itertools import chain from PySide2.QtWidgets import QApplication, QTableWidgetItem,QDesktopWidget, QHeaderView from PySide2.QtUiTools import QUiLoader import pyqtgraph as pg import sys from PySide2.QtCore import Qt, QTimer, QDate from PySide2 import QtCore, QtGui, QtWidgets ##from PySide2.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout, QApplication #from PySide2 import QtGui import PySide2 import MySQLdb import pickle import numpy as np import pandas as pd import datetime import time import os import ctypes from sklearn import preprocessing from sklearn.neighbors import KNeighborsClassifier from random import randrange as rand #分类模型在线训练部分 from sklearn import preprocessing from sklearn.discriminant_analysis import LinearDiscriminantAnalysis import matplotlib.pyplot as plt #from minisom import MiniSom from matplotlib.colors import ListedColormap from sklearn.cross_decomposition import PLSRegression from sklearn import datasets from sklearn.model_selection import GridSearchCV import numpy as np from imblearn.over_sampling import SMOTE from collections import Counter from sklearn.model_selection import train_test_split #计算精度 from sklearn.metrics import precision_score from catboost import CatBoostRegressor,CatBoostClassifier ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid") dirname = os.path.dirname(PySide2.__file__) plugin_path = os.path.join(dirname, 'plugins', 'platforms') os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path #number_batch 存储最近20个批次的罐次序号 在read_onlinedata每1min更新一次 number_batch = [0]*20 number_can = {} #last_len 判断温度是否更新了 last_len = 0 res_0h = 0 res_8h = 0 res_24h = 0 res_40h = 0 a0 = 0 a8 = 0 a24 = 0 a40 = 0 result_0h = [] result_8h = [] result_24h = [] result_40h = [] #####模型在线更新 新样本区分类别后的预测值和真实值######### result40h_calss1_pre = [] result40h_calss1_true = [] result40h_calss2_pre = [] result40h_calss2_true = [] result24h_calss1_pre = [] result24h_calss1_true = [] result24h_calss2_pre = [] result24h_calss2_true = [] result8h_calss1_pre = [] result8h_calss1_true = [] result8h_calss2_pre = [] result8h_calss2_true = [] result0h_calss1_pre = [] result0h_calss1_true = [] result0h_calss2_pre = [] result0h_calss2_true = [] # smote过采样之后新样本的自变量(对应各个时刻的检验数据)和因变量(类别) new_sample_calss1_40h = [] new_sample_calss1_24h = [] new_sample_calss1_8h = [] new_sample_calss1_0h = [] new_sample_calss2_40h = [] new_sample_calss2_24h = [] new_sample_calss2_8h = [] new_sample_calss2_0h = [] new_sampley_calss1_40h = [] new_sampley_calss1_24h = [] new_sampley_calss1_8h = [] new_sampley_calss1_0h = [] new_sampley_calss2_40h = [] new_sampley_calss2_24h = [] new_sampley_calss2_8h = [] new_sampley_calss2_0h = [] # 将UI界面放置在屏幕正中央 def center_window(widget): window = widget.window() window.setGeometry( QtWidgets.QStyle.alignedRect( QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter, window.size(), QtGui.QGuiApplication.primaryScreen().availableGeometry(), # QtGui.QGuiApplication.screens().availableGeometry(), ), ) class Model: # 程序执行后首先会将新样本使用旧模型的真实值预测值对比进行对比,其次将新样本旧样本混合更新模型 def __init__(self): #调试使用 # self.newsample_match() # self.classification_0h() # self.calssmoudle1_train() # self.calssmoudle2_train() self.timer_model = QTimer() self.timer_model.timeout.connect(self.month_change) # 计时时间查询是否达到一个多月的时间间隔 self.timer_model.start(60 * 1000 * 60 * 24) # 以毫秒计 一天判断一次是否达更新模型的时间要求 #新样本的匹配 def newsample_match(self): # 首先读取乙醇表中的批次和对应实际值数据 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_ethanol = db.cursor() sqltem = "select ID,罐次,乙醇出罐浓度 from ethanol where 罐次 is not null and 乙醇出罐浓度 is not null" try: cursor_ethanol.execute(sqltem) ethanol = cursor_ethanol.fetchall() except: ethanol = [] print("乙醇出罐浓度数据读取失败") cursor_ethanol.close() db.close() ethanol_array = np.array(ethanol) # 列表元组 if ethanol: row = cursor_ethanol.rowcount # 取得记录个数,用于设置表格的行数 # 读取初始时刻检验数据,调用0h分类模型(xgb模型)判断属于哪一类别 for i in range(row): # 对所有批次逐一处理 tank_number = str(ethanol_array[i][1]) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_inspection = db.cursor() sqlprediction0h = "select * from prediction0h where tank_no ='%s'" % tank_number try: cursor_inspection.execute(sqlprediction0h) inspection = cursor_inspection.fetchone() except: inspection = [] print("初始时刻数据读取失败") cursor_inspection.close() db.close() with open('scaler_train_0h_classification', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_0h_classification', 'rb')as f: lda_test = pickle.load(f) # 导入xgb模型 if inspection: value = float(ethanol[i][2]) * 1.195 #乙醇体积比*1.195就是实际出罐的乙醇浓度 inspection_test = scaler_test.transform(np.matrix(inspection[5:])) prob_inspection = lda_test.predict(inspection_test) # 1:表示第一批 2:表示第二批 if prob_inspection[0] == 1: if value >= 15.75: label = 1 elif (15.4 < value) & (value < 15.75): label = 2 elif value <= 15.4: label = 3 # 读prediction40表数据 输入模型获得预测值 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) # 判断40h的表中是否有此批次如果有此批次,确定发酵阶段 cursor40h = db.cursor() sql40h = "select * from prediction40h where tank_no ='%s'" % tank_number try: res_40h = cursor40h.execute(sql40h) if res_40h == 1: result_40h = cursor40h.fetchone() else: result_40h=[] except: result_40h = [] print("Error: handle unable to fecth 40h data1") cursor40h.close() db.close() # 读prediction24表数据 输入模型获得预测值 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) # 判断24h的表中是否有此批次如果有此批次,确定发酵阶段 cursor24h = db.cursor() sql24h = "select * from prediction24h where tank_no ='%s'" % tank_number try: res_24h = cursor24h.execute(sql24h) if res_24h == 1: result_24h = cursor24h.fetchone() else: result_24h=[] except: result_24h = [] print("Error: handle unable to fecth 24h data1") cursor24h.close() db.close() # 读prediction8表数据 输入模型获得预测值 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) # 判断8h的表中是否有此批次如果有此批次,确定发酵阶段 cursor8h = db.cursor() sql8h = "select * from prediction8h where tank_no ='%s'" % tank_number try: res_8h = cursor8h.execute(sql8h) if res_8h == 1: result_8h = cursor8h.fetchone() else: result_8h=[] except: result_8h = [] print("Error: handle unable to fecth 8h data1") cursor8h.close() db.close() # 该批次对应的检验数据不为空才进行处理 如果为空说明此发酵时刻没有匹配到数据没必要进行处理 if result_40h: new_sample_calss1_40h.append(result_40h[5:]) # 新样本的自变量 new_sampley_calss1_40h.append(value) # 新样本的因变量 # 调用xgb模型 with open('scaler_train_40h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_40h', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 x_test = scaler_test.transform(np.matrix(result_40h[5:])) prob = lda_test.predict(x_test) result40h_calss1_pre.append(int(prob))#模型的预测值 result40h_calss1_true.append(label)#实际的类别 if result_24h: new_sample_calss1_24h.append(result_24h[5:]) # 新样本的自变量 new_sampley_calss1_24h.append(value) # 新样本的因变量 # 调用FDA模型 with open('scaler_train_24h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_24h', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 x_test = scaler_test.transform(np.matrix(result_24h[5:])) # 获得测试数据输出的概率投影 prob = lda_test.predict(x_test) result24h_calss1_pre.append(int(prob)) result24h_calss1_true.append(label) if result_8h: new_sample_calss1_8h.append(result_8h[5:]) # 新样本的自变量 new_sampley_calss1_8h.append(value) # 新样本的因变量 # 调用FDA模型 with open('scaler_train_8h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_8h', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 x_test = scaler_test.transform(np.matrix(result_8h[5:])) # 获得测试数据输出的概率投影 prob = lda_test.predict(x_test) result8h_calss1_pre.append(int(prob)) result8h_calss1_true.append(label) #如果此批次属于初始时刻,进行以下处理 if inspection: new_sample_calss1_0h.append(inspection[5:]) # 新样本的自变量 new_sampley_calss1_0h.append(value) # 新样本的因变量 # 调用FDA模型 with open('scaler_train_0h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_0h', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 x_test = scaler_test.transform(np.matrix(inspection[5:])) # 获得测试数据输出的概率投影 prob = lda_test.predict(x_test) result0h_calss1_pre.append(int(prob)) # 将预测值添加到相应的预测值列表 result0h_calss1_true.append(label) # 将真实值添加到相应的真实值列表 # 类别二新样本匹配 elif prob_inspection[0] == 2: if value >= 14.6: label = 1 elif (13.8 < value) & (value < 14.6): label = 2 elif value <= 13.8: label = 3 # 读prediction40表数据 输入模型获得预测值 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) # 判断40h的表中是否有此批次如果有此批次,确定发酵阶段 cursor40h = db.cursor() sql40h = "select * from prediction40h where tank_no ='%s'" % tank_number try: res_40h = cursor40h.execute(sql40h) if res_40h == 1: result_40h = cursor40h.fetchone() else: result_40h = [] except: result_40h = [] # res_40h = 0 print("Error: handle unable to fecth 40h data2") cursor40h.close() db.close() db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) # 判断24h的表中是否有此批次如果有此批次,确定发酵阶段 cursor24h = db.cursor() sql24h = "select * from prediction24h where tank_no ='%s'" % tank_number try: res_24h = cursor24h.execute(sql24h) if res_24h == 1: result_24h = cursor24h.fetchone() else: result_24h = [] except: result_24h = [] print("Error: handle unable to fecth 24h data2") cursor24h.close() db.close() # 读prediction8表数据 输入模型获得预测值 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) # 判断8h的表中是否有此批次如果有此批次,确定发酵阶段 cursor8h = db.cursor() sql8h = "select * from prediction8h where tank_no ='%s'" % tank_number try: res_8h = cursor8h.execute(sql8h) if res_8h == 1: result_8h = cursor8h.fetchone() else: result_8h = [] except: result_8h = [] print("Error: handle unable to fecth 8h data") cursor8h.close() db.close() if result_40h: # 将读到的新样本加入到空矩阵中,方便和原来的检验数据进行合并 new_sample_calss2_40h.append(result_40h[5:]) # 新样本的自变量 new_sampley_calss2_40h.append(value) # 新样本的因变量 # 调用xgb模型 with open('scaler_train2_40h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel2_40h', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 x_test = scaler_test.transform(np.matrix(result_40h[5:])) # 获得测试数据输出的概率投影 prob = lda_test.predict(x_test) result40h_calss2_pre.append(int(prob)) result40h_calss2_true.append(label) if result_24h: new_sample_calss2_24h.append(result_24h[5:]) new_sampley_calss2_24h.append(value) # 调用xgb模型 with open('scaler_train2_24h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel2_24h', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 x_test = scaler_test.transform(np.matrix(result_24h[5:])) # 获得测试数据输出的概率投影 prob = lda_test.predict(x_test) result24h_calss2_pre.append(int(prob)) result24h_calss2_true.append(label) if result_8h: # 不为空才进行下面的预测 否则得不到预测值没办法绘制曲线 new_sample_calss2_8h.append(result_8h[5:]) new_sampley_calss2_8h.append(value) # 调用xgb模型 with open('scaler_train2_8h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel2_8h', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 x_test = scaler_test.transform(np.matrix(result_8h[5:])) # 获得测试数据输出的概率投影 prob = lda_test.predict(x_test) result8h_calss2_pre.append(int(prob)) result8h_calss2_true.append(label) if inspection: new_sample_calss2_0h.append(inspection[5:]) new_sampley_calss2_0h.append(value) # 调用FDA模型 with open('scaler_train2_0h', 'rb')as f: scaler_test = pickle.load(f) with open('ldamodel2_0h', 'rb')as f: lda_test = pickle.load(f) # 测试数据标准化 x_test = scaler_test.transform(np.matrix(inspection[5:])) # 获得测试数据输出的概率投影 prob = lda_test.predict(x_test) # prob = lda_test.predict_proba(x_test) # a40 = round(prob[0][0], 3) # b40 = round(prob[0][1], 3) # c40 = round(prob[0][2], 3) result0h_calss2_pre.append(int(prob)) result0h_calss2_true.append(label) def classification_0h(self): # 初始时刻分类模型的构建D:\sxt\GraduatePrograms\Tieling\finalsoftware\1206\pyside1209 select_classification = 0 # 0 是0h 8是 8h 24是24h 40是40h 选择训练的是哪部分数据 # data = pd.read_excel( # 'D:\\TL_Optimization_Model\\TL_Process_Interface\\regression_liquefaction_yeast_out.xlsx', # sheet_name="Sheet1", ) data = pd.read_excel( 'regression_liquefaction_yeast_out.xlsx', sheet_name="Sheet1", ) X = data.iloc[:, 1:38] # 取1~36列数据 y = data.loc[:, '乙醇_体积比(出罐)'] # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, y) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) # 测试数据标准化 scaler_train = preprocessing.StandardScaler().fit(X_train) X = scaler_train.transform(X_train) # lda对测试数据进行聚类,并绘制图像 lda = CatBoostClassifier(iterations=1000, learning_rate=0.25, random_seed=888, depth=8, verbose=100) #lda = LinearDiscriminantAnalysis(n_components=None)由fda模型改为xgb模型 lda.fit(X, y_train) # 三个label对应三类样本 # X_new = lda.transform(X)#经过转化之后的输入矩阵 # ##绘图数据## # # 设置X和Y的边界值 # X_train = X_train.values # X_min, X_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1 # y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1 # # 使用meshgrid函数返回X和Y两个坐标向量矩阵 # h = .01 # xx, yy = np.meshgrid(np.arange(X_min, X_max, h), np.arange(y_min, y_max, h)) # # 设置colormap颜色 # cm_bright = ListedColormap(['#D9e021', '#0D8ECF']) # plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright) # plt.title('样本类别区分模型训练结果') # plt.axis('tight') # plt.show() import pickle # 标准化数据 模型保存 with open('scaler_train_' + str(select_classification) + 'h_classification', 'wb')as f: pickle.dump(scaler_train, f) with open('scaler_train_' + str(select_classification) + 'h_classification', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_' + str(select_classification) + 'h_classification', 'wb')as f: pickle.dump(lda, f) with open('ldamodel_' + str(select_classification) + 'h_classification', 'rb')as f: lda2 = pickle.load(f) # 导入LDA模型 # 模型的测试 # 模型的测试程序 x_test = scaler_test.transform(X_test) prob = lda2.predict(x_test) sim_time = np.arange(len(x_test)) # ##绘图数据## # plt.figure(figsize=(16, 12)) # plt.plot(sim_time, y_test, '*-', sim_time, prob, 'o-.') # plt.xlabel('批次', fontsize=14) # plt.ylabel('样本类别(1:calss1 2:calss2)', fontsize=14) # plt.title('样本分类真实值与预测值对比' + str(select_classification) + 'h', fontsize=14) # plt.legend(['true', 'predicted']) # plt.show() # #train_fig = 'classification_true_predicted_calss20h.png' # #plt.savefig(train_fig, dpi=500) print(f"样本类别模型准确率: {precision_score(y_test, prob, average='micro')}") if precision_score(y_test, prob, average='micro') > 0.8: pass #继续执行程序 else: self.classification_0h() #不满足要求再次进行模型训练 # 第一类模型的构建 def calssmoudle1_train(self): for i in {0, 8, 24, 40}: select = i if select == 40: data = pd.read_excel('sample40hpart1.xlsx', sheet_name="Sheet1", ) if new_sample_calss1_40h: X = data.iloc[:, 1:123] # 40h 训练数据 X = np.array(X) sample40h = np.array(new_sample_calss1_40h) X = np.vstack((X, sample40h))#新样本自变量融合 y = data.loc[:, '乙醇_体积比(出罐)'] sample40hy = pd.Series(new_sampley_calss1_40h) y = pd.concat([y, sample40hy])#新样本因变量融合 label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 15.75)] = 1 label[np.where((15.4 < y) & (y < 15.75))] = 2 label[np.where(y <= 15.4)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) #划分训练集测试集 elif select == 24: data = pd.read_excel('sample24hpart1.xlsx', sheet_name="Sheet1", ) if new_sample_calss1_24h: X = data.iloc[:, 1:93] X = np.array(X) #X = np.concatenate((X1, X2), axis=1) sample24h = np.array(new_sample_calss1_24h) X = np.vstack((X, sample24h)) y = data.loc[:, '乙醇_体积比(出罐)'] sample24hy = pd.Series(new_sampley_calss1_24h) y = pd.concat([y, sample24hy]) label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 15.75)] = 1 label[np.where((15.4 < y) & (y < 15.75))] = 2 label[np.where(y <= 15.4)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) elif select == 8: data = pd.read_excel('sample8hpart1.xlsx', sheet_name="Sheet1", ) # 8h训练数据(没有糖化酶、酒母醪、液化醪等数据) if new_sample_calss1_8h: X = data.iloc[:, 1:60] X = np.array(X) #X2 = data.iloc[:, 82:90] #X = np.concatenate((X1, X2), axis=1) sample8h = np.array(new_sample_calss1_8h) X = np.vstack((X, sample8h)) y = data.loc[:, '乙醇_体积比(出罐)'] sample8hy = pd.Series(new_sampley_calss1_8h) y = pd.concat([y, sample8hy]) label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 15.75)] = 1 label[np.where((15.4 < y) & (y < 15.75))] = 2 label[np.where(y <= 15.4)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) elif select == 0: data = pd.read_excel('sample0hpart1.xlsx', sheet_name="Sheet1", ) if new_sample_calss1_0h: X = data.iloc[:, 1:38] X = np.array(X) sample0h = np.array(new_sample_calss1_0h) X = np.vstack((X, sample0h)) y = data.loc[:, '乙醇_体积比(出罐)'] sample0hy = pd.Series(new_sampley_calss1_0h) y = pd.concat([y, sample0hy]) label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 15.75)] = 1 label[np.where((15.4 < y) & (y < 15.75))] = 2 label[np.where(y <= 15.4)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) #匹配到相应阶段的新样本,只要不为空就进行模型的训练 if new_sample_calss1_0h or new_sample_calss1_8h or new_sample_calss1_24h or new_sample_calss1_40h: # 训练数据标准化 scaler_train = preprocessing.StandardScaler().fit(X_train) X = scaler_train.transform(X_train) # lda对测试数据进行聚类,并绘制图像 lda = LinearDiscriminantAnalysis(n_components=2) lda.fit(X, y_train) # 三个label对应三类样本 X_new = lda.transform(X) map_color = {1: 'r', 2: 'g', 3: 'b'} label1 = np.reshape(y_train, (len(y_train),)) label2 = label1.tolist() # 将label转化为列表 color = list(map(lambda x: map_color[x], label2)) # plt.figure(figsize=(16, 12)) # # 将聚类后的样本绘制散点图 # plt.scatter(X_new[:, 0], X_new[:, 1], marker='o', c=color) # plt.show() # 标准化数据 模型保存 with open('scaler_train_' + str(select) + 'h', 'wb')as f: pickle.dump(scaler_train, f) with open('scaler_train_' + str(select) + 'h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_' + str(select) + 'h', 'wb')as f: pickle.dump(lda, f) with open('ldamodel_' + str(select) + 'h', 'rb')as f: lda2 = pickle.load(f) # 导入LDA模型 # 模型的测试程序 x_test = scaler_test.transform(X_test) prob = lda2.predict(x_test) sim_time = np.arange(len(x_test)) # # 绘制新模型预测值和真实值对比图 # plt.figure(figsize=(16, 12)) # plt.plot(sim_time, y_test, '*-', sim_time, prob, 'o-.') # plt.xlabel('批次', fontsize=14) # plt.ylabel('状态分类(1:优 2:中 3:差)', fontsize=14) # plt.title('Calss2测试集真实值与预测值对比' + str(select) + 'h', fontsize=14) # plt.legend(['true', 'predicted']) # plt.show() # #train_fig = 'test_true_predicted_calss20h.png' # #plt.savefig(train_fig, dpi=500) print(f"第一类模型准确率: {precision_score(y_test, prob, average='micro')}") if precision_score(y_test, prob, average='micro') > 0.40:#绝对准确度>0.5我们认为满足要求 pass # # 标准化数据 模型保存 # with open('scaler_train_' + str(select) + 'h', 'wb')as f: # pickle.dump(scaler_train, f) # with open('ldamodel_' + str(select) + 'h', 'wb')as f: # pickle.dump(lda, f) else: self.calssmoudle1_train() def calssmoudle2_train(self): # 构建一个for循环依次对四个模型进行更新 # 第二类模型的构建 for i in {0, 8, 24, 40}: select2 = i if select2 == 40: data = pd.read_excel('sample40hpart2.xlsx', sheet_name="Sheet1", ) if new_sample_calss2_40h: X = data.iloc[:, 1:123] # 40h 训练数据 X = np.array(X) sample40h = np.array(new_sample_calss2_40h) X = np.vstack((X, sample40h)) y = data.loc[:, '乙醇_体积比(出罐)'] sample40hy = pd.Series(new_sampley_calss2_40h) y = pd.concat([y, sample40hy]) label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 14.6)] = 1 label[np.where((13.8 < y) & (y < 14.6))] = 2 label[np.where(y <= 13.8)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) elif select2 == 24: data = pd.read_excel('sample24hpart2.xlsx', sheet_name="Sheet1", ) if new_sample_calss2_24h: X = data.iloc[:, 1:93] # X2 = data.iloc[:, 82:106] # X = np.concatenate((X1, X2), axis=1) X=np.array(X) sample24h = np.array(new_sample_calss2_24h) X = np.vstack((X, sample24h)) # 给样本加标签1:优 2:中 3:差 y = data.loc[:, '乙醇_体积比(出罐)'] sample24hy = pd.Series(new_sampley_calss2_24h) y = pd.concat([y, sample24hy]) label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 14.6)] = 1 label[np.where((13.8 < y) & (y < 14.6))] = 2 label[np.where(y <= 13.8)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) elif select2 == 8: data = pd.read_excel('sample8hpart2.xlsx', sheet_name="Sheet1", ) # 8h训练数据(没有糖化酶、酒母醪、液化醪等数据) if new_sample_calss2_8h: X = data.iloc[:, 1:60] X = np.array(X) # X2 = data.iloc[:, 82:90] # X = np.concatenate((X1, X2), axis=1) sample8h = np.array(new_sample_calss2_8h) X = np.vstack((X, sample8h)) y = data.loc[:, '乙醇_体积比(出罐)'] sample8hy = pd.Series(new_sampley_calss2_8h) y = pd.concat([y, sample8hy]) label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 14.6)] = 1 label[np.where((13.8 < y) & (y < 14.6))] = 2 label[np.where(y <= 13.8)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) elif select2 == 0: data = pd.read_excel('sample0hpart2.xlsx', sheet_name="Sheet1", ) if new_sample_calss2_0h: X = data.iloc[:, 1:38] X = np.array(X) sample0h = np.array(new_sample_calss2_0h) X = np.vstack((X, sample0h)) y = data.loc[:, '乙醇_体积比(出罐)'] sample0hy = pd.Series(new_sampley_calss2_0h) y = pd.concat([y, sample0hy]) #print(type(y)) #print(y.shape) label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where(y >= 14.6)] = 1 label[np.where((13.8 < y) & (y < 14.6))] = 2 label[np.where(y <= 13.8)] = 3 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2) if new_sample_calss2_0h or new_sample_calss2_8h or new_sample_calss2_24h or new_sample_calss2_40h: # 训练数据标准化 scaler_train = preprocessing.StandardScaler().fit(X_train) X = scaler_train.transform(X_train) # 调用XGB模型进行模型训练 # lda = LinearDiscriminantAnalysis(n_components=2) # lda.fit(X, y_train) # 三个label对应三类样本 xgb = CatBoostClassifier(iterations=1000, learning_rate=0.25, random_seed=888, # custom_metric='F1', depth=8, verbose=100) xgb.fit(X, y_train) # X_new = lda.transform(X) # map_color = {1: 'r', 2: 'g', 3: 'b'} # label1 = np.reshape(y_train, (len(y_train),)) # label2 = label1.tolist() # 将label转化为列表 # color = list(map(lambda x: map_color[x], label2)) # plt.figure(figsize=(16, 12)) # # 将聚类后的样本绘制散点图 # plt.scatter(X_new[:, 0], X_new[:, 1], marker='o', c=color) # plt.show() # 标准化数据 模型保存 with open('scaler_train2_' + str(select2) + 'h', 'wb')as f: pickle.dump(scaler_train, f) with open('scaler_train2_' + str(select2) + 'h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel2_' + str(select2) + 'h', 'wb')as f: pickle.dump(xgb, f) with open('ldamodel2_' + str(select2) + 'h', 'rb')as f: lda2 = pickle.load(f) # 导入LDA模型 # 模型的测试程序 x_test = scaler_test.transform(X_test) prob = lda2.predict(x_test) sim_time = np.arange(len(x_test)) # plt.figure(figsize=(16, 12)) # plt.plot(sim_time, y_test, '*-', sim_time, prob, 'o-.') # plt.xlabel('批次', fontsize=14) # plt.ylabel('状态分类(1:优 2:中 3:差)', fontsize=14) # plt.title('Calss2测试集真实值与预测值对比' + str(select2) + 'h', fontsize=14) # plt.legend(['true', 'predicted']) # plt.show() # #train_fig = 'test_true_predicted_calss20h.png' # #plt.savefig(train_fig, dpi=500) print(f"第二类模型准确率: {precision_score(y_test, prob, average='micro')}") if precision_score(y_test, prob, average='micro') > 0.55:#如果绝对准确率大于0.55就进行更新模型的保存 pass # # 标准化数据 模型保存 # with open('scaler_train2_' + str(select2) + 'h', 'wb')as f: # pickle.dump(scaler_train, f) # with open('ldamodel2_' + str(select2) + 'h', 'wb')as f: # pickle.dump(xgb, f) else: self.calssmoudle2_train()#不满足准确率要求,再次训练模型 # 每一月更新一次模型 def month_change(self): global date_old#将修改之后的变量也设置为globall类型 global count_days if int(count_days) >= 45 or int(count_days) <= -45: # 只要相差一个月就更新一下 关键温度曲线和操作变量表 date_old = date_new#如果满足条件 立刻将表中最后一个时间赋值给date_old以进入下一个30天循环来更新模型 print('.............开始模型更新................') self.newsample_match() self.classification_0h() self.calssmoudle1_train() self.calssmoudle2_train() print('.............完成模型更新................') else: print('.............不满足更新模型的时间间隔要求................') #将UI界面放置在屏幕正中央 def center_window(widget): window = widget.window() window.setGeometry( QtWidgets.QStyle.alignedRect( QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter, window.size(), QtGui.QGuiApplication.primaryScreen().availableGeometry(), # QtGui.QGuiApplication.screens().availableGeometry(), ), ) class Stock: def __init__(self): #!!!初始化程序中的程序出现错误不能影响后续功能的初始化 #####加载ui界面######### loader = QUiLoader() loader.registerCustomWidget(pg.PlotWidget) self.ui = loader.load("main0525.ui") # self.ui = QUiLoader().load("main0525.ui") self.ui2 = loader.load("historycurve.ui") self.ui3 = loader.load("suggestion.ui") self.ui4 = loader.load("temperature.ui") # 设置程序图标 self.ui.setWindowIcon(QtGui.QIcon('EthanolProcessOptimization.ico')) self.ui2.setWindowIcon(QtGui.QIcon('EthanolProcessOptimization.ico')) self.ui3.setWindowIcon(QtGui.QIcon('EthanolProcessOptimization.ico')) self.ui4.setWindowIcon(QtGui.QIcon('EthanolProcessOptimization.ico')) ######设置温度曲线###### self.ui.historyPlot.setBackground((250, 250, 250)) # 显示表格线 self.ui.historyPlot.showGrid(x=True, y=True) self.ui.historyPlot.setLabel("left", "T(℃)",size='2pt') self.ui.historyPlot.setLabel("bottom", "Time(h)",size='2pt') # 设置Y轴 刻度 范围 self.ui.historyPlot.setYRange(min=29, max=35) # 最大值(min=29, max=33) self.ui.historyPlot.setXRange(min=1, max=65) # 最大值 self.ui.historyPlot.addLegend() # 水平方向,表格大小拓展到适当的尺寸 self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.ui.tableWidget.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) self.ui3.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.ui3.tableWidget.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) self.ui4.tableWidget2.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.ui4.tableWidget2.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) # 初始化历史浓度区块的时间区段为最近30天 end_date = datetime.datetime.now() start_date = end_date - datetime.timedelta(days=30) self.ui.dateEdit.setDate(QDate(start_date.year, start_date.month, start_date.day)) self.ui.dateEdit_2.setDate(QDate(end_date.year, end_date.month, end_date.day)) ######定时器设置######## self.table_initial()#对所有的表ui表进行初始化 self.select_you_temperture_inspection()#收集温度和检验数据分别放到class1和class2中,方便之后计算关键温度点和计算置信参数更新操作表 # 定时更新发酵罐温度、液位等信息,一分钟一次 self.read_onlinedata() self.timer_onlinedata = QTimer() self.timer_onlinedata.timeout.connect(self.read_onlinedata) self.timer_onlinedata.start(60 * 1000 * 2) # 以毫秒计 # 定时更新温度,一分钟一次 self.read_temp() self.timer_temp = QTimer() self.timer_temp.timeout.connect(self.read_temp) self.timer_temp.start(60 * 1000 * 2) # 以毫秒计 self.curve1.scene().sigMouseMoved.connect(self.mouseover) # 按钮功能的实现 self.ui.pushButton.clicked.connect(self.readhistory_prediction) self.ui.pushButton_3.clicked.connect(self.readhistorycurve) self.ui.pushButton_2.clicked.connect(self.readhistory_true) self.ui.pushButton_4.clicked.connect(self.suggestion) # 罐号选择实现 self.handleSelectionChange() self.ui.comboBox.currentIndexChanged.connect(self.handleSelectionChange) #定时更新较优批次表,关键温度,最优操作区间 self.ui.pushButton_5.clicked.connect(self.key_temperature_table) self.update_operation_interval() #读取一下乙醇表中最后一行的时间信息 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) Maxtime = db.cursor() sqlMaxtime = "select DATETIME from ethanol where ID in(select max(ID) from ethanol )" #搜索表中最后一行对应的时间 try: Maxtime.execute(sqlMaxtime) result_Maxtime = Maxtime.fetchone() # print(f"result_0h: {result_0h}") except: result_Maxtime = [] print("Error:无法获取乙醇表中的最大时间") Maxtime.close() db.close() global date_old global date_new print(f"程序开始运行时乙醇表最后一行时间: {result_Maxtime}") # date_old = datetime.datetime.strptime(str(result_Maxtime), "%Y-%m-%d") # date_old = time.mktime(time.strptime(date_old), '%Y-%m-%d') #date_old = result_Maxtime[0].strftime('%Y-%m-%d')) date_old = datetime.datetime.strptime(str(result_Maxtime[0]), "%Y-%m-%d %H:%M:%S") date_old = time.mktime(time.strptime(str(date_old), "%Y-%m-%d %H:%M:%S")) #隔几天查询一次 self.date_new = QTimer() self.date_new.timeout.connect(self.date_new_update) self.date_new.start(60 * 1000 * 60 * 24 ) # 每隔一天查询一次是否达到一个月 def date_new_update(self): global count_days #两次查询的时间间隔 #将每月更新一次的时间作为起始时间,如果与表中最大时间之差大于30天就进行模型的训练 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) Maxtime_new = db.cursor() sqlMaxtime_new = "select DATETIME from ethanol where ID in(select max(ID) from ethanol )" # 搜索表中最后一行对应的时间 try: Maxtime_new.execute(sqlMaxtime_new) result_Maxtime_new = Maxtime_new.fetchone() # print(f"result_0h: {result_0h}") except: result_Maxtime_new = [] print("Error:无法获取乙醇表中的最大时间") Maxtime_new.close() db.close() date_new = datetime.datetime.strptime(str(result_Maxtime_new[0]), "%Y-%m-%d %H:%M:%S") date_new = time.mktime(time.strptime(str(date_new), "%Y-%m-%d %H:%M:%S")) count_days = int((date_new - date_old) / (24 * 60 * 60))# print(f"距离上次模型更新的天数: {count_days}") #如果达到一个月更新发酵过程关键温度和变量操作区间 if int(count_days)>=45 or int(count_days)<=-45 :#一个月更新关键温度曲线和操作变量表 self.select_you_temperture_inspection_everymonth()#较优批次表 self.key_temperature()#发酵过程关键温度 self.update_operation_interval()#变量操作区间 #初始化时对现有乙醇表中的所有批次进行判断,将较优批次选择出来 def select_you_temperture_inspection(self): global ethanol_rowcount #每月更新一次 将较优批次对应的温度 和检验数据 分两类后存到两个表格中 type1 和 type2 里面包含批次 、 60小时温度 、和检验数据(GI液化 葡萄糖液化 干物液化 葡萄糖酒母 甘油液化 #PH液化 、乙酸液化 、粘度液化 、DP4plus酒母 、 DP4plus液化 、 干物酒母 、DP2液化 、乙酸酒母 、DP2酒母 、死亡率 、糖化酶发酵罐 、酒母醪发酵罐 ) #1、从ethanol表读取较优批次 建表 向表中插入 温度信息和上述液化数据(可先插入所有液化数据之后 再以编号的形式 赋给表格) #注意 依次判断每个批次属于 那个部分 用每个部分的区间判断是否为较优批次 如果为较优批次则进行插值操作 #从乙醇出罐浓度表读取数据 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_ethanol = db.cursor() sqltem = "select ID,罐次,乙醇出罐浓度 from ethanol where 罐次 is not null and 乙醇出罐浓度 is not null " try: cursor_ethanol.execute(sqltem) ethanol = cursor_ethanol.fetchall() except: ethanol = [] print ("乙醇出罐浓度数据读取失败") cursor_ethanol.close() ethanol_array = np.array(ethanol)#列表元组 if ethanol: #self.row = cursor_ethanol.rowcount # 取得记录个数,用于设置表格的行数 ethanol_rowcount = cursor_ethanol.rowcount # 取得记录个数,用于设置表格的行数 for i in range(ethanol_rowcount): tank_number=str(ethanol_array[i][1]) #2在温度表中筛选该批次对应的60个温度信息,限制不为空的情况下 将读取到的温度信息 和ethanol拼接起来 #1在prediction40h表中筛选该批次对应的所有(或者在酒母四十小时表中筛选该批次 ,限制不为空的情况下,将检验信息和第一步数据拼接起来) #0通过0小时应该有的指标来判断 这些批次到底是第一批次还是第二批次 然后根据每个批次的范围来判断是不是较优 系列 然后分别对两部分提取温度信息 和 检验数据 拼接起来 #将经过拼接的两部分数据 分别写进两个mysql表格 方便进一步计算均值 和 置信区间参数 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_inspection = db.cursor() sqlprediction0h = "select * from prediction0h where tank_no ='%s'"%tank_number try: cursor_inspection.execute(sqlprediction0h) inspection = cursor_inspection.fetchone() except: inspection = [] print ("初始时刻数据读取失败") cursor_inspection.close() db.close() #判断这一批次属于哪个类别 with open('scaler_train_0h_classification', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_0h_classification', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 if inspection:#有的批次不出现在prediction0h表中,inspection为空,就不执行类别判定程序 inspection_test = scaler_test.transform(np.matrix(inspection[5:])) prob_inspection = lda_test.predict(inspection_test) if prob_inspection[0]==1:#建立第一类的表格(包含温度、检验数据等信息) ###零、首先判断是不是属于较优批次### if float(ethanol[i][2])*1.195>=15.75: class1_data=[int(ethanol[i][0]),float(ethanol[i][1]),float(ethanol[i][2])]#ID,罐次,乙醇浓度 ###一、先建表### #首先判断表格是否存在 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_insert = db.cursor() try: sql = """ create table if not exists tableName(Type1)( ID int , tank_no float, ethanol float , x1 float, x2 float, x3 float, x4 float, x5 float, x6 float, x7 float, x8 float, x9 float, x10 float, x11 float, x12 float, x13 float, x14 float, x15 float, x16 float, x17 float, x18 float, x19 float, x20 float, x21 float, x22 float, x23 float, x24 float, x25 float, x26 float, x27 float, x28 float, x29 float, x30 float, x31 float, x32 float, x33 float, x34 float, x35 float,x36 float, x37 float, x38 float, x39 float, x40 float,x41 float, x42 float, x43 float, x44 float, x45 float, x46 float, x47 float, x48 float, x49 float, x50 float, x51 float, x52 float, x53 float, x54 float, x55 float, x56 float, x57 float, x58 float, x59 float, x60 float, x61 float,x62 float,x63 float,x64 float,x65 float,x66 float,x67 float,x68 float,x69 float, x70 float,x71 float,x72 float,x73 float,x74 float,x75 float,x76 float,x77 float,x78 float,x79 float,x80 float,x81 float,x82 float,t83 float,t84 float,t85 float,t86 float,t87 float,t88 float,t89 float,t90 float,t91 float,t92 float,t93 float,t94 float,t95 float,t96 float, t97 float,t98 float,t99 float,t100 float,t101 float,t102 float,t103 float,t104 float,t105 float,t106 float,t107 float,t108 float,t109 float,t110 float,t111 float,t112 float,t113 float,t114 float,t115 float,t116 float,t117 float,t118 float,t119 float,t120 float,t121 float,t122 float, t123 float,t124 float,t125 float,t126 float,t127 float,t128 float,t129 float,t130 float,t131 float,t132 float,t133 float,t134 float,t135 float,t136 float,t137 float,t138 float,t139 float,t140 float,t141 float,t142 float ) """ cursor_insert.execute(sql) print('Create Table Type1 OK!') except: print('数据表Type1已存在或未成功建立!') # 插入数据语句 query = """insert into Type1( ID, tank_no, ethanol,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,x31,x32,x33,x34,x35,x36,x37,x38,x39,x40,x41,x42,x43,x44,x45,x46,x47,x48,x49,x50,x51,x52,x53,x54,x55,x56,x57,x58,x59,x60,x61,x62,x63,x64,x65,x66,x67,x68,x69,x70,x71,x72,x73,x74,x75, x76,x77,x78,x79,x80,x81,x82,t83,t84,t85,t86,t87,t88,t89,t90,t91,t92,t93,t94,t95,t96,t97,t98,t99,t100,t101,t102,t103,t104,t105,t106,t107,t108,t109,t110,t111,t112,t113,t114,t115,t116,t117,t118,t119,t120,t121,t122,t123,t124,t125,t126,t127,t128,t129,t130,t131,t132,t133,t134,t135,t136,t137,t138,t139,t140,t141,t142 ) values( %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s )""" ###二、检索数据### #从prediction40表中读取检验数据和前40小时温度信息 cursor_prediction40 = db.cursor() sqltem = "select * from prediction40h where tank_no ='%s'"%tank_number try: cursor_prediction40.execute(sqltem) inspection_temp40 = cursor_prediction40.fetchone() except: inspection_temp40 = () print ("检验数据和前40h温度数据读取失败") cursor_prediction40.close() if inspection_temp40: for tk in inspection_temp40[5:-1]: class1_data.append([float(tk)])#进行类型的转换否则容易出错 else: class1_data=() if class1_data: cursor_temp60 = db.cursor() sqltem = "select * from temperature where tank_no ='%s' and T_41h is not null and T_50h is not null and T_60h is not null"%tank_number#通过多个条件的限制来保证该批次符合筛选的要求 try: cursor_temp60.execute(sqltem) inspection_temp60 = cursor_temp60.fetchone() #print(inspection_temp60) except: inspection_temp60 = () print ("type1:41h-60h小时温度数据读取失败") cursor_temp60.close() if inspection_temp60: for tk in inspection_temp60[43:-1]: class1_data.append([float(tk)]) else : class1_data =() if class1_data: class1_data=tuple(class1_data) cursor_insert.execute(query,class1_data) #向数据库表中插入检测到的较优批次(针对第一类样本) cursor_insert.close() db.commit() db.close() elif prob_inspection[0]==2: ###零、首先判断是不是属于较优批次### if float(ethanol[i][2])*1.195>=14.6:#对第二列的第一个值进行操作 硬核行数对应起来 class2_data=[int(ethanol[i][0]),float(ethanol[i][1]),float(ethanol[i][2])] ###一、先建表### db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_insert = db.cursor() try: sql=""" create table if not exists tableName(Type2) ( ID int , tank_no float, ethanol float , x1 float, x2 float, x3 float, x4 float, x5 float, x6 float, x7 float, x8 float, x9 float, x10 float, x11 float, x12 float, x13 float, x14 float, x15 float, x16 float, x17 float, x18 float, x19 float, x20 float, x21 float, x22 float, x23 float, x24 float, x25 float, x26 float, x27 float, x28 float, x29 float, x30 float, x31 float, x32 float, x33 float, x34 float, x35 float,x36 float, x37 float, x38 float, x39 float, x40 float,x41 float, x42 float, x43 float, x44 float, x45 float, x46 float, x47 float, x48 float, x49 float, x50 float, x51 float, x52 float, x53 float, x54 float, x55 float, x56 float, x57 float, x58 float, x59 float, x60 float, x61 float,x62 float,x63 float,x64 float,x65 float,x66 float,x67 float,x68 float,x69 float, x70 float,x71 float,x72 float,x73 float,x74 float,x75 float,x76 float,x77 float,x78 float,x79 float,x80 float,x81 float,x82 float,t83 float,t84 float,t85 float,t86 float,t87 float,t88 float,t89 float,t90 float,t91 float,t92 float,t93 float,t94 float,t95 float,t96 float, t97 float,t98 float,t99 float,t100 float,t101 float,t102 float,t103 float,t104 float,t105 float,t106 float,t107 float,t108 float,t109 float,t110 float,t111 float,t112 float,t113 float,t114 float,t115 float,t116 float,t117 float,t118 float,t119 float,t120 float,t121 float,t122 float, t123 float,t124 float,t125 float,t126 float,t127 float,t128 float,t129 float,t130 float,t131 float,t132 float,t133 float,t134 float,t135 float,t136 float,t137 float,t138 float,t139 float,t140 float,t141 float,t142 float ) """ cursor_insert.execute(sql) print('Create Table Class2 OK!') except: print('数据表Type2已存在或未成功建立!') #插入数据语句 query = """insert into Type2( ID, tank_no, ethanol,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,x31,x32,x33,x34,x35,x36,x37,x38,x39,x40,x41,x42,x43,x44,x45,x46,x47,x48,x49,x50,x51,x52,x53,x54,x55,x56,x57,x58,x59,x60,x61,x62,x63,x64,x65,x66,x67,x68,x69,x70,x71,x72,x73,x74,x75, x76,x77,x78,x79,x80,x81,x82,t83,t84,t85,t86,t87,t88,t89,t90,t91,t92,t93,t94,t95,t96,t97,t98,t99,t100,t101,t102,t103,t104,t105,t106,t107,t108,t109,t110,t111,t112,t113,t114,t115,t116,t117,t118,t119,t120,t121,t122,t123,t124,t125,t126,t127,t128,t129,t130,t131,t132,t133,t134,t135,t136,t137,t138,t139,t140,t141,t142 ) values( %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s )""" ###二、检索数据### cursor_prediction40 = db.cursor() sqltem = "select * from prediction40h where tank_no ='%s'"%tank_number try: cursor_prediction40.execute(sqltem) inspection_temp40 = cursor_prediction40.fetchone() except: inspection_temp40 = () print ("type2:检验数据和0h-40h数据读取失败") cursor_prediction40.close() if inspection_temp40: for tk in inspection_temp40[5:-1]: class2_data.append(float(tk)) else: class2_data=() if class2_data: cursor_temp60 = db.cursor() sqltem = "select * from temperature where tank_no user='%s' and T_41h is not null and T_50h is not null and T_60h is not null"%tank_number try: cursor_temp60.execute(sqltem) inspection_temp60 = cursor_temp60.fetchone() except: inspection_temp60 = [] print ("type2:41h-60h小时温度数据读取失败") cursor_temp60.close() if inspection_temp60: for tk in inspection_temp60[43:-1]: class2_data.append(float(tk)) else: class2_data=() if class2_data: #先将列表转换成元组 class2_data = tuple(class2_data) cursor_insert.execute(query,class2_data)#将检测到的 cursor_insert.close() db.commit() db.close() else:#分类程序出现错误,不对筛选的批次进行处理 pass #每次都重新查询整个乙醇表效率太低,下次更新type1和type2时从上次结束查询的位置开始本次查询 def select_you_temperture_inspection_everymonth(self): global ethanol_rowcount db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_ethanol = db.cursor() #需要调试查看ethanol_rowcount表述方法可不可行 sqltem = "select ID,罐次,乙醇出罐浓度 from ethanol where 罐次 is not null and 乙醇出罐浓度 is not null and ID between ethanol_rowcount and (select max(ID) from ethanol) " try: cursor_ethanol.execute(sqltem) ethanol = cursor_ethanol.fetchall() except: ethanol = [] print("乙醇出罐浓度数据读取失败") cursor_ethanol.close() ethanol_array = np.array(ethanol) # 列表元组 if ethanol: ethanol_rowcount_everymonth = cursor_ethanol.rowcount # 取得记录个数,用于设置表格的行数 for i in range(ethanol_rowcount_everymonth): tank_number = str(ethanol_array[i][1]) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_inspection = db.cursor() sqlprediction0h = "select * from prediction0h where tank_no ='%s'" % tank_number try: cursor_inspection.execute(sqlprediction0h) inspection = cursor_inspection.fetchone() except: inspection = [] print("初始时刻数据读取失败") cursor_inspection.close() db.close() with open('scaler_train_0h_classification', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('ldamodel_0h_classification', 'rb')as f: lda_test = pickle.load(f) # 导入LDA模型 # 测试数据标准化 if inspection: ###问题 有的批次像347没有出现在prediction0中 inspection_test = scaler_test.transform(np.matrix(inspection[5:])) prob_inspection = lda_test.predict(inspection_test) #print(prob_inspection) if prob_inspection[0] == 1: # 建立第一类的表格(包含温度、检验数据等信息) if float(ethanol[i][2]) * 1.195 >= 15.75: # 对第二列的第一个值进行操作 class1_data = [int(ethanol[i][0]), float(ethanol[i][1]), float(ethanol[i][2])] # ID,罐次,乙醇浓度 ###一、先建表### db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_insert = db.cursor() try: sql = """ create table if not exists tableName(Type1)( ID int primary key , tank_no float, ethanol float , x1 float, x2 float, x3 float, x4 float, x5 float, x6 float, x7 float, x8 float, x9 float, x10 float, x11 float, x12 float, x13 float, x14 float, x15 float, x16 float, x17 float, x18 float, x19 float, x20 float, x21 float, x22 float, x23 float, x24 float, x25 float, x26 float, x27 float, x28 float, x29 float, x30 float, x31 float, x32 float, x33 float, x34 float, x35 float,x36 float, x37 float, x38 float, x39 float, x40 float,x41 float, x42 float, x43 float, x44 float, x45 float, x46 float, x47 float, x48 float, x49 float, x50 float, x51 float, x52 float, x53 float, x54 float, x55 float, x56 float, x57 float, x58 float, x59 float, x60 float, x61 float,x62 float,x63 float,x64 float,x65 float,x66 float,x67 float,x68 float,x69 float, x70 float,x71 float,x72 float,x73 float,x74 float,x75 float,x76 float,x77 float,x78 float,x79 float,x80 float,x81 float,x82 float,t83 float,t84 float,t85 float,t86 float,t87 float,t88 float,t89 float,t90 float,t91 float,t92 float,t93 float,t94 float,t95 float,t96 float, t97 float,t98 float,t99 float,t100 float,t101 float,t102 float,t103 float,t104 float,t105 float,t106 float,t107 float,t108 float,t109 float,t110 float,t111 float,t112 float,t113 float,t114 float,t115 float,t116 float,t117 float,t118 float,t119 float,t120 float,t121 float,t122 float, t123 float,t124 float,t125 float,t126 float,t127 float,t128 float,t129 float,t130 float,t131 float,t132 float,t133 float,t134 float,t135 float,t136 float,t137 float,t138 float,t139 float,t140 float,t141 float,t142 float ) """ cursor_insert.execute(sql) print('Create Table Type1 OK!') except: print('数据表Type1已存在或未成功建立!') query = """insert into Type1( ID, tank_no, ethanol,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,x31,x32,x33,x34,x35,x36,x37,x38,x39,x40,x41,x42,x43,x44,x45,x46,x47,x48,x49,x50,x51,x52,x53,x54,x55,x56,x57,x58,x59,x60,x61,x62,x63,x64,x65,x66,x67,x68,x69,x70,x71,x72,x73,x74,x75, x76,x77,x78,x79,x80,x81,x82,t83,t84,t85,t86,t87,t88,t89,t90,t91,t92,t93,t94,t95,t96,t97,t98,t99,t100,t101,t102,t103,t104,t105,t106,t107,t108,t109,t110,t111,t112,t113,t114,t115,t116,t117,t118,t119,t120,t121,t122,t123,t124,t125,t126,t127,t128,t129,t130,t131,t132,t133,t134,t135,t136,t137,t138,t139,t140,t141,t142 ) values( %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s )""" ###二、检索数据### # 从prediction40表中读取检验数据和前40小时温度信息 cursor_prediction40 = db.cursor() sqltem = "select * from prediction40h where tank_no ='%s'" % tank_number try: cursor_prediction40.execute(sqltem) inspection_temp40 = cursor_prediction40.fetchone() except: inspection_temp40 = () print("检验数据和前40h温度数据读取失败") cursor_prediction40.close() if inspection_temp40: for tk in inspection_temp40[5:-1]: class1_data.append([float(tk)]) # 进行类型的转换否则容易出错 else: class1_data = () if class1_data: cursor_temp60 = db.cursor() sqltem = "select * from temperature where tank_no ='%s' and T_41h is not null and T_50h is not null and T_60h is not null" % tank_number # 通过多个条件的限制来保证该批次符合筛选的要求 try: cursor_temp60.execute(sqltem) inspection_temp60 = cursor_temp60.fetchone() except: inspection_temp60 = () print("41h-60h小时温度数据读取失败") cursor_temp60.close() if inspection_temp60: for tk in inspection_temp60[43:-1]: class1_data.append([float(tk)]) else: class1_data =() if class1_data: class1_data = tuple(class1_data) cursor_insert.execute(query, class1_data) # 向数据库表中插入检测到的较优批次(针对第一类样本) cursor_insert.close() db.commit() # 关闭游标,提交,关闭数据库连接 db.close() # 如果没有这些关闭操作,执行后在数据库中查看不到数据 elif prob_inspection[0] == 2: ###零、首先判断是不是属于较优批次### if float(ethanol[i][2]) * 1.195 >= 14.6: class2_data = [int(ethanol[i][0]), float(ethanol[i][1]), float(ethanol[i][2])] ###一、建表### db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_insert = db.cursor() try:#注意不能设置自增类型,因为ID是不连续的(剔除乙醇表中的无效数据) sql = """ create table if not exists tableName(Type2)( ID int primary key , tank_no float, ethanol float , x1 float(32), x2 float(32), x3 float(32), x4 float(32), x5 float(32), x6 float(32), x7 float(32), x8 float(32), x9 float(32), x10 float(32), x11 float(32), x12 float(32), x13 float(32), x14 float(32), x15 float(32), x16 float(32), x17 float(32), x18 float(32), x19 float(32), x20 float(32), x21 float(32), x22 float(32), x23 float(32), x24 float(32), x25 float(32), x26 float(32), x27 float(32), x28 float(32), x29 float(32), x30 float(32), x31 float(32), x32 float(32), x33 float(32), x34 float(32), x35 float(32),x36 float(32), x37 float(32), x38 float(32), x39 float(32), x40 float(32),x41 float(32), x42 float(32), x43 float(32), x44 float(32), x45 float(32), x46 float(32), x47 float(32), x48 float(32), x49 float(32), x50 float(32), x51 float(32), x52 float(32), x53 float(32), x54 float(32), x55 float(32), x56 float(32), x57 float(32), x58 float(32), x59 float(32), x60 float(32), x61 float(32),x62 float(32),x63 float(32),x64 float(32),x65 float(32),x66 float(32),x67 float(32),x68 float(32),x69 float(32), x70 float(32),x71 float(32),x72 float(32),x73 float(32),x74 float(32),x75 float(32),x76 float(32),x77 float(32),x78 float(32),x79 float(32),x80 float(32),x81 float(32),x82 float(32),t83 float(32),t84 float(32),t85 float(32),t86 float(32),t87 float(32),t88 float(32),t89 float(32),t90 float(32),t91 float(32),t92 float(32),t93 float(32),t94 float(32),t95 float(32),t96 float(32), t97 float(32),t98 float(32),t99 float(32),t100 float(32),t101 float(32),t102 float(32),t103 float(32),t104 float(32),t105 float(32),t106 float(32),t107 float(32),t108 float(32),t109 float(32),t110 float(32),t111 float(32),t112 float(32),t113 float(32),t114 float(32),t115 float(32),t116 float(32),t117 float(32),t118 float(32),t119 float(32),t120 float(32),t121 float(32),t122 float(32), t123 float(32),t124 float(32),t125 float(32),t126 float(32),t127 float(32),t128 float(32),t129 float(32),t130 float(32),t131 float(32),t132 float(32),t133 float(32),t134 float(32),t135 float(32),t136 float(32),t137 float(32),t138 float(32),t139 float(32),t140 float(32),t141 float(32),t142 float(32) ) """ cursor_insert.execute(sql) print('Create Table Class2 OK!') except: print('数据表Type2已存在或未成功建立!') # 插入数据语句 query = """insert into Type2( ID, tank_no, ethanol,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,x31,x32,x33,x34,x35,x36,x37,x38,x39,x40,x41,x42,x43,x44,x45,x46,x47,x48,x49,x50,x51,x52,x53,x54,x55,x56,x57,x58,x59,x60,x61,x62,x63,x64,x65,x66,x67,x68,x69,x70,x71,x72,x73,x74,x75, x76,x77,x78,x79,x80,x81,x82,t83,t84,t85,t86,t87,t88,t89,t90,t91,t92,t93,t94,t95,t96,t97,t98,t99,t100,t101,t102,t103,t104,t105,t106,t107,t108,t109,t110,t111,t112,t113,t114,t115,t116,t117,t118,t119,t120,t121,t122,t123,t124,t125,t126,t127,t128,t129,t130,t131,t132,t133,t134,t135,t136,t137,t138,t139,t140,t141,t142 ) values( %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s )""" ###二、检索数据### cursor_prediction40 = db.cursor() sqltem = "select * from prediction40h where tank_no ='%s'" % tank_number try: cursor_prediction40.execute(sqltem) inspection_temp40 = cursor_prediction40.fetchone() except: inspection_temp40 = () print("type2:检验数据和0h-40h数据读取失败") cursor_prediction40.close() if inspection_temp40: inspection_temp40_array = [] for tk in inspection_temp40[5:-1]: class2_data.append(float(tk)) else: class2_data = () if class2_data: cursor_temp60 = db.cursor() sqltem = "select * from temperature where tank_no user='%s' and T_41h is not null and T_50h is not null and T_60h is not null" % tank_number try: cursor_temp60.execute(sqltem) inspection_temp60 = cursor_temp60.fetchone() #print(inspection_temp60) except: inspection_temp60 = [] print("type2:41h-60h小时温度数据读取失败") cursor_temp60.close() if inspection_temp60: for tk in inspection_temp60[43:-1]: class2_data.append(float(tk)) else: class2_data =() if class2_data: #将列表转换成元组 class2_data = tuple(class2_data) cursor_insert.execute(query, class2_data) cursor_insert.close() db.commit() db.close() else: pass ethanol_rowcount = ethanol_rowcount_everymonth + ethanol_rowcount#对乙醇表中的新数据完成搜索后,将下次搜索区间的最小值改成 当前乙醇表的行数,方便下次从当前乙醇表行数开始搜索 #将关键温度信息添加到界面上,方便工作人员查看 def key_temperature_table(self):#真实温度数据(该批次对应的60个小时的温度 或者是一部分温度数据)关键温度数据:经过上边函数计算得出的温度 if temperature2 :#### 当前批次的实际温度 ####当实际值不为空才进行显示 self.ui4.textBrowser2.setText('各个时刻(Ti:发酵第i小时)对应的温度为:') self.ui4.tableWidget2.show() #实时温度值 if len(temperature2) <= 20: for i in range(1, len(temperature2)): self.ui4.tableWidget2.item(0, i - 1).setText(str("{:.2f}".format(temperature2[i - 1]))) # GI 液化 if 20 < len(temperature2) <= 40: for i in range(1, 21): self.ui4.tableWidget2.item(0, i - 1).setText(str("{:.2f}".format(temperature2[i - 1]))) # GI 液化 # self.ui.progressBar.setValue(float(str(resultonline[0][10]))) for i in range(21, len(temperature2)): self.ui4.tableWidget2.item(3, i - 21).setText(str("{:.2f}".format(temperature2[i - 1]))) # 乙酸 液化 if 40 < len(temperature2) : for i in range(1, 21): self.ui4.tableWidget2.item(0, i - 1).setText(str("{:.2f}".format(temperature2[i - 1]))) # GI 液化 # self.ui.progressBar.setValue(float(str(resultonline[0][10]))) for i in range(21, 41): self.ui4.tableWidget2.item(3, i - 21).setText(str("{:.2f}".format(temperature2[i - 1]))) # 乙酸 液化 for i in range(41, len(temperature2)): self.ui4.tableWidget2.item(6, i - 41).setText(str("{:.2f}".format(temperature2[i - 1]))) # 乙酸 液化 # if temperature: #人为计算固定的温度 # # 将真实温度信息赋值给ui4的温度表tableWidget # for i in range(1,21): # self.ui4.tableWidget2.item(1,i-1).setText(str("{:.2f}".format(temperature[i - 1]))) #GI 液化 # #self.ui.progressBar.setValue(float(str(resultonline[0][10]))) # for i in range(21,41): # self.ui4.tableWidget2.item(4,i-21).setText(str("{:.2f}".format(temperature[i - 1])))#乙酸 液化 # for i in range(41,61): # self.ui4.tableWidget2.item(7,i-41).setText(str("{:.2f}".format(temperature[i - 1])))#乙酸 液化 # 最佳发酵温度两种情况 一种计算出的平均温度 另一种是经过计算的平均温度固定值(无法计算时递补) #程序保证关键温度始终存在 if key_temp_calculate: #将关键温度信息赋值给ui4的温度表tableWidget for i in range(1,21): self.ui4.tableWidget2.item(1,i-1).setText(str("{:.2f}".format(key_temp_calculate[i - 1]))) #GI 液化 for i in range(21,41): self.ui4.tableWidget2.item(4,i-21).setText(str("{:.2f}".format(key_temp_calculate[i - 1])))#乙酸 液化 for i in range(41,61): self.ui4.tableWidget2.item(7,i-41).setText(str("{:.2f}".format(key_temp_calculate[i - 1])))#乙酸 液化 else: # 经过计算的平均温度固定值(无法计算时递补) for i in range(1,21): self.ui4.tableWidget2.item(1,i-1).setText(str("{:.2f}".format(temperature[i - 1]))) #GI 液化 #self.ui.progressBar.setValue(float(str(resultonline[0][10]))) for i in range(21,41): self.ui4.tableWidget2.item(4,i-21).setText(str("{:.2f}".format(temperature[i - 1])))#乙酸 液化 for i in range(41,61): self.ui4.tableWidget2.item(7,i-41).setText(str("{:.2f}".format(temperature[i - 1])))#乙酸 液化 #关键温度数据插入 为空的时候就不插入 if key_dot_temperature : #将关键温度信息赋值给ui4的温度表tableWidget #print(key_dot_temperature ) for i in range(1,9): self.ui4.tableWidget2.item(9,i-1).setText(str("{:.2f}".format(key_dot_temperature[i - 1]))) #GI 液化 else: self.ui4.textBrowser2.setText('无法获取当前发酵温度') self.ui4.tableWidget2.hide() self.ui4.show() #注意分类结果发生改变 应更新关键温度曲线、操作区间的值 (因为关键温度曲线和操作区间值取自不同的类别数据) # 将所有的温度绘图整合 def key_temperature(self): global hour,temperature,key_temp_calculate,key_dot_temperature db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor0h = db.cursor() sql0h = "select * from prediction0h where ID in(select max(ID) from prediction0h where tank_no='%s')"%(currentnumber)#将当前批号对应的操作区间写入变量最佳操作区间表 try: cursor0h.execute(sql0h) result_0h = cursor0h.fetchone() #print(f"result_0h: {result_0h}") except: result_0h=[] print("Error:无法获取当前批次的0h数据") cursor0h.close() with open('scaler_train_0h_classification', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_0h_classification', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_0h[5:]))#判断当前批次属于那一类数据 prob = lda_test.predict(x_test) #最佳发酵温度计算 if prob[0]==1: #从class1表中读取60小时温度数据 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) key_temp = db.cursor() key_temp_calculate = [] temperature_time = ["t83", "t84", "t85", "t86", "t87", "t88", "t89", "t90", "t91", "t92", "t93", "t94", "t95", "t96", "t97", "t98", "t99", "t100", "t101", "t102", "t103", "t104", "t105", "t106", "t107", "t108", "t109", "t110", "t111", "t112", "t113", "t114", "t115", "t116", "t117", "t118", "t119", "t120", "t121", "t122", "t123", "t124", "t125", "t126", "t127", "t128", "t129", "t130", "t131", "t132", "t133", "t134", "t135", "t136", "t137", "t138", "t139", "t140", "t141", "t142" ] for ci in temperature_time: sqltem = "select avg(%s) from Type1" % (ci) try: key_temp.execute(sqltem) key_temp_data_avg1 = key_temp.fetchall() except: key_temp_data_avg1 = [] print("Calss1关键温度平均值读取失败") key_temp_calculate.append(key_temp_data_avg1[0][0])#依次将各个字段计算的平均值 添加到列表 key_temp.close() ###计算平均温度 并将数据转化为list格式### # 计算平均温度 if key_temp_calculate: print(f"输出class1的关键温度数据: {key_temp_calculate}") temperature = [] # 转化为元组 key_temp_calculate = tuple(key_temp_calculate) self.ui.historyPlot.clear() hour = [i for i in range(1, 61)] self.curve1 = self.ui.historyPlot.plot(pen=pg.mkPen('b', width=2), name='关键温度') # 线条颜色 self.curve1.setData(hour, key_temp_calculate) listx = [11, 52, 37, 6, 40, 27, 33, 21] listy = [key_temp_calculate[10], key_temp_calculate[51], key_temp_calculate[36], key_temp_calculate[5], key_temp_calculate[39], key_temp_calculate[26], key_temp_calculate[32], key_temp_calculate[20]] key_dot_temperature = [key_temp_calculate[5],key_temp_calculate[10],key_temp_calculate[20],key_temp_calculate[26],key_temp_calculate[32],key_temp_calculate[36],key_temp_calculate[39],key_temp_calculate[51]] self.ui.historyPlot.plot().setData(listx, listy, pen=None, symbol='star', symbolBrush='b', symbolSize=20) else: # 如果不存在表或者 没有从表中计算出数据就默认绘制一下关键温度曲线 self.ui.historyPlot.clear() hour = [i for i in range(1, 61)] temperature = [31.07, 31.52, 32.04, 32.31, 32.45, 32.55, 32.68, 32.78, 32.82, 32.80, 32.73, 32.65, 32.58, 32.51, 32.45, 32.38, 32.31, 32.24, 32.17, 32.11, 32.07, 32.06, 32.04, 32.02, 32.00, 31.98, 31.95, 31.92, 31.90, 31.89, 31.89, 31.89, 31.88, 31.88, 31.86, 31.85, 31.82, 31.82, 31.79, 31.77, 31.74, 31.70, 31.67, 31.65, 31.63, 31.61, 31.59, 31.56, 31.51, 31.46, 31.41, 31.36, 31.31, 31.26, 31.20, 31.15, 31.09, 31.03, 30.97, 30.92] self.curve1 = self.ui.historyPlot.plot(pen=pg.mkPen('b', width=2), name='关键温度') # 线条颜色 self.curve1.setData(hour, temperature) listx = [11, 52, 37, 6, 40, 27, 33, 21] # 互信息查询之后的关键温度点 listy = [32.73, 31.36, 31.82, 32.55, 31.77, 31.95, 31.88, 32.07] key_dot_temperature = [32.55, 32.73, 32.07, 31.95, 31.88, 31.82, 31.77, 31.36] self.ui.historyPlot.plot().setData(listx, listy, pen=None, symbol='star', symbolBrush='b', symbolSize=20) # 表数据 print("type1默认最佳温度发酵曲线绘制成功") # listx = [11, 52, 37, 6, 40, 27, 33, 21] # listy = [32.73, 31.36, 31.82, 32.55, 31.77, 31.95, 31.88, 32.07] # key_dot_temperature = [32.55, 32.73, 32.07, 31.95, 31.88, 31.82, 31.77, 31.36] # self.ui.historyPlot.plot().setData(listx, listy, pen=None, symbol='star', symbolBrush='b', # symbolSize=20) # 表数据 print(" Calss1关键温度数据读取计算成功,成功更新关键温度曲线") self.ui.historyPlot.setLabel("top", str(datetime.datetime.now().strftime('%m')) +"月最佳发酵温度曲线已更新",size='2pt') elif prob[0]==2: #从class2表中读取检60小时温度数据并计算其均值 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) key_temp = db.cursor() key_temp_calculate=[] temperature_time=["t83","t84","t85","t86","t87","t88","t89","t90","t91","t92","t93","t94","t95","t96","t97","t98","t99","t100","t101","t102", "t103","t104","t105","t106","t107","t108","t109","t110","t111","t112","t113","t114","t115","t116","t117","t118","t119","t120","t121","t122", "t123","t124","t125","t126","t127","t128","t129","t130","t131","t132","t133","t134","t135","t136","t137","t138","t139","t140","t141","t142" ] for ci in temperature_time: sqltem = "select avg(%s) from Type2"%(ci) try: key_temp.execute(sqltem) key_temp_data_avg = key_temp.fetchall() except: key_temp_data_avg= [] print("Calss2关键温度平均值读取失败") # #如果不存在表或者 没有从表中计算出数据就默认绘制一下关键温度曲线 # self.ui.historyPlot.clear() # hour = [i for i in range (1,61)] # temperature = [31.07, 31.52, 32.04, 32.31, 32.45, 32.55, 32.68, 32.78, 32.82, 32.80, # 32.73, 32.65, 32.58, 32.51, 32.45, 32.38, 32.31, 32.24, 32.17, 32.11, # 32.07, 32.06, 32.04, 32.02, 32.00, 31.98, 31.95, 31.92, 31.90, 31.89, # 31.89, 31.89, 31.88, 31.88, 31.86, 31.85, 31.82, 31.82, 31.79, 31.77, # 31.74, 31.70, 31.67, 31.65, 31.63, 31.61, 31.59, 31.56, 31.51, 31.46, # 31.41, 31.36, 31.31, 31.26, 31.20, 31.15, 31.09, 31.03, 30.97, 30.92] # self.curve1 = self.ui.historyPlot.plot(pen=pg.mkPen('b', width=2), name='关键温度') # 线条颜色 # self.curve1.setData(hour, temperature) # listx=[11, 52, 37, 6, 40, 27, 33, 21] # listy = [key_temp_calculate[10], key_temp_calculate[51], key_temp_calculate[36], key_temp_calculate[5], key_temp_calculate[39], key_temp_calculate[26], key_temp_calculate[32], key_temp_calculate[20]] # key_dot_temperature = [key_temp_calculate[5], key_temp_calculate[10], key_temp_calculate[20], # key_temp_calculate[26], key_temp_calculate[32], key_temp_calculate[36], # key_temp_calculate[39], key_temp_calculate[51]] # self.ui.historyPlot.plot().setData(listx,listy, pen=None, symbol='star',symbolBrush='b',symbolSize=20) #表数据 # print ("绘制默认关键曲线") key_temp_calculate.append(key_temp_data_avg[0][0]) key_temp.close() #print(key_temp_calculate) #temperature = [] if key_temp_calculate: print(f"输出class2的关键温度数据: {key_temp_calculate}") key_temp_calculate=tuple(key_temp_calculate) self.ui.historyPlot.clear() hour = [i for i in range(1, 61)] self.curve1 = self.ui.historyPlot.plot(pen=pg.mkPen('b', width=2), name='关键温度') # 线条颜色 self.curve1.setData(hour, key_temp_calculate) # 标出一些温度关键点, 已经提前分析好 后面 有需要可以修改 listx = [11, 52, 37, 6, 40, 27, 33, 21] listy = [key_temp_calculate[10], key_temp_calculate[51], key_temp_calculate[36], key_temp_calculate[5], key_temp_calculate[39], key_temp_calculate[26], key_temp_calculate[32], key_temp_calculate[20]] key_dot_temperature = [key_temp_calculate[5],key_temp_calculate[10],key_temp_calculate[20],key_temp_calculate[26],key_temp_calculate[32],key_temp_calculate[36],key_temp_calculate[39],key_temp_calculate[51]] self.ui.historyPlot.plot().setData(listx, listy, pen=None, symbol='star', symbolBrush='b', symbolSize=20) else: # 如果不存在表或者 没有从表中计算出数据就默认绘制一下关键温度曲线 self.ui.historyPlot.clear() hour = [i for i in range(1, 61)] temperature = [31.07, 31.52, 32.04, 32.31, 32.45, 32.55, 32.68, 32.78, 32.82, 32.80, 32.73, 32.65, 32.58, 32.51, 32.45, 32.38, 32.31, 32.24, 32.17, 32.11, 32.07, 32.06, 32.04, 32.02, 32.00, 31.98, 31.95, 31.92, 31.90, 31.89, 31.89, 31.89, 31.88, 31.88, 31.86, 31.85, 31.82, 31.82, 31.79, 31.77, 31.74, 31.70, 31.67, 31.65, 31.63, 31.61, 31.59, 31.56, 31.51, 31.46, 31.41, 31.36, 31.31, 31.26, 31.20, 31.15, 31.09, 31.03, 30.97, 30.92] self.curve1 = self.ui.historyPlot.plot(pen=pg.mkPen('b', width=2), name='关键温度') # 线条颜色 self.curve1.setData(hour, temperature) listx = [11, 52, 37, 6, 40, 27, 33, 21] listy = [key_temp_calculate[10], key_temp_calculate[51], key_temp_calculate[36], key_temp_calculate[5], key_temp_calculate[39], key_temp_calculate[26], key_temp_calculate[32], key_temp_calculate[20]] key_dot_temperature = [key_temp_calculate[5], key_temp_calculate[10], key_temp_calculate[20], key_temp_calculate[26], key_temp_calculate[32], key_temp_calculate[36], key_temp_calculate[39], key_temp_calculate[51]] self.ui.historyPlot.plot().setData(listx, listy, pen=None, symbol='star', symbolBrush='b', symbolSize=20) # 表数据 print("type2默认最佳温度发酵曲线绘制成功") print("Calss2关键温度数据读取计算成功,成功更新关键温度曲线") self.ui.historyPlot.setLabel("top", str(datetime.datetime.now().strftime('%m')) +"月最佳发酵温度曲线已更新",size='2pt') #计算对应字段的置信区间,作为最优操作区间 def update_operation_interval(self): #handlechange已经成功读取result_0h[5:](全局变量) with open('scaler_train_0h_classification', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_0h_classification', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 x_test = scaler_test.transform(np.matrix(result_0h[5:])) prob = lda_test.predict(x_test) if prob[0]==1: # 从type1表中读取检验数据 operation_interval1_low = [] # 置信区间 operation_interval1_high = [] db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) interval1_mean_db = db.cursor() interval1_std_db = db.cursor() variable1 = [ # 按顺序对应操作区间表的变量 "x20", "x10", "x5", "x30", "x14", "x4", "x15", "x6", "x27", "x7", "x23", "x9", "x35", "x29", "x26", "x3", "x2" ] for ci in variable1: mean1 = "select avg(%s) from Type1" % (ci) # 计算均值 std1 = "select stddev_samp(%s) from Type1" % (ci) # 计算std try: interval1_mean_db.execute(mean1) interval1_std_db.execute(std1) interval1_mean = interval1_mean_db.fetchall() interval1_std = interval1_std_db.fetchall() except: interval1_mean = [] interval1_std = [] print("type1操作变量平均值、标准差读取失败") # 计算相应字段的置信区间 if interval1_mean and interval1_std: ci_low = float(interval1_mean[0][0]) - float(interval1_std[0][0]) * 1.64 # 置信水平0.9 ci_high = float(interval1_mean[0][0]) + float(interval1_std[0][0]) * 1.64 operation_interval1_low.append(ci_low) operation_interval1_high.append(ci_high) interval1_mean_db.close() interval1_std_db.close() db.close() # 赋值操作区间 #"{:.2f}".format(operation_interval1_low[0]) "{:.2f}".format(operation_interval1_high[0]) if operation_interval1_low and operation_interval1_high: self.ui.tableWidget.item(0, 0).setText( [round(operation_interval1_low[0], 2), round(operation_interval1_high[0], 2)]) # GI 液化 self.ui.tableWidget.item(0, 1).setText(str([round(operation_interval1_low[1], 2), round(operation_interval1_high[1], 2)])) # 葡萄糖 液化 self.ui.tableWidget.item(0, 2).setText(str([round(operation_interval1_low[2], 2), round(operation_interval1_high[2], 2)])) # 干物 液化 self.ui.tableWidget.item(0, 3).setText(str([round(operation_interval1_low[3], 2), round(operation_interval1_high[3], 2)])) # 葡萄糖 酒母 self.ui.tableWidget.item(0, 4).setText(str([round(operation_interval1_low[4], 2), round(operation_interval1_high[4], 2)])) # 甘油 液化 self.ui.tableWidget.item(0, 5).setText(str([round(operation_interval1_low[5], 2), round(operation_interval1_high[5], 2)])) # PH 液化 self.ui.tableWidget.item(3, 0).setText(str([round(operation_interval1_low[6], 2), round(operation_interval1_high[6], 2)])) # 乙酸 液化 self.ui.tableWidget.item(3, 1).setText(str([round(operation_interval1_low[7], 2), round(operation_interval1_high[7], 2)])) # 粘度 液化 self.ui.tableWidget.item(3, 2).setText(str([round(operation_interval1_low[8], 2), round(operation_interval1_high[8], 2)])) # DP4 酒母 self.ui.tableWidget.item(3, 3).setText(str([round(operation_interval1_low[9], 2), round(operation_interval1_high[9], 2)])) # DP4液化 self.ui.tableWidget.item(3, 4).setText(str([round(operation_interval1_low[10], 2), round(operation_interval1_high[10], 2)])) # 干物酒母 self.ui.tableWidget.item(3, 5).setText(str([round(operation_interval1_low[11], 2), round(operation_interval1_high[11], 2)])) # DP2 液化 self.ui.tableWidget.item(6, 0).setText(str([round(operation_interval1_low[12], 2), round(operation_interval1_high[12], 2)])) # 乙酸酒母 self.ui.tableWidget.item(6, 1).setText(str([round(operation_interval1_low[13], 2), round(operation_interval1_high[13], 2)])) # DP2 酒母 self.ui.tableWidget.item(6, 2).setText(str([round(operation_interval1_low[14], 2), round(operation_interval1_high[14], 2)])) # 死亡率 self.ui.tableWidget.item(6, 3).setText(str([round(operation_interval1_low[15], 2), round(operation_interval1_high[15], 2)])) # 糖化酶 self.ui.tableWidget.item(6, 4).setText(str([round(operation_interval1_low[16], 2), round(operation_interval1_high[16], 2)])) # 酒母醪 else:#置信区间计算失败,填入默认值 self.ui.tableWidget.item(0, 0).setText(str([2.5,4])) # GI 液化 self.ui.tableWidget.item(0, 1).setText(str([0.5,1])) # 葡萄糖 液化 self.ui.tableWidget.item(0, 2).setText(str([30,31.5])) # 干物 液化 self.ui.tableWidget.item(0, 3).setText(str([7,11])) # 葡萄糖 酒母 self.ui.tableWidget.item(0, 4).setText(str([0.06,0.15])) # 甘油 液化 self.ui.tableWidget.item(0, 5).setText(str([4.2,4.4])) # PH 液化 self.ui.tableWidget.item(3, 0).setText(str([0,0.03])) # 乙酸 液化 self.ui.tableWidget.item(3, 1).setText(str([110,150])) # 粘度 液化 self.ui.tableWidget.item(3, 2).setText(str([7,11])) # DP4 酒母 self.ui.tableWidget.item(3, 3).setText(str([16,20])) # DP4液化 self.ui.tableWidget.item(3, 4).setText(str([25,28])) # 干物酒母 self.ui.tableWidget.item(3, 5).setText(str([1.6,2.4])) # DP2 液化 self.ui.tableWidget.item(6, 0).setText(str([0,0.03])) # 乙酸酒母 self.ui.tableWidget.item(6, 1).setText(str([4.2,6.2])) # DP2 酒母 self.ui.tableWidget.item(6, 2).setText(str([6,10])) # 死亡率 self.ui.tableWidget.item(6, 3).setText(str([280,400])) # 糖化酶 self.ui.tableWidget.item(6, 4).setText(str([350,590])) # 酒母醪 if prob[0] == 2: # 从type2表中读取检验数据 operation_interval_low = [] # 置信区间 operation_interval_high = [] db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) interval_mean_db = db.cursor() interval_std_db = db.cursor() variable = [#按顺序对应操作区间表的变量 "x20", "x10", "x5", "x30", "x14", "x4", "x15", "x6", "x27", "x7", "x23", "x9", "x35", "x29", "x26", "x3", "x2" ] for ci in variable: mean = "select avg(%s) from Type2" % (ci)#计算均值 std = "select stddev_samp(%s) from Type2" % (ci)#计算std try: interval_mean_db.execute(mean) interval_std_db.execute(std) interval_mean = interval_mean_db.fetchall() interval_std = interval_std_db.fetchall() except: interval_mean = [] interval_std=[] print("关键温度平均值、标准差读取失败") #计算相应字段的置信区间 if interval_mean and interval_std: ci_low =float( interval_mean[0][0]) - float(interval_std[0][0])*1.64#置信水平0.9 ci_high=float( interval_mean[0][0]) + float(interval_std[0][0])*1.64 operation_interval_low.append(ci_low) operation_interval_high.append(ci_high) interval_mean_db.close() interval_std_db.close() db.close() if operation_interval_low and operation_interval_high: #赋值操作区间"{:.2f}".format(operation_interval1_low[0]) "{:.2f}".format(operation_interval1_high[0]) self.ui.tableWidget.item(0, 0).setText( str([round(operation_interval_low[0], 2), round(operation_interval_high[0], 2)])) # GI 液化 self.ui.tableWidget.item(0, 1).setText( str([round(operation_interval_low[1], 2), round(operation_interval_high[1], 2)])) # 葡萄糖 液化 self.ui.tableWidget.item(0, 2).setText( str([round(operation_interval_low[2], 2), round(operation_interval_high[2], 2)])) # 干物 液化 self.ui.tableWidget.item(0, 3).setText( str([round(operation_interval_low[3], 2), round(operation_interval_high[3], 2)])) # 葡萄糖 酒母 self.ui.tableWidget.item(0, 4).setText( str([round(operation_interval_low[4], 2), round(operation_interval_high[4], 2)])) # 甘油 液化 self.ui.tableWidget.item(0, 5).setText( str([round(operation_interval_low[5], 2), round(operation_interval_high[5], 2)])) # PH 液化 self.ui.tableWidget.item(3, 0).setText( str([round(operation_interval_low[6], 2), round(operation_interval_high[6], 2)])) # 乙酸 液化 self.ui.tableWidget.item(3, 1).setText( str([round(operation_interval_low[7], 2), round(operation_interval_high[7], 2)])) # 粘度 液化 self.ui.tableWidget.item(3, 2).setText( str([round(operation_interval_low[8], 2), round(operation_interval_high[8], 2)])) # DP4 酒母 self.ui.tableWidget.item(3, 3).setText( str([round(operation_interval_low[9], 2), round(operation_interval_high[9], 2)])) # DP4液化 self.ui.tableWidget.item(3, 4).setText( str([round(operation_interval_low[10], 2), round(operation_interval_high[10], 2)])) # 干物酒母 self.ui.tableWidget.item(3, 5).setText( str([round(operation_interval_low[11], 2), round(operation_interval_high[11], 2)])) # DP2 液化 self.ui.tableWidget.item(6, 0).setText( str([round(operation_interval_low[12], 2), round(operation_interval_high[12], 2)])) # 乙酸酒母 self.ui.tableWidget.item(6, 1).setText( str([round(operation_interval_low[13], 2), round(operation_interval_high[13], 2)])) # DP2 酒母 self.ui.tableWidget.item(6, 2).setText( str([round(operation_interval_low[14], 2), round(operation_interval_high[14], 2)])) # 死亡率 self.ui.tableWidget.item(6, 3).setText( str([round(operation_interval_low[15], 2), round(operation_interval_high[15], 2)])) # 糖化酶 self.ui.tableWidget.item(6, 4).setText( str([round(operation_interval_low[16], 2), round(operation_interval_high[16], 2)])) # 酒母醪 else: self.ui.tableWidget.item(0, 0).setText(str([2.5,4])) # GI 液化 self.ui.tableWidget.item(0, 1).setText(str([0.5,1])) # 葡萄糖 液化 self.ui.tableWidget.item(0, 2).setText(str([30,31.5])) # 干物 液化 self.ui.tableWidget.item(0, 3).setText(str([7,11])) # 葡萄糖 酒母 self.ui.tableWidget.item(0, 4).setText(str([0.06,0.15])) # 甘油 液化 self.ui.tableWidget.item(0, 5).setText(str([4.2,4.4])) # PH 液化 self.ui.tableWidget.item(3, 0).setText(str([0,0.03])) # 乙酸 液化 self.ui.tableWidget.item(3, 1).setText(str([110,150])) # 粘度 液化 self.ui.tableWidget.item(3, 2).setText(str([7,11])) # DP4 酒母 self.ui.tableWidget.item(3, 3).setText(str([16,20])) # DP4液化 self.ui.tableWidget.item(3, 4).setText(str([25,28])) # 干物酒母 self.ui.tableWidget.item(3, 5).setText(str([1.6,2.4])) # DP2 液化 self.ui.tableWidget.item(6, 0).setText(str([0,0.03])) # 乙酸酒母 self.ui.tableWidget.item(6, 1).setText(str([4.2,6.2])) # DP2 酒母 self.ui.tableWidget.item(6, 2).setText(str([6,10])) # 死亡率 self.ui.tableWidget.item(6, 3).setText(str([280,400])) # 糖化酶 self.ui.tableWidget.item(6, 4).setText(str([350,590])) # 酒母醪 def mouseover(self, pos): # 温度曲线坐标显示 # 参数pos是像素坐标,需要转化为刻度坐标 act_pos =self.curve1.mapFromScene(pos) if type(act_pos) != PySide2.QtCore.QPointF: return self.ui.label_74.setText(str(round(act_pos.x(),3))) self.ui.label_76.setText(str(round(act_pos.y(),3))) def mouseover2(self, pos): # 乙醇浓度曲线坐标显示 # 参数pos是像素坐标,需要转化为刻度坐标 act_pos = self.curveA.mapFromScene(pos) if type(act_pos) != PySide2.QtCore.QPointF: return self.ui2.label_74.setText(str(round(act_pos.x(), 3))) self.ui2.label_76.setText(str(round(act_pos.y(), 3))) # self.ui2.label_76.setText(str(round(act_pos.y2(),3))) def table_initial(self): #操作区间初始化 for i in range(0, 6): item = QTableWidgetItem() item.setText('') item.setTextAlignment(Qt.AlignHCenter) self.ui.tableWidget.setItem(0, i, item) item1 = QTableWidgetItem() item1.setText('') item1.setTextAlignment(Qt.AlignHCenter) self.ui.tableWidget.setItem(3, i, item1) if i < 5: item2 = QTableWidgetItem() item2.setText('') item2.setTextAlignment(Qt.AlignHCenter) self.ui.tableWidget.setItem(6, i, item2) #操作区间对应的实际值初始化 for i in range(0, 6): item = QTableWidgetItem() item.setText('') item.setTextAlignment(Qt.AlignHCenter) self.ui.tableWidget.setItem(1, i, item) item1 = QTableWidgetItem() item1.setText('') item1.setTextAlignment(Qt.AlignHCenter) self.ui.tableWidget.setItem(4, i, item1) if i < 5: item2 = QTableWidgetItem() item2.setText('') item2.setTextAlignment(Qt.AlignHCenter) self.ui.tableWidget.setItem(7, i, item2) #suggestion对应的建议值初始化 for i in range(0, 6): item = QTableWidgetItem() item.setText('') item.setTextAlignment(Qt.AlignHCenter) self.ui3.tableWidget.setItem(1, i, item) item1 = QTableWidgetItem() item1.setText('') item1.setTextAlignment(Qt.AlignHCenter) self.ui3.tableWidget.setItem(4, i, item1) if i < 5: item2 = QTableWidgetItem() item2.setText('') item2.setTextAlignment(Qt.AlignHCenter) self.ui3.tableWidget.setItem(7, i, item2) # suggestion对应的实际值初始化 for i in range(0, 6): item = QTableWidgetItem() item.setText('') item.setTextAlignment(Qt.AlignHCenter) self.ui3.tableWidget.setItem(0, i, item) item1 = QTableWidgetItem() item1.setText('') item1.setTextAlignment(Qt.AlignHCenter) self.ui3.tableWidget.setItem(3, i, item1) if i < 5: item2 = QTableWidgetItem() item2.setText('') item2.setTextAlignment(Qt.AlignHCenter) self.ui3.tableWidget.setItem(6, i, item2) #关键温度表赋值初始化 for i in range(0, 20): #实时温度的初始化 item = QTableWidgetItem() item.setText('') item.setTextAlignment(Qt.AlignHCenter) self.ui4.tableWidget2.setItem(0, i, item) item1 = QTableWidgetItem() item1.setText('') item1.setTextAlignment(Qt.AlignHCenter) self.ui4.tableWidget2.setItem(3, i, item1) item2 = QTableWidgetItem() item2.setText('') item2.setTextAlignment(Qt.AlignHCenter) self.ui4.tableWidget2.setItem(6, i, item2) #最佳发酵温度的初始化 item3 = QTableWidgetItem() item3.setText('') item3.setTextAlignment(Qt.AlignHCenter) self.ui4.tableWidget2.setItem(1, i, item3) item4 = QTableWidgetItem() item4.setText('') item4.setTextAlignment(Qt.AlignHCenter) self.ui4.tableWidget2.setItem(4, i, item4) item5 = QTableWidgetItem() item5.setText('') item5.setTextAlignment(Qt.AlignHCenter) self.ui4.tableWidget2.setItem(7, i, item5) # 关键温度表关键温度数据的初始化 for i in range(0, 9): item6 = QTableWidgetItem() item6.setText('') item6.setTextAlignment(Qt.AlignHCenter) self.ui4.tableWidget2.setItem(9, i, item6) #当前状态不佳时,提出调整建议。包括实际值和建议值两部分 def suggestion(self): global res_0h, res_8h, res_24h, res_40h, result_0h, result_24h, a0, a8, a24, a40 if res_0h == 1:#当前发酵阶段:初始时刻 各操作变量实际值 self.ui3.tableWidget.item(0,0).setText(str(result_0h[21])) #GI 液化 self.ui3.tableWidget.item(0,1).setText(str(result_0h[11])) # 葡萄糖 液化 self.ui3.tableWidget.item(0,2).setText(str(result_0h[6])) # 干物 液化 self.ui3.tableWidget.item(0,3).setText(str(result_0h[31]))# 葡萄糖 酒母 self.ui3.tableWidget.item(0,4).setText(str(result_0h[15]))# 甘油 液化 self.ui3.tableWidget.item(0,5).setText(str(result_0h[5]))#PH 液化 self.ui3.tableWidget.item(3,0).setText(str(result_0h[16]))#乙酸 液化 self.ui3.tableWidget.item(3,1).setText(str(result_0h[7]))# 粘度 液化 self.ui3.tableWidget.item(3,2).setText(str(result_0h[28]))# DP4 酒母 self.ui3.tableWidget.item(3,3).setText(str(result_0h[8]))# DP4液化 self.ui3.tableWidget.item(3,4).setText(str(result_0h[24]))#干物酒母 self.ui3.tableWidget.item(3,5).setText(str(result_0h[10]))# DP2 液化 self.ui3.tableWidget.item(6,0).setText(str(result_0h[36]))# 乙酸酒母 self.ui3.tableWidget.item(6,1).setText(str(result_0h[30]))#DP2 酒母 self.ui3.tableWidget.item(6,2).setText(str(result_0h[27]))# 死亡率 if res_24h == 1:#当前发酵阶段:24h self.ui3.tableWidget.item(6,4).setText(str(result_24h[6]))#酒母 self.ui3.tableWidget.item(6,3).setText(str(result_24h[7]))# 糖化酶 ###!!!之所以只有res_0h和res_24h这两个阶段的判断,是因为建议值只在初始时刻进行调整,进入发酵阶段以后便无法进行调控 if res_40h == 1: if float(str(a40)) > 0.7: self.ui3.textBrowser.setText('不需要调整') self.ui3.tableWidget.hide() else: self.ui3.tableWidget.show() data = pd.read_csv('40h.csv',engine="python") x_train = data.values y_train = np.arange(1, 27) x_test = np.asarray(result_40h[5:], dtype=np.float32) x_test = x_test.reshape(1, -1) # 定义模型 knn = KNeighborsClassifier(n_neighbors=5) knn.fit(x_train, y_train) # 预测 y_pred_on_train = knn.predict(x_test)#预测出一个最近邻优值,赋给建议值 self.ui3.textBrowser.setText('各指标(按重要程度排序)调整为:') result_zuiyou40h = x_train[y_pred_on_train[0]-1] self.ui3.tableWidget.item(1,0).setText(str(result_zuiyou40h[19])) #GI 液化 self.ui3.tableWidget.item(1,1).setText(str(result_zuiyou40h[9])) # 葡萄糖 液化 self.ui3.tableWidget.item(1,2).setText(str(result_zuiyou40h[4])) # 干物 液化 self.ui3.tableWidget.item(1,3).setText(str(result_zuiyou40h[29]))# 葡萄糖 酒母 self.ui3.tableWidget.item(1,4).setText(str(result_zuiyou40h[13]))# 甘油 液化 self.ui3.tableWidget.item(1,5).setText(str(result_zuiyou40h[3]))#PH 液化 self.ui3.tableWidget.item(4,0).setText(str(result_zuiyou40h[14]))#乙酸 液化 self.ui3.tableWidget.item(4,1).setText(str(result_zuiyou40h[5]))# 粘度 液化 self.ui3.tableWidget.item(4,2).setText(str(result_zuiyou40h[16]))# DP4 酒母 self.ui3.tableWidget.item(4,3).setText(str(result_zuiyou40h[6]))# DP4液化 self.ui3.tableWidget.item(4,4).setText(str(result_zuiyou40h[22]))#干物酒母 self.ui3.tableWidget.item(4,5).setText(str(result_zuiyou40h[8]))# DP2 液化 self.ui3.tableWidget.item(7,0).setText(str(result_zuiyou40h[34]))# 乙酸酒母 self.ui3.tableWidget.item(7,1).setText(str(result_zuiyou40h[28]))#DP2 酒母 self.ui3.tableWidget.item(7,2).setText(str(result_zuiyou40h[25]))# 死亡率 self.ui3.tableWidget.item(7,4).setText(str(result_zuiyou40h[1]))#酒母 self.ui3.tableWidget.item(7,3).setText(str(result_zuiyou40h[2]))# 糖化酶 self.ui3.show() elif res_24h == 1: if float(str(a24)) > 0.7: self.ui3.textBrowser.setText('不需要调整') self.ui3.tableWidget.hide() else: self.ui3.tableWidget.show() data = pd.read_csv('24h.csv',engine="python") x_train = data.values y_train = np.arange(1, 27) x_test=np.asarray(result_24h[5:], dtype=np.float32) x_test = x_test.reshape(1, -1) # 定义模型 knn = KNeighborsClassifier(n_neighbors=5) knn.fit(x_train, y_train) # 预测 y_pred_on_train = knn.predict(x_test) self.ui3.textBrowser.setText('各指标(按重要程度排序)调整为:') result_zuiyou24h = x_train[y_pred_on_train[0]-1]#判断与当前样本最相似的一个最优样本 self.ui3.tableWidget.item(1,0).setText(str(result_zuiyou24h[19])) #GI 液化 self.ui3.tableWidget.item(1,1).setText(str(result_zuiyou24h[9])) # 葡萄糖 液化 self.ui3.tableWidget.item(1,2).setText(str(result_zuiyou24h[4])) # 干物 液化 self.ui3.tableWidget.item(1,3).setText(str(result_zuiyou24h[29]))# 葡萄糖 酒母 self.ui3.tableWidget.item(1,4).setText(str(result_zuiyou24h[13]))# 甘油 液化 self.ui3.tableWidget.item(1,5).setText(str(result_zuiyou24h[3]))#PH 液化 self.ui3.tableWidget.item(4,0).setText(str(result_zuiyou24h[14]))#乙酸 液化 self.ui3.tableWidget.item(4,1).setText(str(result_zuiyou24h[5]))# 粘度 液化 self.ui3.tableWidget.item(4,2).setText(str(result_zuiyou24h[26]))# DP4 酒母 self.ui3.tableWidget.item(4,3).setText(str(result_zuiyou24h[6]))# DP4液化 self.ui3.tableWidget.item(4,4).setText(str(result_zuiyou24h[22]))#干物酒母 self.ui3.tableWidget.item(4,5).setText(str(result_zuiyou24h[8]))# DP2 液化 self.ui3.tableWidget.item(7,0).setText(str(result_zuiyou24h[34]))# 乙酸酒母 self.ui3.tableWidget.item(7,1).setText(str(result_zuiyou24h[28]))#DP2 酒母 self.ui3.tableWidget.item(7,2).setText(str(result_zuiyou24h[25]))# 死亡率 self.ui3.tableWidget.item(7,4).setText(str(result_zuiyou24h[1]))#酒母 self.ui3.tableWidget.item(7,3).setText(str(result_zuiyou24h[2]))# 糖化酶 self.ui3.show() elif res_8h == 1: if float(str(a8)) > 0.7: self.ui3.textBrowser.setText('不需要调整') self.ui3.tableWidget.hide() else: self.ui3.tableWidget.show() data = pd.read_csv('8h.csv',engine="python") x_train = data.values y_train = np.arange(1, 27) x_test = np.asarray(result_8h[5:], dtype=np.float32) x_test = x_test.reshape(1, -1) # 定义模型 knn = KNeighborsClassifier(n_neighbors=5) knn.fit(x_train, y_train) # 预测 y_pred_on_train = knn.predict(x_test) self.ui3.textBrowser.setText('各指标(按重要程度排序)调整为:') result_zuiyou8h = x_train[y_pred_on_train[0]-1] self.ui3.tableWidget.item(1,0).setText(str(result_zuiyou8h[16])) #GI 液化 self.ui3.tableWidget.item(1,1).setText(str(result_zuiyou8h[6])) # 葡萄糖 液化 self.ui3.tableWidget.item(1,2).setText(str(result_zuiyou8h[1])) # 干物 液化 self.ui3.tableWidget.item(1,3).setText(str(result_zuiyou8h[26]))# 葡萄糖 酒母 self.ui3.tableWidget.item(1,4).setText(str(result_zuiyou8h[10]))# 甘油 液化 self.ui3.tableWidget.item(1,5).setText(str(result_zuiyou8h[0]))#PH 液化 self.ui3.tableWidget.item(4,0).setText(str(result_zuiyou8h[11]))#乙酸 液化 self.ui3.tableWidget.item(4,1).setText(str(result_zuiyou8h[2]))# 粘度 液化 self.ui3.tableWidget.item(4,2).setText(str(result_zuiyou8h[23]))# DP4 酒母 self.ui3.tableWidget.item(4,3).setText(str(result_zuiyou8h[3]))# DP4液化 self.ui3.tableWidget.item(4,4).setText(str(result_zuiyou8h[19]))#干物酒母 self.ui3.tableWidget.item(4,5).setText(str(result_zuiyou8h[5]))# DP2 液化 self.ui3.tableWidget.item(7,0).setText(str(result_zuiyou8h[31]))# 乙酸酒母 self.ui3.tableWidget.item(7,1).setText(str(result_zuiyou8h[25]))#DP2 酒母 self.ui3.tableWidget.item(7,2).setText(str(result_zuiyou8h[22]))# 死亡率 self.ui3.show() elif res_0h == 1: if float(str(a0)) > 0.7: self.ui3.textBrowser.setText('不需要调整') self.ui3.tableWidget.hide() else: self.ui3.tableWidget.show() data = pd.read_csv('0h.csv',engine="python") x_train = data.values y_train = np.arange(1, 27) x_test=np.asarray(result_0h[5:], dtype=np.float32) x_test = x_test.reshape(1, -1) # 定义模型 knn = KNeighborsClassifier(n_neighbors=5) knn.fit(x_train, y_train) # 预测 y_pred_on_train = knn.predict(x_test) self.ui3.textBrowser.setText('各指标(按重要程度排序)调整为:') result_zuiyou0h = x_train[y_pred_on_train[0]-1] self.ui3.tableWidget.item(1,0).setText(str(result_zuiyou0h[16])) #GI 液化 self.ui3.tableWidget.item(1,1).setText(str(result_zuiyou0h[6])) # 葡萄糖 液化 self.ui3.tableWidget.item(1,2).setText(str(result_zuiyou0h[1])) # 干物 液化 self.ui3.tableWidget.item(1,3).setText(str(result_zuiyou0h[26]))# 葡萄糖 酒母 self.ui3.tableWidget.item(1,4).setText(str(result_zuiyou0h[10]))# 甘油 液化 self.ui3.tableWidget.item(1,5).setText(str(result_zuiyou0h[0]))#PH 液化 self.ui3.tableWidget.item(4,0).setText(str(result_zuiyou0h[11]))#乙酸 液化 self.ui3.tableWidget.item(4,1).setText(str(result_zuiyou0h[2]))# 粘度 液化 self.ui3.tableWidget.item(4,2).setText(str(result_zuiyou0h[23]))# DP4 酒母 self.ui3.tableWidget.item(4,3).setText(str(result_zuiyou0h[3]))# DP4液化 self.ui3.tableWidget.item(4,4).setText(str(result_zuiyou0h[19]))#干物酒母 self.ui3.tableWidget.item(4,5).setText(str(result_zuiyou0h[5]))# DP2 液化 self.ui3.tableWidget.item(7,0).setText(str(result_zuiyou0h[31]))# 乙酸酒母 self.ui3.tableWidget.item(7,1).setText(str(result_zuiyou0h[25]))#DP2 酒母 self.ui3.tableWidget.item(7,2).setText(str(result_zuiyou0h[22]))# 死亡率 self.ui3.show() elif res_0h == 0 and res_8h == 0 and res_24h == 0 and res_40h == 0: self.ui3.hide() # 将ui界面放置在屏幕正中 def center(self): qr=self.ui.frameGeometry() cp=QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.ui.move(qr.topLeft()) # 读取历史预测值 def readhistory_prediction(self): self.ui.tableWidget_2.clear() #设置列名称 column_name = [ '时间', '罐次', '罐名', '乙醇浓度', ] self.ui.tableWidget_2.setHorizontalHeaderLabels(column_name) startDate = self.ui.dateEdit.date().toString(Qt.ISODate) endDate = self.ui.dateEdit_2.date().toString(Qt.ISODate) b = datetime.datetime.strptime(str(startDate), "%Y-%m-%d") c = datetime.datetime.strptime(str(endDate), "%Y-%m-%d") db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cur = db.cursor() try: sql = "select * from prediction40h where time between %s and %s" # 将数据从数据库中拿出来 cur.execute(sql,[b,c]) #赋值的方法较安全 total = cur.fetchall() except: print("无法获取发酵40h出罐乙醇浓度预测值!") total = [] if total: self.row = cur.rowcount # 取得记录个数,用于设置表格的行数 self.ui.tableWidget_2.setRowCount(self.row) #将tablewidget实体化之后进行赋值 for i in range(self.row): for j in range(0,4): item = QTableWidgetItem() item.setText(str(total[i][j+1])) item.setTextAlignment(Qt.AlignHCenter) stock.ui.tableWidget_2.setItem(i,j,item) cur.close() db.close() def readhistory_true(self): self.ui.tableWidget_2.clear() #设置列名称 column_name = [ '时间', '罐次', '罐名', '乙醇浓度', ] self.ui.tableWidget_2.setHorizontalHeaderLabels(column_name) startDate = self.ui.dateEdit.date().toString(Qt.ISODate) endDate = self.ui.dateEdit_2.date().toString(Qt.ISODate) b = datetime.datetime.strptime(str(startDate), "%Y-%m-%d") c = datetime.datetime.strptime(str(endDate), "%Y-%m-%d") db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) curtrue = db.cursor() try: sqltrue = "select * from ethanol where 时间 between %s and %s" # 将数据从数据库中拿出来 curtrue.execute(sqltrue,[b,c]) #这样传比较安全 totaltrue = curtrue.fetchall() except: print("无法获取历史乙醇浓度真实值!") totaltrue = [] if totaltrue: #print (total) self.row = curtrue.rowcount # 取得记录个数,用于设置表格的行数 #self.ui.tableWidget_2.setColumnCount(self.vol) self.ui.tableWidget_2.setRowCount(self.row) #将tablewidget实体化之后进行赋值 for i in range(self.row): for j in range(0,4): item = QTableWidgetItem() item.setText(str(totaltrue[i][j+2])) item.setTextAlignment(Qt.AlignHCenter) self.ui.tableWidget_2.setItem(i,j,item) curtrue.close() db.close() def readhistorycurve(self): startDate = self.ui.dateEdit.date().toString(Qt.ISODate) endDate = self.ui.dateEdit_2.date().toString(Qt.ISODate) b = datetime.datetime.strptime(str(startDate), "%Y-%m-%d") c = datetime.datetime.strptime(str(endDate), "%Y-%m-%d") self.ui2.historyPlot.clear() # 乙醇体积比曲线设置 self.ui2.historyPlot.setBackground((250, 250, 250)) self.ui2.historyPlot.showGrid(x=True, y=True) self.ui2.historyPlot.setLabel("left", "乙醇体积比(%)", size='4pt') self.ui2.historyPlot.setLabel("bottom", "时间", size='4pt') # 设置Y轴 刻度 范围 self.ui2.historyPlot.setYRange(min=13, max=17) # 最大值 # self.ui2.historyPlot.setXRange(min=1, max=55) # 最大值时间范围 self.ui2.historyPlot.addLegend() self.curveA = self.ui2.historyPlot.plot(pen=pg.mkPen('b', width=2), name='预测值') # 线条颜色 self.curveB = self.ui2.historyPlot.plot(pen=pg.mkPen('m', width=2), name='真实值') # 线条颜色 ####确定x轴时间范围 time#### db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) curcurve = db.cursor() sqlcurve="select time from prediction40h where time between %s and %s" # 将数据从数据库中拿出来 try: curcurve.execute(sqlcurve, [b, c]) #这样传比较安全 time1 = curcurve.fetchall() except: time1 = [] print("curve读取x数据失败") time_list = list(chain.from_iterable(time1)) dc=[] if time_list: for i in range(curcurve.rowcount): a = int(time_list[i].strftime('%Y%m%d'))/10000#强制转换为数字方便显示在x轴 dc.append(a) curcurve.close() ####确定y轴预测值范围#### cury = db.cursor() sqly="select prediction from prediction40h where time between %s and %s" try : cury.execute(sqly,[b,c]) prediction = cury.fetchall() except: prediction = [] print("curve读取预测数据失败") prediction_list = list(chain.from_iterable(prediction)) cury.close() ####确定y轴实际值范围#### curp = db.cursor() sqltp = "select 时间 from ethanol where 时间 between %s and %s" try : curp.execute(sqltp, [b, c]) time2 = curp.fetchall() except: time2 = [] print("curve读取实际数据失败") time_list2 = list(chain.from_iterable(time2)) if time_list2: dc2=[] for i in range(curp.rowcount): a2 = int(time_list2[i].strftime('%Y%m%d'))/10000#强制转换为数字方便显示在x轴 dc2.append(a2) sqlp="select 乙醇出罐浓度 from ethanol where 时间 between %s and %s" try : curp.execute(sqlp, [b, c]) #这样传比较安全 True_value = curp.fetchall() except: True_value= [] print("curve读取预测数据失败") True_value_list = list(chain.from_iterable(True_value)) curp.close() ####绘制图形#### if True_value_list: for i in range(curp.rowcount): if True_value_list[i] is not None: True_value_list[i] = float(True_value_list[i]) else: True_value_list[i] = 0 self.curveB.setData(dc2, True_value_list) self.curveA.setData(dc, prediction_list) self.curveA.scene().sigMouseMoved.connect(self.mouseover2) #显示图像 self.ui2.show() db.close() def read_onlinedata(self): global number_batch,number_can db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursorread_onlinedata = db.cursor() # 从温度表读取当前有哪些批次编号 sqlread_onlinedata="select * from prediction0h order by ID desc limit %s" %(20)#倒序读取20个批次 try : cursorread_onlinedata.execute(sqlread_onlinedata) result = cursorread_onlinedata.fetchall() except: result = [] print ("Error: unable to fecth temperture") if len(result) < 20:#小于20个批次不读取 pass else: for i in range (20): #更新序号 只读取最近20个批次的数据 number_batch[i] = result[i][2]#批号 number_can[number_batch[i]] = result[i][3] #罐的名字 number_batch_str=[str(i) for i in number_batch] print(f"最近20个批次: {number_batch_str}") self.ui.comboBox.clear() self.ui.comboBox.insertItems(0, number_batch_str) self.ui.label_126.setText(str(number_can[number_batch[0]]))#将最新的批次对应罐号显示出来 cursorread_onlinedata.close() # 首先读取实时数据赋值 time_now = datetime.datetime.now() year_now = time_now.year month_now = time_now.month table_name = 'c' + str(year_now) + str(month_now) + '01' #print(table_name) cursoronline = db.cursor() sqlonline="select * from %s order by ID desc limit 1"%(table_name) try : cursoronline.execute(sqlonline) resultonline = cursoronline.fetchall() except: resultonline = None print ("Error: unable to fecth onlinedata") cursoronline.close() db.close() if resultonline: self.ui.label_5.setText(str(resultonline[0][7]))#罐A如果result没有读到值,就会提示在赋值之前就先引用local variable 'resultonline' referenced before assignment self.ui.label_7.setText(str(resultonline[0][11])) if float(str(resultonline[0][11]))>100: self.ui.progressBar.setValue(100) elif float(str(resultonline[0][11]))<0: self.ui.progressBar.setValue(0) else: self.ui.progressBar.setValue(float(str(resultonline[0][11]))) self.ui.label_13.setText(str(resultonline[0][8]))#罐B self.ui.label_16.setText(str(resultonline[0][12])) if float(str(resultonline[0][12]))>100: self.ui.progressBar_2.setValue(100) elif float(str(resultonline[0][12]))<0: self.ui.progressBar_2.setValue(0) else: self.ui.progressBar_2.setValue(float(str(resultonline[0][12]))) self.ui.label_22.setText(str(resultonline[0][9]))#罐c self.ui.label_25.setText(str(resultonline[0][13])) if float(str(resultonline[0][13]))>100: self.ui.progressBar_3.setValue(100) elif float(str(resultonline[0][13]))<0: self.ui.progressBar_3.setValue(0) else: self.ui.progressBar_3.setValue(float(str(resultonline[0][13]))) self.ui.label_31.setText(str(resultonline[0][10]))#罐D self.ui.label_34.setText(str(resultonline[0][14])) if float(str(resultonline[0][14]))>100: self.ui.progressBar_4.setValue(100) elif float(str(resultonline[0][14]))<0: self.ui.progressBar_4.setValue(0) else: self.ui.progressBar_4.setValue(float(str(resultonline[0][14]))) self.ui.label_38.setText(str(resultonline[0][19]))#罐E self.ui.label_63.setText(str(resultonline[0][23])) if float(str(resultonline[0][23]))>100: self.ui.progressBar_5.setValue(100) elif float(str(resultonline[0][23]))<0: self.ui.progressBar_5.setValue(0) else: self.ui.progressBar_5.setValue(float(str(resultonline[0][23]))) self.ui.label_59.setText(str(resultonline[0][20]))#罐F self.ui.label_44.setText(str(resultonline[0][24])) if float(str(resultonline[0][24]))>100: self.ui.progressBar_7.setValue(100) elif float(str(resultonline[0][24]))<0: self.ui.progressBar_7.setValue(0) else: self.ui.progressBar_7.setValue(float(str(resultonline[0][24]))) self.ui.label_60.setText(str(resultonline[0][21]))#罐G self.ui.label_51.setText(str(resultonline[0][25])) if float(str(resultonline[0][25]))>100: self.ui.progressBar_6.setValue(100) elif float(str(resultonline[0][25]))<0: self.ui.progressBar_6.setValue(0) else: self.ui.progressBar_6.setValue(float(str(resultonline[0][25]))) self.ui.label_56.setText(str(resultonline[0][22]))#罐H self.ui.label_55.setText(str(resultonline[0][26])) if float(str(resultonline[0][26]))>100: self.ui.progressBar_8.setValue(100) elif float(str(resultonline[0][26]))<0: self.ui.progressBar_8.setValue(0) else: self.ui.progressBar_8.setValue(float(str(resultonline[0][26]))) def read_temp(self):#通过温度表长度的变化来判断是否需要更新温度曲线 global last_len,currentnumber,temperature2 currentnumber = self.ui.comboBox.currentText() # 得到的是字符串 当前界面显示的批次号 print(currentnumber) if len(currentnumber) >= 1: # 选择框中有编号以后再执行 # 更新温度曲线: db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursortemnew = db.cursor() sqltemnew = "select * from temperature where ID in(select max(ID) from temperature where tank_no=%s)" % \ (currentnumber) try: cursortemnew.execute(sqltemnew) result_tempnew = cursortemnew.fetchone() except: result_tempnew = None print("read_temp读取温度数据失败") cursortemnew.close() db.close() # temperature2 = [] hour2 = [] hour_count = 1 if result_tempnew is not None: for temp in result_tempnew[4:]: # 根据temperature2的长度来判断是否有数据更新了 if temp is not None: temperature2.append(temp) hour2.append(hour_count) hour_count += 1 # len_nonzero=len(temperature2)-temperature2.count(0) len_nonzero = len(temperature2) if len_nonzero == 0: last_len = len_nonzero pass elif len_nonzero > last_len: #print("数据更新") last_len = len_nonzero self.ui.historyPlot.clear() #此处因为将温度曲线更新了,所以需要重新调用一下关键温度曲线 直接更新一下图形就可以 self.key_temperature()#虽然关键温度曲线经常更新,但关键温度对应的温度表缺不更新 #绘制真实温度曲线 self.ui.curve2 = self.ui.historyPlot.plot(pen=pg.mkPen('r', width=2), name='真实温度') # 线条颜色 self.ui.curve2.setData(hour2, temperature2) ##罐次选择改变后,执行该函数,主要是更新温度曲线,以及成分表 def handleSelectionChange(self): global res_0h, res_8h, res_24h, res_40h, a0, a8, a24, a40, result_0h, result_8h, result_24h, result_40h, temperature2 currentnumber = self.ui.comboBox.currentText() #当前选中批号 if currentnumber: a = number_can[int(currentnumber)] self.ui.label_126.setText(a) # 发酵罐号码 ########更新温度曲线########### #从温度表读取数据 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cu = db.cursor() sqltem = "select * from temperature where ID in(select max(ID) from temperature where tank_no=%s)"%(currentnumber) try: cu.execute(sqltem) result_temp = cu.fetchone() except: result_temp = None print ("handle读取温度数据失败") cu.close() temperature2 = [] hour2 = [] hour_count = 1 if result_temp is not None: for temp in result_temp[4:]: #根据temperature2的长度来判断是否有数据更新了 if temp is not None: temperature2.append(temp) hour2.append(hour_count) hour_count += 1 self.ui.historyPlot.clear() #直接调用一下关键温度函数 self.key_temperature() self.ui.curve2=self.ui.historyPlot.plot( pen=pg.mkPen('r', width=2),name='实时温度') # 线条颜色 self.ui.curve2.setData(hour2, temperature2) ############查询初始成分分析填入到表格中-对应表格prediction0h################## cursor0h = db.cursor() sql0h = "select * from prediction0h where ID in(select max(ID) from prediction0h where tank_no=%s)"%(currentnumber)#将当前批号对应的操作区间写入变量最佳操作区间表 try: res_0h = cursor0h.execute(sql0h) if res_0h == 1: # 表示预测表prediction0h存在该罐次编号 result_0h = cursor0h.fetchone() print(f"result_0h: {result_0h}") # 先更新表格数据 self.ui.tableWidget.item(1,0).setText(str(result_0h[21])) #GI 液化 self.ui.tableWidget.item(1,1).setText(str(result_0h[11])) # 葡萄糖 液化 self.ui.tableWidget.item(1,2).setText(str(result_0h[6])) # 干物 液化 self.ui.tableWidget.item(1,3).setText(str(result_0h[31]))# 葡萄糖 酒母 self.ui.tableWidget.item(1,4).setText(str(result_0h[15]))# 甘油 液化 self.ui.tableWidget.item(1,5).setText(str(result_0h[5]))#PH 液化 self.ui.tableWidget.item(4,0).setText(str(result_0h[16]))#乙酸 液化 self.ui.tableWidget.item(4,1).setText(str(result_0h[7]))# 粘度 液化 self.ui.tableWidget.item(4,2).setText(str(result_0h[28]))# DP4 酒母 self.ui.tableWidget.item(4,3).setText(str(result_0h[8]))# DP4液化 self.ui.tableWidget.item(4,4).setText(str(result_0h[24]))#干物酒母 self.ui.tableWidget.item(4,5).setText(str(result_0h[10]))# DP2 液化 self.ui.tableWidget.item(7,0).setText(str(result_0h[36]))# 乙酸酒母 self.ui.tableWidget.item(7,1).setText(str(result_0h[30]))#DP2 酒母 self.ui.tableWidget.item(7,2).setText(str(result_0h[27]))# 死亡率 else: # 如果0h预测数据也不存在,先重置表格数据 print("0h预测数据不存在,先重置表格数据") self.ui.tableWidget.item(1, 0).setText(str('')) # GI 液化 self.ui.tableWidget.item(1, 1).setText(str('')) # 葡萄糖 液化 self.ui.tableWidget.item(1, 2).setText(str('')) # 干物 液化 self.ui.tableWidget.item(1, 3).setText(str('')) # 葡萄糖 酒母 self.ui.tableWidget.item(1, 4).setText(str('')) # 甘油 液化 self.ui.tableWidget.item(1, 5).setText(str('')) # PH 液化 self.ui.tableWidget.item(4, 0).setText(str('')) # 乙酸 液化 self.ui.tableWidget.item(4, 1).setText(str('')) # 粘度 液化 self.ui.tableWidget.item(4, 2).setText(str('')) # DP4 酒母 self.ui.tableWidget.item(4, 3).setText(str('')) # DP4液化 self.ui.tableWidget.item(4, 4).setText(str('')) # 干物酒母 self.ui.tableWidget.item(4, 5).setText(str('')) # DP2 液化 self.ui.tableWidget.item(7, 0).setText(str('')) # 乙酸酒母 self.ui.tableWidget.item(7, 1).setText(str('')) # DP2 酒母 self.ui.tableWidget.item(7, 2).setText(str('')) # 死亡率 except: res_0h = 0 print("fail fecth 0h data") # 关闭数据库连接 cursor0h.close() cursor8h = db.cursor() #sql="select * from 8h where number=%s"%(currentnumber) sql8h = "select * from prediction8h where ID in(select max(ID) from prediction8h where tank_no=%s)"%(currentnumber) try: res_8h = cursor8h.execute(sql8h) if res_8h == 1: #等于1 表示存在该编号 result_8h = cursor8h.fetchone() # 由于0h匹配数据并不一定准确,当8h预测数据存在时,应重新更新相关数据 self.ui.tableWidget.item(1, 0).setText(str(result_8h[21])) # GI 液化 self.ui.tableWidget.item(1, 1).setText(str(result_8h[11])) # 葡萄糖 液化 self.ui.tableWidget.item(1, 2).setText(str(result_8h[6])) # 干物 液化 self.ui.tableWidget.item(1, 3).setText(str(result_8h[31])) # 葡萄糖 酒母 self.ui.tableWidget.item(1, 4).setText(str(result_8h[15])) # 甘油 液化 self.ui.tableWidget.item(1, 5).setText(str(result_8h[5])) # PH 液化 self.ui.tableWidget.item(4, 0).setText(str(result_8h[16])) # 乙酸 液化 self.ui.tableWidget.item(4, 1).setText(str(result_8h[7])) # 粘度 液化 self.ui.tableWidget.item(4, 2).setText(str(result_8h[28])) # DP4 酒母 self.ui.tableWidget.item(4, 3).setText(str(result_8h[8])) # DP4液化 self.ui.tableWidget.item(4, 4).setText(str(result_8h[24])) # 干物酒母 self.ui.tableWidget.item(4, 5).setText(str(result_8h[10])) # DP2 液化 self.ui.tableWidget.item(7, 0).setText(str(result_8h[36])) # 乙酸酒母 self.ui.tableWidget.item(7, 1).setText(str(result_8h[30])) # DP2 酒母 self.ui.tableWidget.item(7, 2).setText(str(result_8h[27])) # 死亡率 # else: # # 如果0h预测数据也不存在,先重置表格数据 # print("8h预测数据不存在,先重置表格数据") # self.ui.tableWidget.item(1, 0).setText(str('')) # GI 液化 # self.ui.tableWidget.item(1, 1).setText(str('')) # 葡萄糖 液化 # self.ui.tableWidget.item(1, 2).setText(str('')) # 干物 液化 # self.ui.tableWidget.item(1, 3).setText(str('')) # 葡萄糖 酒母 # self.ui.tableWidget.item(1, 4).setText(str('')) # 甘油 液化 # self.ui.tableWidget.item(1, 5).setText(str('')) # PH 液化 # self.ui.tableWidget.item(4, 0).setText(str('')) # 乙酸 液化 # self.ui.tableWidget.item(4, 1).setText(str('')) # 粘度 液化 # self.ui.tableWidget.item(4, 2).setText(str('')) # DP4 酒母 # self.ui.tableWidget.item(4, 3).setText(str('')) # DP4液化 # self.ui.tableWidget.item(4, 4).setText(str('')) # 干物酒母 # self.ui.tableWidget.item(4, 5).setText(str('')) # DP2 液化 # self.ui.tableWidget.item(7, 0).setText(str('')) # 乙酸酒母 # self.ui.tableWidget.item(7, 1).setText(str('')) # DP2 酒母 # self.ui.tableWidget.item(7, 2).setText(str('')) # 死亡率 except: res_8h = 0 print("fail fecth 8h data") cursor8h.close() # 读取糖化酶的量#,读取酒母醪的量(第24小时) cursor24h = db.cursor() sql24h = "select * from prediction24h where ID in(select max(ID) from prediction24h where tank_no=%s)"%(currentnumber) try: res_24h = cursor24h.execute(sql24h) if res_24h == 1: #等于1 表示存在该编号 result_24h = cursor24h.fetchone() self.ui.tableWidget.item(1, 0).setText(str(result_24h[24])) # GI 液化 self.ui.tableWidget.item(1, 1).setText(str(result_24h[14])) # 葡萄糖 液化 self.ui.tableWidget.item(1, 2).setText(str(result_24h[9])) # 干物 液化 self.ui.tableWidget.item(1, 3).setText(str(result_24h[34])) # 葡萄糖 酒母 self.ui.tableWidget.item(1, 4).setText(str(result_24h[18])) # 甘油 液化 self.ui.tableWidget.item(1, 5).setText(str(result_24h[8])) # PH 液化 self.ui.tableWidget.item(4, 0).setText(str(result_24h[19])) # 乙酸 液化 self.ui.tableWidget.item(4, 1).setText(str(result_24h[10])) # 粘度 液化 self.ui.tableWidget.item(4, 2).setText(str(result_24h[31])) # DP4 酒母 self.ui.tableWidget.item(4, 3).setText(str(result_24h[11])) # DP4液化 self.ui.tableWidget.item(4, 4).setText(str(result_24h[27])) # 干物酒母 self.ui.tableWidget.item(4, 5).setText(str(result_24h[13])) # DP2 液化 self.ui.tableWidget.item(7, 0).setText(str(result_24h[39])) # 乙酸酒母 self.ui.tableWidget.item(7, 1).setText(str(result_24h[33])) # DP2 酒母 self.ui.tableWidget.item(7, 2).setText(str(result_24h[30])) # 死亡率 self.ui.tableWidget.item(7, 4).setText(str(result_24h[6])) # 酒母 self.ui.tableWidget.item(7, 3).setText(str(result_24h[7])) # 糖化酶 else: #如果是0h或者8h的批次,到这里不会被置空 self.ui.tableWidget.item(7, 4).setText('') # 酒母如果读不到数据就设置为空 self.ui.tableWidget.item(7, 3).setText('') # 糖化酶 except: res_24h = 0 print("fail fecth 24h data") cursor24h.close() ##############################确定发酵状态,给出乙醇浓度预测值############################## #调用FDA模型 with open('scaler_train_0h_classification', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_0h_classification', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_0h[5:])) #获得测试数据输出的预测值1或者2 prob = lda_test.predict(x_test) print(f"当前批次对应模型类别: {prob[0]}") # 建立result表格(时间 批次 罐号 you zhong cha ethanol) # 判断完概率后更新prediction表格(时间 批次 罐号 you zhong cha ethanol) # 对应哪个批次就去对应预测表里读取数据(时间、批次、罐号、乙醇浓度) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() #tableName(result) try: sql = """ create table if not exists result( time DATETIME, tank_no int, tank_name VARCHAR(25), ethanol float, you float, zhong float, cha float, state int ) """ cursor_result.execute(sql) print('Create Table result OK!') except: print('数据表result已存在或未成功建立!') # 插入数据语句 query = """insert into result( time, tank_no,tank_name , ethanol,you,zhong,cha,state) values(%s,%s,%s,%s,%s,%s,%s,%s)""" cursor_result.close() db.close() if prob[0]==1: #判断40h的表中是否有此批次如果有此批次,确定发酵阶段 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor40h = db.cursor() #sql="select * from 40h where number=%s"%(currentnumber) sql40h = "select * from prediction40h where ID in(select max(ID) from prediction40h where tank_no=%s)"%(currentnumber) try: res_40h = cursor40h.execute(sql40h) if res_40h == 1: result_40h = cursor40h.fetchone() except: res_40h = 0 print("fail fecth 40h data") cursor40h.close() db.close() print(res_40h) print(res_24h) print(res_8h) print(res_0h) if res_40h == 1: #说明该编号的40h的预测值已经更新了 那么就调用fda预测模型 self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:green;") #40h-末尾 self.ui.label_115.setText(str(result_40h[4]))#预测的乙醇体积比 #调用FDA模型 with open('scaler_train_40h', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_40h', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_40h[5:])) #获得测试数据输出的概率投影 prob = lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a40 = round(prob[0][0], 3) b40 = round(prob[0][1], 3) c40 = round(prob[0][2], 3) self.ui.label_116.setText(str(a40))#预测的状态 优的概率 self.ui.label_118.setText(str(b40))#预测的状态 中的概率 self.ui.label_120.setText(str(c40))#预测的状态 差的概率 result40data = [] if result_40h: # for tk in result_24h[1:4]: # result40data.append(tk) # 进行类型的转换否则容易出错 result40data.append([result_40h[1]]) result40data.append([int(result_40h[2])]) result40data.append([result_40h[3]]) result40data.append([float(result_40h[4])]) result40data.append(float(a40)) result40data.append(float(b40)) result40data.append(float(c40)) result40data.append(int(prob_state)) if result40data: result40data = tuple(result40data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result40data) print('result40h数据更新成功') except: print('result40h数据更新失败') cursor_result.close() db.close() elif res_24h == 1: self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:green;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.setText(str(result_24h[4]))#预测的乙醇体积比 #调用FDA模型 #调用FDA模型 with open('scaler_train_24h', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_24h', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test=scaler_test.transform(np.matrix(result_24h[5:])) #获得测试数据输出的概率投影 prob=lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a24 = round(prob[0][0], 3) b24 = round(prob[0][1], 3) c24 = round(prob[0][2], 3) self.ui.label_116.setText(str(a24))#预测的状态 优的概率 self.ui.label_118.setText(str(b24))#预测的状态 中的概率 self.ui.label_120.setText(str(c24))#预测的状态 差的概率 result24data = [] if result_24h: # for tk in result_24h[1:4]: # result24data.append(tk) # 进行类型的转换否则容易出错 result24data.append([result_24h[1]]) result24data.append([int(result_24h[2])]) result24data.append([result_24h[3]]) result24data.append([float(result_24h[4])]) result24data.append(float(a24)) result24data.append(float(b24)) result24data.append(float(c24)) result24data.append(int(prob_state)) if result24data: result24data = tuple(result24data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result24data) print('result24h数据更新成功') except: print('result24h数据更新失败') cursor_result.close() db.close() elif res_8h == 1: self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:green;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.setText(str(result_8h[4]))#预测的乙醇体积比 #调用FDA模型 #调用FDA模型 with open('scaler_train_8h','rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_8h','rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_8h[5:])) #获得测试数据输出的概率投影 prob = lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a8 = round(prob[0][0], 3) b8 = round(prob[0][1], 3) c8 = round(prob[0][2], 3) self.ui.label_116.setText(str(a8))#预测的状态 优的概率 self.ui.label_118.setText(str(b8))#预测的状态 中的概率 self.ui.label_120.setText(str(c8))#预测的状态 差的概率 result8data = [] if result_8h: # for tk in result_8h[1:4]: # result8data.append(tk) # 进行类型的转换否则容易出错 result8data.append([result_8h[1]]) result8data.append([int(result_8h[2])]) result8data.append([result_8h[3]]) result8data.append([float(result_8h[4])]) result8data.append(float(a8)) result8data.append(float(b8)) result8data.append(float(c8)) result8data.append(int(prob_state)) if result8data: result8data = tuple(result8data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result8data) print('result8h数据更新成功') except: print('result8h数据更新失败') cursor_result.close() db.close() elif res_0h == 1: self.ui.label_127.setStyleSheet("background-color:green;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.setText(str(result_0h[4]))#预测的乙醇体积比 #调用FDA模型 #调用FDA模型 with open('scaler_train_0h','rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel_0h','rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_0h[5:])) #获得测试数据输出的概率投影 prob = lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a0 = round(prob[0][0], 3) b0 = round(prob[0][1], 3) c0 = round(prob[0][2], 3) self.ui.label_116.setText(str(a0))#预测的状态 优的概率 self.ui.label_118.setText(str(b0))#预测的状态 中的概率 self.ui.label_120.setText(str(c0))#预测的状态 差的概率 result0data = [] if result_0h: # for tk in result_24h[1:4]: # result0data.append(tk) # 进行类型的转换否则容易出错 result0data.append([result_0h[1]]) result0data.append([int(result_0h[2])]) result0data.append([result_0h[3]]) result0data.append([float(result_0h[4])]) result0data.append(float(a0)) result0data.append(float(b0)) result0data.append(float(c0)) result0data.append(int(prob_state)) if result0data: result0data = tuple(result0data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result0data) print('result0h数据更新成功') except: print('result0h数据更新失败') cursor_result.close() db.close() else: self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.clear() self.ui.label_116.clear() self.ui.label_118.clear() self.ui.label_120.clear() elif prob[0]==2: #判断40h的表中是否有此批次如果有此批次,确定发酵阶段 db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor40h = db.cursor() #sql="select * from 40h where number=%s"%(currentnumber) sql40h = "select * from prediction40h where ID in(select max(ID) from prediction40h where tank_no=%s)"%(currentnumber) try: res_40h = cursor40h.execute(sql40h) if res_40h == 1: result_40h = cursor40h.fetchone() else: res_40h = 0 except: res_40h = 0 print("fail fecth 40h data2") cursor40h.close() db.close() print(res_40h) print(res_24h) print(res_8h) print(res_0h) if res_40h == 1: #说明该编号的40h的预测值已经更新了 那么就调用fda预测模型 self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:green;") #40h-末尾 self.ui.label_115.setText(str(result_40h[4]))#预测的乙醇体积比 #调用FDA模型 with open('scaler_train2_40h', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel2_40h', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_40h[5:])) #获得测试数据输出的概率投影 prob = lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a40 = round(prob[0][0], 3) b40 = round(prob[0][1], 3) c40 = round(prob[0][2], 3) self.ui.label_116.setText(str(a40))#预测的状态 优的概率 self.ui.label_118.setText(str(b40))#预测的状态 中的概率 self.ui.label_120.setText(str(c40))#预测的状态 差的概率 result40data=[] if result_40h: # for tk in result_24h[1:4]: # result40data.append(tk) # 进行类型的转换否则容易出错 result40data.append([result_40h[1]]) result40data.append([int(result_40h[2])]) result40data.append([result_40h[3]]) result40data.append([float(result_40h[4])]) result40data.append(float(a40)) result40data.append(float(b40)) result40data.append(float(c40)) result40data.append(int(prob_state)) if result40data: result40data = tuple(result40data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result40data) print('result40h数据更新成功') except: print('result40h数据更新失败') cursor_result.close() db.close() elif res_24h == 1: self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:green;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.setText(str(result_24h[4]))#预测的乙醇体积比 #调用FDA模型 #调用FDA模型 with open('scaler_train2_24h', 'rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel2_24h', 'rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test=scaler_test.transform(np.matrix(result_24h[5:])) #获得测试数据输出的概率投影 prob=lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a24 = round(prob[0][0], 3) b24 = round(prob[0][1], 3) c24 = round(prob[0][2], 3) self.ui.label_116.setText(str(a24))#预测的状态 优的概率 self.ui.label_118.setText(str(b24))#预测的状态 中的概率 self.ui.label_120.setText(str(c24))#预测的状态 差的概率 result24data = [] if result_24h: # for tk in result_24h[1:4]: # result24data.append(tk) # 进行类型的转换否则容易出错 result24data.append([result_24h[1]]) result24data.append([int(result_24h[2])]) result24data.append([result_24h[3]]) result24data.append([float(result_24h[4])]) result24data.append(float(a24)) result24data.append(float(b24)) result24data.append(float(c24)) result24data.append(int(prob_state)) if result24data: result24data = tuple(result24data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result24data) print('result24h数据更新成功') except: print('result24h数据更新失败') cursor_result.close() db.close() elif res_8h == 1: self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:green;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.setText(str(result_8h[4]))#预测的乙醇体积比 #调用FDA模型 #调用FDA模型 with open('scaler_train2_8h','rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel2_8h','rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_8h[5:])) #获得测试数据输出的概率投影 prob = lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a8 = round(prob[0][0], 3) b8 = round(prob[0][1], 3) c8 = round(prob[0][2], 3) self.ui.label_116.setText(str(a8))#预测的状态 优的概率 self.ui.label_118.setText(str(b8))#预测的状态 中的概率 self.ui.label_120.setText(str(c8))#预测的状态 差的概率 result8data = [] if result_8h: # for tk in result_8h[1:4]: # #result8data.append([float(tk)]) # 进行类型的转换否则容易出错 # result8data.append([tk]) result8data.append([result_8h[1]]) result8data.append([int(result_8h[2])]) result8data.append([result_8h[3]]) result8data.append([float(result_8h[4])]) result8data.append(float(a8)) result8data.append(float(b8)) result8data.append(float(c8)) result8data.append(int(prob_state)) if result8data: result8data = tuple(result8data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result8data) print('result8h数据更新成功') except: print('result8h数据更新失败') cursor_result.close() db.close() elif res_0h == 1: self.ui.label_127.setStyleSheet("background-color:green;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.setText(str(result_0h[4]))#预测的乙醇体积比 #调用FDA模型 #调用FDA模型 with open('scaler_train2_0h','rb')as f: scaler_test = pickle.load(f) #导入测试数据的标准化模型 with open('ldamodel2_0h','rb')as f: lda_test = pickle.load(f) #导入LDA模型 #测试数据标准化 x_test = scaler_test.transform(np.matrix(result_0h[5:])) #获得测试数据输出的概率投影 prob = lda_test.predict_proba(x_test) prob_state = lda_test.predict(x_test) a0 = round(prob[0][0], 3) b0 = round(prob[0][1], 3) c0 = round(prob[0][2], 3) self.ui.label_116.setText(str(a0))#预测的状态 优的概率 self.ui.label_118.setText(str(b0))#预测的状态 中的概率 self.ui.label_120.setText(str(c0))#预测的状态 差的概率 result0data = [] if result_0h: # for tk in result_24h[1:4]: # result0data.append(tk) # 进行类型的转换否则容易出错 result0data.append([result_0h[1]]) result0data.append([int(result_0h[2])]) result0data.append([result_0h[3]]) result0data.append([float(result_0h[4])]) result0data.append(float(a0)) result0data.append(float(b0)) result0data.append(float(c0)) result0data.append(int(prob_state)) if result0data: result0data = tuple(result0data) db = MySQLdb.connect("localhost", "root", "123456", "sdic_ecust", charset='utf8', autocommit=1) cursor_result = db.cursor() try: cursor_result.execute(query, result0data) print('result0h数据更新成功') except: print('result0h数据更新失败') cursor_result.close() db.close() else: self.ui.label_127.setStyleSheet("background-color:white;") #0-8h self.ui.label_111.setStyleSheet("background-color:white;") #8-24h self.ui.label_113.setStyleSheet("background-color:white;") #24-40h self.ui.label_112.setStyleSheet("background-color:white;") #40h-末尾 self.ui.label_115.clear() self.ui.label_116.clear() self.ui.label_118.clear() self.ui.label_120.clear() if __name__=="__main__": app = QApplication.instance() PySide2.QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts) if app is None: app = QApplication(sys.argv) model =Model() stock = Stock() stock.ui.show() #stock.ui2.show() #初始化界面中的操作变量表 #QtCore.QTimer.singleShot(0, lambda: center_window(stock.ui))#将UI界面放置在屏幕正中央 sys.exit(app.exec_())
模型训练和验证
# -*- coding: utf-8 -*- """ Created on Fri Mar 19 21:42:34 2021 @author: Administrator """ import numpy as np # todo 对excel的操作 import pandas as pd from sklearn import preprocessing from sklearn.discriminant_analysis import LinearDiscriminantAnalysis import matplotlib.pyplot as plt from minisom import MiniSom from sklearn.metrics import precision_score from sklearn.model_selection import train_test_split # 机器学习工具 from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error, explained_variance_score from sklearn.model_selection import GridSearchCV, cross_val_score, cross_val_predict #指标 from sklearn.metrics import classification_report , confusion_matrix , accuracy_score,roc_curve,roc_auc_score # 绘图基础 import matplotlib as mpl import matplotlib.pyplot as plt #将样本分成了4类 #使用两个目标 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来显示负号 import seaborn as sns sns.set_theme(context='paper', style='ticks', palette='muted', font='SimHei') from catboost import CatBoostRegressor,CatBoostClassifier from sklearn.model_selection import GridSearchCV from imblearn.over_sampling import SMOTE from collections import Counter from sklearn.ensemble import RandomForestClassifier if __name__ == "__main__": def report(model, x, y_train, test, y_test, scaler): model.fit(x, y_train) print(classification_report(y_test, model.predict(scaler.transform(test)))) cnf_matrix = confusion_matrix(y_test, model.predict(scaler.transform(test)))#.argmax(axis=1) y_2 = model.predict(scaler.transform(x_test)) prob = model.predict_proba(scaler.transform(x_test)) class_names = [1, 2, 3] # name of classes fig, ax = plt.subplots() tick_marks = np.arange(len(class_names)) plt.xticks(tick_marks, class_names) plt.yticks(tick_marks, class_names) # create heatmap sns.set(font_scale=1.3) # 将混淆矩阵的字体放大1.3倍 sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu", fmt='g') ax.xaxis.set_label_position("top") plt.tight_layout() plt.title('Confusion matrix', y=1.1) plt.ylabel('Actual label') plt.xlabel('Predicted label') #train_fig = 'Matrix_0h.png' #plt.savefig(train_fig,dpi=300) return model def test_model(model, x, y_train, x_test, y_test, scaler): test_model = model.fit(x, y_train) y_1 = test_model.predict(x) # 测验训练集上的方均误差,看看模型是否有训练能力 print("训练模型平均绝对误差: {}".format(mean_absolute_error(y_train, y_1))) print("训练模型平均方均误差: {}".format(mean_squared_error(y_train, y_1))) print("训练模型R_2指标: {}".format(r2_score(y_train, y_1))) print("训练模型回归方差: {}".format(explained_variance_score(y_train, y_1))) y_2 = test_model.predict(scaler.transform(x_test)) print('.............模型准确率................') print(precision_score(y_test, y_2, average='micro')) print("测试集平均绝对误差: {}".format(mean_absolute_error(y_test, y_2))) print("测试集平均方均误差: {}".format(mean_squared_error(y_test, y_2))) print("测试模型R_2指标: {}".format(r2_score(y_test, y_2))) print("测试模型回归方差: {}".format(explained_variance_score(y_test, y_2))) # 绘图表达 out_train = pd.DataFrame(y_train) out_train.insert(0, 'pre', y_1) out_train = out_train.sort_index() out_test = pd.DataFrame(y_test) out_test.insert(0, 'pre', y_2) out_test = out_test.sort_index() f, ax = plt.subplots(1, 2, figsize=(16, 5)) out_train.plot(ax=ax[0]) out_test.plot(ax=ax[1]) out_test.to_csv('结果导出.csv') return test_model select = 8 # 0 是0h 8是 8h 24是24h 40是40h 选择训练的是哪部分数据 # 将40h、24h、8h、0h划分开来 if select == 40: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\classification\\duomubiao\\newsample40h.xlsx', sheet_name="Sheet1", ) X1 = data.iloc[0:219, 0:121] # 40h 训练数据 X2 = data.iloc[229:373, 0:121] # 40h 训练数据 X3 = data.iloc[383:464, 0:121] # 40h 训练数据 X4 = data.iloc[474:523, 0:121] # 40h 训练数据 X = pd.concat([X1, X2, X3,X4]) # 给样本加标签1:优 2:中 3:差 y1 = data.loc[0:218, '乙醇_体积比(出罐)'] y2 = data.loc[229:372, '乙醇_体积比(出罐)'] y3 = data.loc[383:463, '乙醇_体积比(出罐)'] y4 = data.loc[474:522, '乙醇_体积比(出罐)'] y = pd.concat([y1, y2, y3,y4]) y1_tangchun = data.loc[0:218, '出罐糖醇转化率'] y2_tangchun = data.loc[229:372, '出罐糖醇转化率'] y3_tangchun = data.loc[383:463, '出罐糖醇转化率'] y4_tangchun = data.loc[474:522, '出罐糖醇转化率'] y_tangchun = pd.concat([y1_tangchun, y2_tangchun, y3_tangchun,y4_tangchun]) elif select == 24: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\classification\\duomubiao\\newsample24h.xlsx', sheet_name="Sheet1", ) X1 = data.iloc[0:239, 0:92] X2 = data.iloc[249:410, 0:92] X3 = data.iloc[420:515, 0:92] X4 = data.iloc[525:584, 0:92] X = pd.concat([X1, X2, X3,X4]) # 给样本加标签1:优 2:中 3:差 y1 = data.loc[0:238, '乙醇_体积比(出罐)'] y2 = data.loc[249:409, '乙醇_体积比(出罐)'] y3 = data.loc[420:514, '乙醇_体积比(出罐)'] y4 = data.loc[525:583, '乙醇_体积比(出罐)'] y = pd.concat([y1, y2, y3,y4])#乙醇出罐体积比 y1_tangchun = data.loc[0:238, '出罐糖醇转化率'] y2_tangchun = data.loc[249:409, '出罐糖醇转化率'] y3_tangchun = data.loc[420:514, '出罐糖醇转化率'] y4_tangchun = data.loc[525:583, '出罐糖醇转化率'] y_tangchun = pd.concat([y1_tangchun, y2_tangchun, y3_tangchun,y4_tangchun])#乙醇出罐体积比 elif select == 8: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\all_4\\全部糖醇转化率\\newsample8h.xlsx', sheet_name="Sheet1", ) X1 = data.iloc[0:239, 0:59] X2 = data.iloc[249:487, 0:59] X3 = data.iloc[497:515, 0:59] X4 = data.iloc[525:584, 0:59] X = pd.concat([X1, X2, X3,X4]) # 给样本加标签1:优 2:中 3:差 y1 = data.loc[0:238, '乙醇_体积比(出罐)'] y2 = data.loc[249:486, '乙醇_体积比(出罐)'] y3 = data.loc[497:514, '乙醇_体积比(出罐)'] y4 = data.loc[525:583, '乙醇_体积比(出罐)'] y = pd.concat([y1, y2, y3,y4])#乙醇出罐体积比 y1_tangchun = data.loc[0:238, '出罐糖醇转化率'] y2_tangchun = data.loc[249:486, '出罐糖醇转化率'] y3_tangchun = data.loc[497:514, '出罐糖醇转化率'] y4_tangchun = data.loc[525:583, '出罐糖醇转化率'] y_tangchun = pd.concat([y1_tangchun, y2_tangchun, y3_tangchun,y4_tangchun])#乙醇 elif select == 0: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\all_4\\全部糖醇转化率\\newsample0h.xlsx', sheet_name="Sheet1", ) # mrmr_miq = pymrmr.mRMR(data, 'MIQ', 25) # X = pd.DataFrame(data[mrmr_miq]) X1 = data.iloc[0:239, 0:37] X2 = data.iloc[249:495, 0:37] X3 = data.iloc[505:515, 0:37] X4 = data.iloc[525:584, 0:37] X = pd.concat([X1, X2, X3,X4]) # 给样本加标签1:优 2:中 3:差 y1 = data.loc[0:238, '乙醇_体积比(出罐)'] y2 = data.loc[249:494, '乙醇_体积比(出罐)'] y3 = data.loc[505:514, '乙醇_体积比(出罐)'] y4 = data.loc[525:583, '乙醇_体积比(出罐)'] y = pd.concat([y1, y2, y3,y4])#乙醇出罐体积比 y1_tangchun = data.loc[0:238, '出罐糖醇转化率'] y2_tangchun = data.loc[249:494, '出罐糖醇转化率'] y3_tangchun = data.loc[505:514, '出罐糖醇转化率'] y4_tangchun = data.loc[525:583, '出罐糖醇转化率'] y_tangchun = pd.concat([y1_tangchun, y2_tangchun, y3_tangchun,y4_tangchun])#乙醇 #y:乙醇出罐体积比 y_tangchun:糖醇转化率 y_tangchun = y_tangchun.values.reshape(-1,1) # 糖醇转化率比例值 scaler_y2 = preprocessing.MinMaxScaler().fit(y_tangchun) y_tangchun = scaler_y2.transform(y_tangchun) y_tangchun = y_tangchun[:,0]; label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where((y >= 15)&(y_tangchun >= 0.335))] = 1 label[np.where((y_tangchun > 0.335) & (y < 15)) ] = 2 label[np.where((y_tangchun < 0.335) & (y > 15))] = 3 label[np.where((y_tangchun < 0.335) & (y < 15))] = 4 # 将数据合并之后进行样本的过采样 sos = SMOTE(random_state=0) # 原始数据集的分布情况 # print('训练集中因变量 乙醇浓度 分类情况:{}'.format(Counter(train['cls']))) X_resampled, y_resampled = sos.fit_resample(X, label) print('SMOTE过采样后,训练集 y_resampled 中的分类情况:{}'.format(Counter(y_resampled))) y_label = np.array(label) # 转为numpy数组 #print('np.array后:', X_test_label) y_label = list(map(int, y_label)) # 将一个字符串数组转换成整型数组 #print('np.array后:', X_test_label) print('原始训练集 label 中的分类情况:{}'.format(Counter(y_label))) x_train, x_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2,random_state=888) #x_train, x_test, y_train, y_test = train_test_split(X, label, test_size=0.2,random_state=888) # 测试数据标准化 # scaler_train = preprocessing.StandardScaler().fit(x_train) # X=scaler_train.transform(x_train) # 标准化 scaler = StandardScaler() x = scaler.fit_transform(x_train) other_params = { 'iterations': 1000, # 最大迭代次数 'learning_rate': 0.1, # 学习率 'l2_leaf_reg': 3, # 正则系数 'random_seed': 888, # 随机种子 'bagging_temperature': 1, # 贝叶斯套袋控制强度,区间[0, 1] 'random_strength': 1, # 分数标准差乘数 'depth': 6, 'rsm': 1, 'one_hot_max_size': 2, 'leaf_estimation_method': 'Gradient', 'fold_len_multiplier': 2, 'border_count': 128, } # CatBoostClassifier lda = CatBoostClassifier(iterations=1000, learning_rate=0.25, random_seed=888, #custom_metric='F1', depth=8, verbose=100) #out_model = test_model(lda, x, y_train, x_test, y_test, scaler) #print(out_model.classes_) # model = CatBoostClassifier(iterations=1000, # learning_rate=0.25, # random_seed=888, # depth=8, verbose=100) # out_model = report(model, x, y_train, x_test, y_test, scaler) model = RandomForestClassifier(n_estimators=500,oob_score=True,random_state=10) out_model = report(model, x, y_train, x_test, y_test, scaler) # X_new = lda.transform(X) # map_color = {1: 'r', 2: 'g', 3:'b'} # label1=np.reshape(label,(len(label),)) # label2=label1.tolist()#将label转化为列表 # color = list(map(lambda x: map_color[x],label2)) # plt.figure(figsize=(16,8)) # #将聚类后的样本绘制散点图 # plt.scatter(X_new[:, 0], X_new[:, 1], marker='o', c=color) # plt.show() # print('.............第二类模型训练测试准确率................') # print(precision_score(label, prob, average='micro')) import pickle # 标准化数据 模型保存 with open('catboost_scaler_train2_' + str(select) + 'h', 'wb')as f: pickle.dump(scaler, f) with open('catboost_scaler_train2_' + str(select) + 'h', 'rb')as f: scaler_test = pickle.load(f) # 导入测试数据的标准化模型 with open('catboost_scaler_y_train2_' + str(select) + 'h', 'wb')as f: pickle.dump(scaler_y2, f) with open('catboost_scaler_y_train2_' + str(select) + 'h', 'rb')as f: scaler_y_test = pickle.load(f) # 导入测试数据的标准化模型 with open('catboost_ldamodel2_' + str(select) + 'h', 'wb')as f: pickle.dump(model, f) with open('catboost_ldamodel2_' + str(select) + 'h', 'rb')as f: lda2 = pickle.load(f) # 导入LDA模型 # select2 = 0 # 0 是0h 8是 8h 24是24h 40是40h 选择训练的是哪部分数据 if select == 40: # data = pd.read_excel('D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudel_test\\sample40h.xlsx', # sheet_name="Sheet1", ) #加入新样本 # data = pd.read_excel( # 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\all_4\\sample40h.xlsx', # sheet_name="Sheet1", ) #X = pd.DataFrame(data[mrmr_miq]) X1 = data.iloc[219:227, 0:121] X2 = data.iloc[373:381, 0:121] X3 = data.iloc[464:472, 0:121] X4 = data.iloc[523:531, 0:121] y1 = data.loc[219:226, '乙醇_体积比(出罐)'] y2 = data.loc[373:380, '乙醇_体积比(出罐)'] y3 = data.loc[464:471, '乙醇_体积比(出罐)'] y4 = data.loc[523:530, '乙醇_体积比(出罐)'] y5 = data.loc[219:226, '出罐糖醇转化率'].values.reshape(-1,1) y5 = scaler_y_test.transform(y5) y5 = y5[:, 0]; y6 = data.loc[373:380, '出罐糖醇转化率'].values.reshape(-1,1) y6 = scaler_y_test.transform(y6) y6 = y6[:, 0]; y7 = data.loc[464:471, '出罐糖醇转化率'].values.reshape(-1,1) y7 = scaler_y_test.transform(y7) y7 = y7[:, 0]; y8 = data.loc[523:530, '出罐糖醇转化率'].values.reshape(-1,1) y8 = scaler_y_test.transform(y8) y8 = y8[:, 0]; elif select == 24: # data = pd.read_excel('D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudel_test\\sample24h.xlsx', # sheet_name="Sheet1", ) #加入新样本 # data = pd.read_excel( # 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\all_4\\sample24h.xlsx', # sheet_name="Sheet1", ) #X = pd.DataFrame(data[mrmr_miq]) X1 = data.iloc[239:247, 0:92] X2 = data.iloc[410:418, 0:92] X3 = data.iloc[515:523, 0:92] X4 = data.iloc[584:592, 0:92] y1 = data.loc[239:246 ,'乙醇_体积比(出罐)'] y2 = data.loc[410:417, '乙醇_体积比(出罐)'] y3 = data.loc[515:522, '乙醇_体积比(出罐)'] y4 = data.loc[584:591, '乙醇_体积比(出罐)'] y5 = data.loc[239:246, '出罐糖醇转化率'].values.reshape(-1,1) y5 = scaler_y_test.transform(y5) y5 = y5[:, 0]; y6 = data.loc[410:417, '出罐糖醇转化率'].values.reshape(-1,1) y6 = scaler_y_test.transform(y6) y6 = y6[:, 0]; y7 = data.loc[515:522, '出罐糖醇转化率'].values.reshape(-1,1) y7 = scaler_y_test.transform(y7) y7 = y7[:, 0]; y8 = data.loc[584:591, '出罐糖醇转化率'].values.reshape(-1,1) y8 = scaler_y_test.transform(y8) y8 = y8[:, 0]; elif select == 8: # data = pd.read_excel('D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudel_test\\sample8h.xlsx', # sheet_name="Sheet1", ) #加入新样本 # data = pd.read_excel( # 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\all_4\\sample8h.xlsx', # sheet_name="Sheet1", ) #X = pd.DataFrame(data[mrmr_miq]) X1 = data.iloc[239:247, 0:59] X2 = data.iloc[487:495, 0:59] X3 = data.iloc[515:523, 0:59] X4 = data.iloc[584:592, 0:59] y1 = data.loc[239:246, '乙醇_体积比(出罐)'] y2 = data.loc[487:494, '乙醇_体积比(出罐)'] y3 = data.loc[515:522, '乙醇_体积比(出罐)'] y4 = data.loc[584:591, '乙醇_体积比(出罐)'] y5 = data.loc[239:246, '出罐糖醇转化率'].values.reshape(-1, 1) y5 = scaler_y_test.transform(y5) y5 = y5[:, 0]; y6 = data.loc[487:494, '出罐糖醇转化率'].values.reshape(-1, 1) y6 = scaler_y_test.transform(y6) y6 = y6[:, 0]; y7 = data.loc[515:522, '出罐糖醇转化率'].values.reshape(-1, 1) y7 = scaler_y_test.transform(y7) y7 = y7[:, 0]; y8 = data.loc[584:591, '出罐糖醇转化率'].values.reshape(-1, 1) y8 = scaler_y_test.transform(y8) y8 = y8[:, 0]; elif select == 0: # data = pd.read_excel('D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudel_test\\sample0h.xlsx', # sheet_name="Sheet1", ) #第一部分样本 # data = pd.read_excel( # 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\all_4\\sample0h.xlsx', # sheet_name="Sheet1", ) #X = pd.DataFrame(data[mrmr_miq]) X1 = data.iloc[239:247, 0:37] X2 = data.iloc[495:503, 0:37] X3 = data.iloc[515:523, 0:37] X4 = data.iloc[584:592, 0:37] y1 = data.loc[239:246, '乙醇_体积比(出罐)'] y2 = data.loc[495:502, '乙醇_体积比(出罐)'] y3 = data.loc[515:522, '乙醇_体积比(出罐)'] y4 = data.loc[584:591, '乙醇_体积比(出罐)'] y5 = data.loc[239:246, '出罐糖醇转化率'].values.reshape(-1, 1) y5 = scaler_y_test.transform(y5) y5 = y5[:, 0]; y6 = data.loc[495:502, '出罐糖醇转化率'].values.reshape(-1, 1) y6 = scaler_y_test.transform(y6) y6 = y6[:, 0]; y7 = data.loc[515:522, '出罐糖醇转化率'].values.reshape(-1, 1) y7 = scaler_y_test.transform(y7) y7 = y7[:, 0]; y8 = data.loc[584:591, '出罐糖醇转化率'].values.reshape(-1, 1) y8 = scaler_y_test.transform(y8) y8 = y8[:, 0]; label5 = np.zeros((len(X1), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label5[np.where((y1 >= 15)&(y5 >= 0.335))] = 1 label5[np.where((y5 > 0.335) & (y1 < 15)) ] = 2 label5[ np.where((y5 < 0.335) & (y1 > 15))] = 3 label5[np.where((y5 < 0.335) & (y1 < 15))] = 4 label6 = np.zeros((len(X2), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label6[np.where((y2 >= 15)&(y6 >= 0.335))] = 1 label6[np.where((y6 > 0.335) & (y2 < 15)) ] = 2 label6[np.where((y6 < 0.335) & (y2 > 15))] = 3 label6[np.where((y6 < 0.335) & (y2 < 15))] = 4 label7 = np.zeros((len(X3), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label7[np.where((y3 >= 15)&(y7 >= 0.335))] = 1 label7[np.where((y7 > 0.335) & (y3 < 15))] = 2 label7[ np.where((y7 < 0.335) & (y3 > 15))] = 3 label7[np.where((y7 < 0.335) & (y3 < 15))] = 4 label8 = np.zeros((len(X4), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label8[np.where((y4 >= 15)&(y8 >= 0.335))] = 1 label8[np.where((y8 > 0.335) & (y4 < 15)) ] = 2 label8[ np.where((y8 < 0.335) & (y4 > 15))] = 3 label8[np.where((y8 < 0.335) & (y4 < 15))] = 4 ###第一部分模型的绘制 x1_test = scaler_test.transform(X1) prob = lda2.predict(x1_test) sim_time = np.arange(len(x1_test)) print('..............第二类结果对比...............') # # 绘制多条折线 #prob[0] label1 #poob[1] label5 #每一部分画两条折现 plt.figure(figsize=(16, 12), dpi=100) plt.plot(sim_time, label5, '*-', sim_time, prob, 'o-.') plt.tight_layout() plt.xlabel('number', fontsize=14) plt.ylabel('1 you 2 zhong 3 cha', fontsize=14) plt.legend(['true', 'predicted']) plt.title('Class1 test Compare true' + str(select) + 'h', fontsize=14) plt.show() # # train_fig = 'test_true_predicted_calss20h.png' # # plt.savefig(train_fig) # print('.............第二类模型测试准确率................') print(precision_score(label5, prob, average='micro')) cnf_matrix = confusion_matrix(label5, lda2.predict(x1_test)) #y_2 = model.predict(x1_test) prob_ba = model.predict_proba(x1_test) class_names = [1, 2, 3] # name of classes fig, ax = plt.subplots() tick_marks = np.arange(len(class_names)) plt.xticks(tick_marks, class_names) plt.yticks(tick_marks, class_names) # create heatmap sns.set(font_scale=1.3) # 将混淆矩阵的字体放大1.3倍 sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu", fmt='g') ax.xaxis.set_label_position("top") plt.tight_layout() plt.title('Confusion matrix', y=1.1) plt.ylabel('Actual label') plt.xlabel('Predicted label') plt.show() ###第二部分图形的绘制 x2_test = scaler_test.transform(X2) prob = lda2.predict(x2_test) sim_time = np.arange(len(x2_test)) # print('..............第二类结果对比...............') # # 绘制模型 plt.figure(figsize=(16, 12), dpi=100) plt.plot(sim_time, label6, '*-', sim_time, prob, 'o-.') plt.tight_layout() plt.xlabel('number', fontsize=14) plt.ylabel('1 you 2 zhong 3 cha', fontsize=14) plt.legend(['true', 'predicted']) plt.title('Class2 Test Compare True' + str(select) + 'h', fontsize=14) plt.show() # # train_fig = 'test_true_predicted_calss20h.png' # # plt.savefig(train_fig) # print('.............第二类模型测试准确率................') print(precision_score(label6, prob, average='micro')) cnf_matrix = confusion_matrix(label6, lda2.predict(x2_test)) y_2 = model.predict(x2_test) prob_ba = model.predict_proba(x2_test) class_names = [1, 2, 3] # name of classes fig, ax = plt.subplots() tick_marks = np.arange(len(class_names)) plt.xticks(tick_marks, class_names) plt.yticks(tick_marks, class_names) # create heatmap sns.set(font_scale=1.3) # 将混淆矩阵的字体放大1.3倍 sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu", fmt='g') ax.xaxis.set_label_position("top") plt.tight_layout() plt.title('Confusion matrix', y=1.1) plt.ylabel('Actual label') plt.xlabel('Predicted label') plt.show() ####第三部分 x3_test = scaler_test.transform(X3) prob = lda2.predict(x3_test) sim_time = np.arange(len(x3_test)) print('..............第二类结果对比...............') # # 绘制模型 plt.figure(figsize=(16, 12), dpi=100) plt.plot(sim_time, label7, '*-', sim_time, prob, 'o-.') plt.tight_layout() plt.xlabel('number', fontsize=14) plt.ylabel('1 you 2 zhong 3 cha', fontsize=14) plt.legend(['true', 'predicted']) plt.title('Class3 Test Compare True' + str(select) + 'h', fontsize=14) plt.show() # # train_fig = 'test_true_predicted_calss20h.png' # # plt.savefig(train_fig) # print('.............第二类模型测试准确率................') print(precision_score(label7, prob, average='micro')) cnf_matrix = confusion_matrix(label7, lda2.predict(x3_test)) y_2 = model.predict(x3_test) prob_ba = model.predict_proba(x3_test) class_names = [1, 2, 3] # name of classes fig, ax = plt.subplots() tick_marks = np.arange(len(class_names)) plt.xticks(tick_marks, class_names) plt.yticks(tick_marks, class_names) # create heatmap sns.set(font_scale=1.3) # 将混淆矩阵的字体放大1.3倍 sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu", fmt='g') ax.xaxis.set_label_position("top") plt.tight_layout() plt.title('Confusion matrix', y=1.1) plt.ylabel('Actual label') plt.xlabel('Predicted label') plt.show() ####第四部分 x4_test = scaler_test.transform(X4) prob = lda2.predict(x4_test) sim_time = np.arange(len(x4_test)) print('..............第二类结果对比...............') # # 绘制模型 plt.figure(figsize=(16, 12), dpi=100) plt.plot(sim_time, label8, '*-', sim_time, prob, 'o-.') plt.tight_layout() plt.xlabel('number', fontsize=14) plt.ylabel('1 you 2 zhong 3 cha', fontsize=14) plt.legend(['true', 'predicted']) plt.title('Class4 Test Compare True' + str(select) + 'h', fontsize=14) plt.show() # # train_fig = 'test_true_predicted_calss20h.png' # # plt.savefig(train_fig) # print('.............第二类模型测试准确率................') print(precision_score(label8, prob, average='micro')) cnf_matrix = confusion_matrix(label8, lda2.predict(x4_test)) y_2 = model.predict(x4_test) prob_ba = model.predict_proba(x4_test) class_names = [1, 2, 3] # name of classes fig, ax = plt.subplots() tick_marks = np.arange(len(class_names)) plt.xticks(tick_marks, class_names) plt.yticks(tick_marks, class_names) # create heatmap sns.set(font_scale=1.3) # 将混淆矩阵的字体放大1.3倍 sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu", fmt='g') ax.xaxis.set_label_position("top") plt.tight_layout() plt.title('Confusion matrix', y=1.1) plt.ylabel('Actual label') plt.xlabel('Predicted label') plt.show() 聚类
import numpy as np # todo 对excel的操作 import pandas as pd from sklearn import preprocessing from sklearn.discriminant_analysis import LinearDiscriminantAnalysis import matplotlib.pyplot as plt from minisom import MiniSom from matplotlib.colors import ListedColormap from sklearn.cross_decomposition import PLSRegression from sklearn import datasets from sklearn.model_selection import GridSearchCV import numpy as np if __name__ == "__main__": #乙醇浓度/总糖浓度作为目标 按照比值的大小均匀划分三部分 优中差 select = 40 if select == 40: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\classification\\duomubiao\\newsample40h.xlsx', sheet_name="Sheet1", ) X = data.iloc[:, :122] # 40h 训练数据 elif select == 24: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\classification\\duomubiao\\newsample24h.xlsx', sheet_name="Sheet1", ) X = data.iloc[:, :92] elif select == 8: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\yuanshiyangben\\sample8h.xlsx', sheet_name="Sheet1", ) X = data.iloc[:, :59] elif select == 0: data = pd.read_excel( 'D:\\sxt\\GraduatePrograms\\Tieling\\projects\\moudle_create\\yuanshiyangben\\sample0h.xlsx', sheet_name="Sheet1", ) X = data.iloc[:, :37] y1 = data.loc[:, "乙醇_体积比(出罐)"] y2 = data.loc[:, "出罐糖醇转化率"].values.reshape(-1,1) # 糖醇转化率比例值 scaler_y2 = preprocessing.MinMaxScaler().fit(y2) y2 = scaler_y2.transform(y2) y2 = y2[:,0]; #根据目标值均匀划分三个区间 #40h # label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 # label[np.where(y >= 48.0)] = 1 # label[np.where((45 < y) & (y < 48.0))] = 2 # label[np.where(y <= 45)] = 3 # #24h label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 label[np.where((y1 >= 15)&(y2 >= 0.335))] = 1 label[np.where((y2 > 0.335) & (y1 < 15))] = 2 label[np.where((y2 < 0.335) & (y1 > 15))] = 3 label[np.where((y2 < 0.335) & (y1 < 15))] = 4 # #8h # label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 # label[np.where(y >= 43)] = 1 # label[np.where((37 < y) & (y < 43))] = 2 # label[np.where(y <= 37)] = 3 # # #0h # label = np.zeros((len(X), 1)) # 返回来一个给定形状和类型的用0填充的数组len(x)行,1列 # label[np.where(y >= 43)] = 1 # label[np.where((37 < y) & (y < 43))] = 2 # label[np.where(y <= 37)] = 3 best_num = np.sum(label == 1) # 最好的类别样本数目 middle_num = np.sum(label == 2) # 中等的类别样本数目 worst_num = np.sum(label == 3) # 低等的类别样本数目 # 测试数据标准化 得到scaler,里面存的有计算出来的均值和方差,然后再用scaler中的均值和方差来转换X,使X标准化 scaler_train = preprocessing.StandardScaler().fit(X) X = scaler_train.transform(X) # scaler_train2 = preprocessing.StandardScaler().fit(np.array(y).reshape(-1, 1))#新版本一维数据需要reshape一下才能进行零均值标准化 # Y = scaler_train2.transform(np.array(y).reshape(-1,1)) # lda对测试数据进行聚类,并绘制图像 lda = LinearDiscriminantAnalysis(n_components=2) lda.fit(X, label) # 三个label对应三类样本 X_new = lda.transform(X) map_color = {1: 'r', 2: 'g', 3: 'b', 4: 'c'} label1 = np.reshape(label, (len(label),)) label2 = label1.tolist() # 将label转化为列表 color = list(map(lambda x: map_color[x], label2)) # 将训练数据聚类后的样本绘制散点图 plt.figure() plt.scatter(X_new[:, 0], X_new[:, 1], marker='o', c=color) plt.show() # 将变量数组保存到excel import numpy as np import pandas as pd A = X_new data = pd.DataFrame(A) writer = pd.ExcelWriter('fda_all_zhuanhua2_40h_duomubiao.xlsx') # 写入Excel文件 data.to_excel(writer, 'Sheet1', float_format='%.5f') # ‘page_1’是写入excel的sheet名 writer.save() writer.close() B = label data = pd.DataFrame(B) writer = pd.ExcelWriter('fda_all_zhuanhua2_40h_duomubiao_label.xlsx') # 写入Excel文件 data.to_excel(writer, 'Sheet1', float_format='%.5f') # ‘page_1’是写入excel的sheet名 writer.save() writer.close() 预测
import os import warnings import matplotlib.pyplot as plt import numpy as np import json import pandas as pd from sklearn.svm import SVR from sklearn.model_selection import GridSearchCV import joblib from sklearn.model_selection import learning_curve from sklearn.metrics import mean_squared_error, r2_score from sklearn.preprocessing import MinMaxScaler from sklearn import preprocessing import tensorflow as tf from tensorflow.keras.models import Sequential from mylstm import MixedLSTM1, MixedLSTM2, MixedLSTM3, MixedLSTM4 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False def path_check(path): if not os.path.exists(path): if 'test' in path: warnings.warn("Path for testing data is not Correct. Please Check!") elif 'train' in path: warnings.warn("Path for training data is not Correct. Please Check!") os.makedirs(path) # 读取数据 def load_data(path): print("reading data...") data = pd.read_excel(path +'data.xlsx',sheet_name = "Sheet1", header=0) data = np.asarray(data, dtype=np.float32) label = pd.read_excel(path +'label.xlsx',sheet_name = "Sheet1", header=0) label = label.values.reshape(-1, 1) print(type(data)) print(type(data)) return np.asarray(data, dtype=np.float32), np.asarray(label, dtype=np.float32) def hardlimit(x_array): x_length = len(x_array) for i in range(x_length): if x_array[i] < 0: x_array[i] = 0 elif x_array[i] > 1: x_array[i] = 1 return x_array if __name__ == '__main__': # modes = ['initial', '8h', '24h', '40h'] modes = ['40h'] variable_num_dict = {'initial': 37, '8h': 59, '24h': 92, '40h': 122} test_num = 90 # 测试集数量 for mode in modes:#创建各个模型训练、测试、图形保存、模型保存的文件夹 #test_path = "input_data\\withTemperature\\" + mode + "\\test\\" train_path = "input_data\\withTemperature\\" + mode + "\\train\\" save_data_path = "output_data\\withTemperature\\" + mode + "\\" save_pictures_path = "pictures\\withTemperature\\" + mode + "\\" save_model_path = "model\\withTemperature\\" + mode + "\\" variable_num = variable_num_dict[mode] #path_check(test_path) path_check(train_path) path_check(save_data_path) path_check(save_pictures_path) path_check(save_model_path) # 读取训练数据及测试数据 train_data, train_label = load_data(train_path) #test_data, test_label = load_data(test_path) # 打乱训练数据 train_num = len(train_data) train_index = np.arange(train_num) np.random.shuffle(train_index) train_data = train_data[train_index] train_label = train_label[train_index] # 归一化 mm_x = MinMaxScaler() #mm_y = MinMaxScaler() train_data = mm_x.fit_transform(train_data) #test_data = mm_x.fit_transform(test_data) enc = preprocessing.OneHotEncoder() train_label = enc.fit_transform(train_label) train_label = train_label.A#numpy.ndarray转化为热度编码 #划分训练集和测试集 train_data, train_label, test_data, test_label = train_data[:-test_num, :], train_label[:-test_num, :], train_data[-test_num:, :], train_label[-test_num:, :] #获取测试标签。存入文件夹便于分析 正确率 test_label_raw = enc.inverse_transform(test_label) test_label_raw_file = 'test_label_raw.csv' test_label_raw_csv = pd.DataFrame(test_label_raw) test_label_raw_csv.to_csv(save_data_path + test_label_raw_file, header=False, index=False) #test_label = enc.fit_transform(test_label) #test_label = train_label.A#numpy.ndarray转化为热度编码 # train_data = train_data.reshape(train_data.shape[0], 1, train_data.shape[1]) # test_data = test_data.reshape(test_data.shape[0], 1, test_data.shape[1]) if mode == "initial": lstm = MixedLSTM1() epochs = 480 elif mode == '8h': lstm = MixedLSTM2() epochs = 320 elif mode == '24h': lstm = MixedLSTM3() epochs = 320 else: lstm = MixedLSTM4() epochs = 320 epochs = 480 earlystop_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=20) lstm.compile(loss="categorical_crossentropy", optimizer="adam") lstm.fit(train_data, train_label, batch_size=25, epochs=epochs, validation_split=0.2) predict_label = lstm.predict(train_data) # predict_label = hardlimit(predict_label) prediction_file = 'train_prediction_data_LSTM_onehot.csv' prediction_csv = pd.DataFrame(predict_label) prediction_csv.to_csv(save_data_path + prediction_file, header=False, index=False) sim_time = np.arange(len(train_label)) #将先将预测出的数据变换到与原始数据相同单位的样子。 train_label = enc.inverse_transform(train_label) predict_label = enc.inverse_transform(predict_label) prediction_file = 'train_prediction_data_LSTM.csv' prediction_csv = pd.DataFrame(predict_label) prediction_csv.to_csv(save_data_path + prediction_file, header=False, index=False) plt.figure(1) plt.figure(figsize=(16, 12)) plt.plot(sim_time, train_label, '*-', sim_time, predict_label, 'o-.') plt.xlabel('批次', fontsize=14) plt.ylabel('状态分类(1:优 2:中 3:差)', fontsize=14) plt.legend(['true', 'predicted']) plt.title('训练数据的真实值与预测值对比', fontsize=14) train_fig = save_pictures_path + 'true_predicted_train_LSTM.png' plt.savefig(train_fig) plt.show() #plt.clf() ###########数据测试########### predict_test_label = lstm.predict(test_data) prediction_file = 'test_prediction_data_LSTM_onehot.csv' prediction_csv = pd.DataFrame(predict_test_label) prediction_csv.to_csv(save_data_path + prediction_file, header=False, index=False) #将先将预测出的数据变换到与原始数据相同单位的样子。 test_label = enc.inverse_transform(test_label) predict_test_label = enc.inverse_transform(predict_test_label) prediction_file = 'test_prediction_data_LSTM.csv' prediction_csv = pd.DataFrame(predict_test_label) prediction_csv.to_csv(save_data_path + prediction_file, header=False, index=False) # predict_label = hardlimit(predict_label) MSE = mean_squared_error(test_label, predict_test_label) RMSE = np.sqrt(MSE) R_squared = r2_score(test_label, predict_test_label) sim_time = np.arange(len(test_label)) plt.figure(2) plt.figure(figsize=(8, 6)) plt.plot(sim_time, test_label, '*-', sim_time, predict_test_label, 'o-.') plt.xlabel('批次', fontsize=14) plt.ylabel('状态分类(1:优 2:中 3:差)', fontsize=14) plt.xticks(fontsize=14) plt.yticks(fontsize=14) plt.legend(['true', 'predicted'], fontsize=14) plt.title('测试数据的真实值与预测值对比', fontsize=14) test_fig = save_pictures_path + 'true_predicted_test_LSTM.png' plt.savefig(test_fig, dpi=500, bbox_inches='tight') best_update = False plt.show() #plt.clf() plt.figure(3) plt.figure(figsize=(8, 6)) x = np.arange(0, 1, 0.01) plt.plot(x, x, 'r') plt.scatter(predict_test_label, test_label, c='blue', marker='.') plt.xlabel('Real state', fontsize=14) plt.ylabel('Predicted state', fontsize=14) RMSE_plot = 'RMSE: ' + str('{:0.4f}'.format(RMSE)) plt.text(0.05, 0.85, RMSE_plot, fontsize=14) R_squared_plot = '${R^2}$: ' + str('{:0.4f}'.format(R_squared)) plt.text(0.05, 0.8, R_squared_plot, fontsize=14) test_fig = save_pictures_path + 'scatterplot_true_predicted_test_LSTM.png' plt.savefig(test_fig, dpi=500, bbox_inches='tight') plt.show() #plt.clf() print('MSE: ', MSE) print('RMSE: ', RMSE) print('R^2: ', R_squared)
import tensorflow as tf class MixedLSTM1(tf.keras.Model): def __init__(self): super(MixedLSTM1, self).__init__() self.branch_01 = tf.keras.layers.Dense(27, activation='relu') self.branch_01_01 = tf.keras.layers.Dense(10, activation='relu') self.branch_02 = tf.keras.layers.Dense(28, activation='relu') self.branch_02_01 = tf.keras.layers.Dense(10, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.output_final = tf.keras.layers.Dense(3, activation='softmax') def call(self, inputs, training=None, **kwargs): input1 = inputs[:, 0:18] print("input1: ", input1.shape) input2 = inputs[:, 18:] print("input2: ", input2.shape) output1 = self.branch_01(input1) output1 = self.branch_01_01(output1) output2 = self.branch_02(input2) output2 = self.branch_02_01(output2) output = tf.concat(values=[output1, output2], axis=-1) # output = output1 + output2 output = self.output_final(output) return output class MixedLSTM2(tf.keras.Model): def __init__(self): super(MixedLSTM2, self).__init__() self.branch_01 = tf.keras.layers.Dense(34, activation='relu') self.branch_01_01 = tf.keras.layers.Dense(10, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.branch_02 = tf.keras.layers.LSTM(21, input_shape=(2, 14)) self.branch_03 = tf.keras.layers.Dense(12, activation='relu') self.branch_03_01 = tf.keras.layers.Dense(4, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.output_final = tf.keras.layers.Dense(3, activation='softmax') def call(self, inputs, training=None, **kwargs): input1 = inputs[:, 0:23] print("input1: ", input1.shape) input2 = inputs[:, 23:51] input2 = tf.reshape(input2, (-1, 2, 14)) print("input2: ", input2.shape) input3 = inputs[:, 51:] print("input3: ", input3.shape) output1 = self.branch_01(input1) output1 = self.branch_01_01(output1) output2 = self.branch_02(input2) output3 = self.branch_03(input3) output3 = self.branch_03_01(output3) output = tf.concat(values=[output1, output2, output3], axis=-1) # output = output1 + output2 + output3 output = self.output_final(output) return output class MixedLSTM3(tf.keras.Model): def __init__(self): super(MixedLSTM3, self).__init__() self.branch_01 = tf.keras.layers.Dense(39, activation='relu') self.branch_01_01 = tf.keras.layers.Dense(10, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.branch_02 = tf.keras.layers.LSTM(21, input_shape=(3, 14)) self.branch_03 = tf.keras.layers.Dense(36, activation='relu') self.branch_03_01 = tf.keras.layers.Dense(12, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.output_final = tf.keras.layers.Dense(3, activation='softmax') self.a = self.add_variable('kernel0', shape=[1, 1]) self.b = self.add_variable('kernel1', shape=[1, 1]) self.c = self.add_variable('kernel3', shape=[1, 1]) def call(self, inputs, training=None, **kwargs): input1 = inputs[:, 0:26] print("input1: ", input1.shape) input2 = inputs[:, 26:68] input2 = tf.reshape(input2, (-1, 3, 14)) print("input2: ", input2.shape) input3 = inputs[:, 68:] print("input3: ", input3.shape) output1 = self.branch_01(input1) output1 = self.branch_01_01(output1) output2 = self.branch_02(input2) output3 = self.branch_03(input3) output3 = self.branch_03_01(output3) output = tf.concat(values=[output1, output2, output3], axis=-1) # output = output1 + output2 + output3 output = self.output_final(output) return output class MixedLSTM4(tf.keras.Model): def __init__(self): super(MixedLSTM4, self).__init__() self.branch_01 = tf.keras.layers.Dense(39, activation='relu') self.branch_01_01 = tf.keras.layers.Dense(10, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.branch_02 = tf.keras.layers.LSTM(21, input_shape=(4, 14), kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.branch_03 = tf.keras.layers.Dense(60, activation='relu') self.branch_03_01 = tf.keras.layers.Dense(20, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.0004)) self.output_final = tf.keras.layers.Dense(3, activation='softmax') self.a = self.add_variable('kernel0', shape=[1, 1]) self.b = self.add_variable('kernel1', shape=[1, 1]) self.c = self.add_variable('kernel3', shape=[1, 1]) def call(self, inputs, training=None, **kwargs): input1 = inputs[:, 0:26] print("input1: ", input1.shape) input2 = inputs[:, 26:82] input2 = tf.reshape(input2, (-1, 4, 14)) print("input2: ", input2.shape) input3 = inputs[:, 82:] print("input3: ", input3.shape) output1 = self.branch_01(input1) output1 = self.branch_01_01(output1) output2 = self.branch_02(input2) output3 = self.branch_03(input3) output3 = self.branch_03_01(output3) output = tf.concat(values=[output1, output2, output3], axis=-1) # output = output1 + output2 + output3 output = self.output_final(output) return output
som、散点图
som_data_struct
function sData = som_data_struct(D, varargin)
%SOM_DATA_STRUCT Create a data struct.
%
% sData = som_data_struct(D, [argID, value, ...])
%
% sData = som_data_struct(D);
% sData = som_data_struct(D,'name','my_data','labels',labs);
%
% Input and output arguments ([]'s are optional):
% D (matrix) data matrix, size dlen x dim
% [argID, (string) See below. These are given as argID, value pairs.
% value] (varies)
%
% sData (struct) created data struct
%
% Here are the argument IDs and corresponding values:
% 'labels' (string array / cellstr) labels for each data vector,
% length=dlen
% 'name' (string) data name
% 'comp_names' (string array / cellstr) component names, size dim x 1
% 'comp_norm' (cell array) normalization operations for each
% component, size dim x 1. Each cell is either empty,
% or a cell array of normalization structs.
%
% For more help, try 'type som_data_struct' or check out online documentation.
% See also SOM_SET, SOM_INFO, SOM_MAP_STRUCT.
%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% som_data_struct
%
% PURPOSE
%
% Creates a data structure.
%
% SYNTAX
%
% sD = som_data_struct(D);
% sD = som_data_struct(...,'argID',value,...);
%
% DESCRIPTION
%
% Creates a data struct. The struct contains, in addition to the data
% matrix, component names, normalization operations for the components,
% labels for each vector, and a name for the whole data set. All of these
% can be given in the optional arguments of the function. If left
% unspecified, they are given default values.
%
% Field Type Size / default value
% ------------------------------------------------------------------------
% .type (string) 'som_data'
% .data (matrix) size dlen x dim
% .name (string) 'unnamed'
% .labels (cellstr) size dlen x m, {''; ''; ... ''}
% .comp_names (cellstr) size dim x 1, {'Variable1', 'Variable2', ...}
% .comp_norm (cell array) size dim x 1, {[], [], ... []}
% .label_names (cellstr) size m x 1, []
%
% '.type' field is the struct identifier. Do not change it.
% '.data' field is the data matrix, each row is one data vector
% '.name' field is the identifier for the whole data struct
% '.labels' field contains the labels for each of the vectors. The ith
% of '.labels' contains the labels for ith data vector. Note that
% if some vectors have more labels than others, the others are
% are given empty labels ('') to pad the '.labels' array up.
% '.comp_names' field contains the names of the vector components
% '.comp_norm' field contains normalization information for each
% component. Each cell of '.comp_norm' is itself a cell array of
% normalization structs. If no normalizations are performed for
% the particular component, the cell is empty ([]).
% '.label_names' is similar to .comp_names field holding the names for
% each data label column
%
% REQUIRED INPUT ARGUMENTS
%
% D (matrix) The data matrix, size dlen x dim. The data matrix may
% contain unknown values, indicated by NaNs.
%
% OPTIONAL INPUT ARGUMENTS
%
% argID (string) Argument identifier string (see below).
% value (varies) Value for the argument (see below).
%
% The optional arguments can be given as 'argID',value -pairs as
% listed below. If an argument is given value multiple times, the
% last one is used.
%
% 'labels' (string array / cellstr) labels for each data vector,
% size dlen x m
% 'name' (string) data name
% 'comp_names' (string array / cellstr) component names, size dim x 1
% 'comp_norm' (cell array) normalization operations for each
% component, size dim x 1. Each cell is either empty,
% or a cell array of normalization structs.
% 'label_names'(string array / cellstr) label names, size m x 1
%
% OUTPUT ARGUMENTS
%
% sD (struct) the data struct
%
% EXAMPLES
%
% Simplest case:
% D = rand(8, 3); % 8 3-dimensional vectors
% sD = som_data_struct(D);
%
% With optional arguments, the other fields can be given values:
% labs = cell(8, 1); labs{1, 1} = 'first_label';
% cnames = {'first'; 'second'; 'third'};
%
% sD = som_data_struct(D,'labels',labs,'name','a data struct');
% sD = som_data_struct(D,'comp_names',cnames);
%
% SEE ALSO
%
% som_set Set values and create SOM Toolbox structs.
% som_map_struct Create a map struct.
% Copyright (c) 1997-2000 by the SOM toolbox programming team.
% http://www.cis.hut.fi/projects/somtoolbox/
% Version 1.0beta ecco 071197
% Version 2.0beta juuso 101199
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% data
[dlen dim] = size(D);
% default values
if ~isempty(inputname(1)), name = inputname(1);
else name = 'unnamed'; end
labels = cell(dlen,1);
labels(1:dlen) = {''};
%for i=1:dlen, labels{i} = ''; end
comp_names = cell(dim,1);
for i = 1:dim, comp_names{i} = sprintf('Variable%d', i); end
comp_norm = cell(dim,1);
label_names = [];
% varargin
i=1;
while i<=length(varargin),
argok = 1;
if ischar(varargin{i}),
switch varargin{i},
% argument IDs
case 'comp_names', i=i+1; comp_names = varargin{i};
case 'labels', i=i+1; labels = varargin{i};
case 'name', i=i+1; name = varargin{i};
case 'comp_norm', i=i+1; comp_norm = varargin{i};
case 'label_names',i=i+1; label_names = varargin{i};
otherwise argok=0;
end
else
argok = 0;
end
if ~argok,
disp(['(som_data_struct) Ignoring invalid argument #' num2str(i+1)]);
end
i = i+1;
end
% create struct
sData = som_set('som_data','data',D,'labels',labels,...
'name',name,'comp_names',comp_names,...
'comp_norm',comp_norm,'label_names',label_names);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
som_label
function [sTo] = som_label(sTo, mode, inds, labels)
%SOM_LABEL Give/clear labels to/from map or data struct.
%
% sTo = som_label(sTo, mode, inds [, labels])
%
% sD = som_label(sD,'add',20,'a_label');
% sM = som_label(sM,'replace',[2 4],'a_label');
% sM = som_label(sM,'add',som_bmus(sM,x),'BMU');
% sD = som_label(sD,'prune',[1:10]');
% sM = som_label(sM,'clear','all');
%
% Input and output arguments ([]'s are optional):
% sTo (struct) data or map struct to which the labels are put
% mode (string) 'add' or 'replace' or 'prune' or 'clear'
% inds (vector) indeces of the vectors to which the labels
% are put. Note: this must be a column vector!
% (matrix) subscript indeces to the '.labels' field. The vector
% is given by the first index (e.g. inds(i,1)).
% (string) for 'prune' and 'clear' modes, the string 'all'
% means that all vectors should be pruned/cleared
% [labels] The labels themselves. The number of rows much match
% the number of given indeces, except if there is either
% only one index or only one label. If mode is
% 'prune' or 'clear', labels argument is ignored.
% (string) Label.
% (string array) Each row is a label.
% (cell array of strings) All labels in a cell are handled
% as a group and are applied to the same vector given
% on the corresponding row of inds.
%
% Note: If there is only one label/index, it is used for each specified
% index/label.
%
% For more help, try 'type som_label' or check out online documentation.
% See also SOM_AUTOLABEL, SOM_SHOW_ADD, SOM_SHOW.
%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% som_label
%
% PURPOSE
%
% Add (or remove) labels to (from) map and data structs.
%
% SYNTAX
%
% sTo = som_label(sTo, 'clear', inds)
% sTo = som_label(sTo, 'prune', inds)
% sTo = som_label(sTo, 'add', inds, labels)
% sTo = som_label(sTo, 'replace', inds, labels)
%
% DESCRIPTION
%
% This function can be used to give and remove labels in map and data
% structs. Of course the same operation could be done by hand, but this
% function offers an alternative and hopefully slightly user-friendlier
% way to do it.
%
% REQUIRED INPUT ARGUMENTS
%
% sTo (struct) data or map struct to which the labels are put
% mode (string) The mode of operation.
% 'add' : adds the given labels
% 'clear' : removes labels
% 'replace' : replaces current labels with given
% labels; basically same as 'clear'
% followed by 'add'
% 'prune' : removes empty labels ('') from between
% non-empty labels, e.g. if the labels of
% a vector were {'A','','','B','','C'}
% they'd become {'A','B','C'}. Some empty
% labels may be left at the end of the list.
%
% inds Identifies the vectors to which the operation
% (given by mode) is applied to.
% (vector) Linear indexes of the vectors, size n x 1.
% Notice! This should be a column vector!
% (matrix) The labels are in a cell matrix. By giving matrix
% argument for inds, you can address this matrix
% directly. The first index gives the vector and the
% second index the vertical position of the label in
% the labels array. Size n x 2, where n is the
% number of labels.
% (string) for 'prune' and 'clear' modes, the string 'all'
% means that all vectors should be pruned/cleared
%
% OPTIONAL INPUT ARGUMENTS
%
% [labels] The labels themselves. The number of rows much match
% the number of given indeces, except if there is either
% only one index or only one label.
% (string) Label, e.g. 'label'
% (string array) Each row is a label,
% e.g. ['label1'; 'label2'; 'label3']
% (cell array of strings) All labels in a cell are handled
% as a group and are applied to the same vector given
% on the corresponding row of inds.
% e.g. three labels: {'label1'; 'label2'; 'label3'}
% e.g. a group of labels: {'label1', 'label2', 'label3'}
% e.g. three groups: {{'la1'},{'la21','la22'},{'la3'}
%
% OUTPUT ARGUMENTS
%
% sTo (struct) the given data/map struct with modified labels
%
% EXAMPLES
%
% This is the basic way to add a label to map structure:
% sMap = som_label(sMap,'add',3,'label');
%
% The following examples have identical results:
% sMap = som_label(sMap,'add',[4; 13], ['label1'; 'label2']);
% sMap = som_label(sMap,'add',[4; 13], {{'label1'};{'label2'}});
%
% Labeling the BMU of a vector x (and removing any old labels)
% sMap = som_label(sMap,'replace',som_bmus(sMap,x),'BMU');
%
% Pruning labels
% sMap = som_label(sMap,'prune','all');
%
% Clearing labels from a struct
% sMap = som_label(sMap,'clear','all');
% sMap = som_label(sMap,'clear',[1:4, 9:30]');
%
% SEE ALSO
%
% som_autolabel Automatically label a map/data set.
% som_show Show map planes.
% som_show_add Add for example labels to the SOM_SHOW visualization.
% Copyright (c) 1997-2000 by the SOM toolbox programming team.
% http://www.cis.hut.fi/projects/somtoolbox/
% Version 2.0beta juuso 101199
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% check arguments
narginchk(3, 4); % check no. of input args is correct
% sTo
switch sTo.type,
case 'som_map', [dlen dim] = size(sTo.codebook);
case 'som_data', [dlen dim] = size(sTo.data);
end
maxl = size(sTo.labels,2); % maximum number of labels for a single vector
% inds
if ischar(inds) & strcmp(inds,'all'),
inds = [1:dlen]';
end
if length(inds)>2 & size(inds,2)>2, inds = inds'; end
ni = size(inds,1);
n = ni;
% labels
if nargin==4,
% convert labels to a cell array of cells
if ischar(labels), labels = cellstr(labels); end
if iscellstr(labels),
tmplab = labels;
nl = size(labels,1);
labels = cell(nl,1);
for i=1:nl,
if ~iscell(tmplab{i})
if ~isempty(tmplab{i}), labels{i} = tmplab(i,:);
else labels{i} = {}; end
else
labels(i) = tmplab(i);
end
end
clear tmplab;
end
nl = size(labels,1);
end
% the case of a single label/index
if any(strcmp(mode,{'add','replace'})),
n = max(nl,ni);
if n>1,
if ni==1,
inds = zeros(n,1)+inds(1);
elseif nl==1,
label = labels{1};
labels = cell(n,1);
for i=1:n, labels{i} = label; end
elseif ni ~= nl,
error('The number of labels and indexes does not match.');
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% action
switch mode,
case 'clear',
if size(inds,2)>2,
inds = inds(find(inds(:,2)<=maxl),:); % ignore if subindex is out-of-range
inds = sub2ind([dlen maxl],inds(:,1),inds(:,2));
sTo.labels{inds} = [];
else
sTo.labels(inds,:) = cell(n,maxl);
end
case 'prune',
if size(inds,2)==1,
% subindex gives the index from which the pruning is started
inds = [inds, ones(n,1)]; % from 1 by default
end
select = ones(1,maxl);
for i=1:n,
v = inds(i,1); s = inds(i,2); select(:) = 1;
for j=s:maxl, select(j) = ~isempty(sTo.labels{v,j}); end
if ~all(select),
labs = cell(1,maxl);
labs(1:sum(select)) = sTo.labels(v,find(select));
sTo.labels(v,:) = labs;
end
end
case 'add',
if size(inds,2)==1,
% subindex gives the index from which the adding is started
inds = [inds, ones(n,1)]; % from 1 by default
end
for i=1:n,
v = inds(i,1); s = inds(i,2); l = length(labels{i});
for j=1:l,
while s<=size(sTo.labels,2) & ~isempty(sTo.labels{v,s}), s=s+1; end
sTo.labels{v,s} = labels{i}{j};
s=s+1;
end
end
case 'replace',
if size(inds,2)==1,
% subindex gives the index from which the replacing is started
inds = [inds, ones(n,1)]; % from 1 by default
end
for i=1:n,
v = inds(i,1); s = inds(i,2); l = length(labels(i));
for j=1:l, sTo.labels{v,s-1+j} = labels{i}{j}; end
end
otherwise
error(['Unrecognized mode: ' mode]);
end
sTo.labels = remove_empty_columns(sTo.labels);
[dlen maxl] = size(sTo.labels);
for i=1:dlen,
for j=1:maxl,
if isempty(sTo.labels{i,j}) & ~ischar(sTo.labels{i,j}),
sTo.labels{i,j} = '';
end
end
end
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% subfunctions
function labels = remove_empty_columns(labels)
[dlen maxl] = size(labels);
% find which columns are empty
cols = zeros(1,maxl);
for i=1:dlen,
for j=1:maxl,
cols(j) = cols(j) + ~isempty(labels{i,j});
end
end
while maxl>0 & cols(maxl)==0, maxl = maxl-1; end % check starting from end
if maxl==0, labels = cell(dlen,1);
elseif maxl
end
% end of remove_empty_columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
som_normlize
function sD = som_normalize(sD,method,comps)
%SOM_NORMALIZE (Re)normalize data or add new normalizations.
%
% sS = som_normalize(sS,[method],[comps])
%
% sS = som_normalize(sD)
% sS = som_normalize(sS,sNorm)
% D = som_normalize(D,'var')
% sS = som_normalize(sS,'histC',[1:3 10])
%
% Input and output arguments ([]'s are optional):
% sS The data to which the normalization is applied.
% The modified and updated data is returned.
% (struct) data or map struct
% (matrix) data matrix (a matrix is also returned)
% [method] The normalization method(s) to add/use. If missing,
% or an empty variable ('') is given, the
% normalizations in sS are used.
% (string) identifier for a normalization method to be added:
% 'var', 'range', 'log', 'logistic', 'histD' or 'histC'.
% (struct) Normalization struct, or an array of such.
% Alternatively, a map/data struct can be given
% in which case its '.comp_norm' field is used
% (see below).
% (cell array) Of normalization structs. Typically, the
% '.comp_norm' field of a map/data struct. The
% length of the array must be equal to data dimension.
% (cellstr array) norm and denorm operations in a cellstr array
% which are evaluated with EVAL command with variable
% name 'x' reserved for the variable.
% [comps] (vector) the components to which the normalization is
% applied, default is [1:dim] ie. all components
%
% For more help, try 'type som_normalize' or check out online documentation.
% See also SOM_DENORMALIZE, SOM_NORM_VARIABLE, SOM_INFO.
%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% som_normalize
%
% PURPOSE
%
% Add/apply/redo normalization on data structs/sets.
%
% SYNTAX
%
% sS = som_normalize(sS)
% sS = som_normalize(sS,method)
% D = som_normalize(D,sNorm)
% sS = som_normalize(sS,csNorm)
% sS = som_normalize(...,comps)
%
% DESCRIPTION
%
% This function is used to (initialize and) add, redo and apply
% normalizations on data/map structs/sets. If a data/map struct is given,
% the specified normalizations are added to the '.comp_norm' field of the
% struct after ensuring that all normalizations specified therein have
% status 'done'. SOM_NORMALIZE actually uses function SOM_NORM_VARIABLE
% to handle the normalization operations, and only handles the data
% struct/set specific stuff itself.
%
% The different normalization methods are listed below. For more
% detailed descriptions, see SOM_NORM_VARIABLE.
%
% method description
% 'var' Variance is normalized to one (linear operation).
% 'range' Values are normalized between [0,1] (linear operation).
% 'log' Natural logarithm is applied to the values:
% xnew = log(x-m+1)
% where m = min(x).
% 'logistic' Logistic or softmax trasformation which scales all
% possible values between [0,1].
% 'histD' Histogram equalization, values scaled between [0,1].
% 'histC' Approximate histogram equalization with partially
% linear operations. Values scaled between [0,1].
% 'eval' freeform operations
%
% To enable undoing and applying the exactly same normalization to
% other data sets, normalization information is saved into a
% normalization struct, which has the fields:
%
% .type ; struct type, ='som_norm'
% .method ; normalization method, a string
% .params ; normalization parameters
% .status ; string: 'uninit', 'undone' or 'done'
%
% Normalizations are always one-variable operations. In the data and map
% structs the normalization information for each component is saved in the
% '.comp_norm' field, which is a cell array of length dim. Each cell
% contains normalizations for one vector component in a struct array of
% normalization structs. Each component may have different amounts of
% different kinds of normalizations. Typically, all normalizations are
% either 'undone' or 'done', but in special situations this may not be the
% case. The easiest way to check out the status of the normalizations is to
% use function SOM_INFO, e.g. som_info(sS,3)
%
% REQUIRED INPUT ARGUMENTS
%
% sS The data to which the normalization is applied.
% (struct) Data or map struct. Before adding any new
% normalizations, it is ensured that the
% normalizations for the specified components in the
% '.comp_norm' field have status 'done'.
% (matrix) data matrix
%
% OPTIONAL INPUT ARGUMENTS
%
% method The normalization(s) to add/use. If missing,
% or an empty variable ('' or []) is given, the
% normalizations in the data struct are used.
% (string) Identifier for a normalization method to be added:
% 'var', 'range', 'log', 'logistic', 'histD' or 'histC'. The
% same method is applied to all specified components
% (given in comps). The normalizations are first
% initialized (for each component separately, of
% course) and then applied.
% (struct) Normalization struct, or an array of structs, which
% is applied to all specified components. If the
% '.status' field of the struct(s) is 'uninit',
% the normalization(s) is initialized first.
% Alternatively, the struct may be map or data struct
% in which case its '.comp_norm' field is used
% (see the cell array option below).
% (cell array) In practice, the '.comp_norm' field of
% a data/map struct. The length of the array
% must be equal to the dimension of the given
% data set (sS). Each cell contains the
% normalization(s) for one component. Only the
% normalizations listed in comps argument are
% applied though.
% (cellstr array) norm and denorm operations in a cellstr array
% which are evaluated with EVAL command with variable
% name 'x' reserved for the variable.
%
% comps (vector) The components to which the normalization(s) is
% applied. Default is to apply to all components.
%
% OUTPUT ARGUMENTS
%
% sS Modified and/or updated data.
% (struct) If a struct was given as input argument, the
% same struct is returned with normalized data and
% updated '.comp_norm' fields.
% (matrix) If a matrix was given as input argument, the
% normalized data matrix is returned.
%
% EXAMPLES
%
% To add (initialize and apply) a normalization to a data struct:
%
% sS = som_normalize(sS,'var');
%
% This uses 'var'-method to all components. To add a method only to
% a few selected components, use the comps argument:
%
% sS = som_normalize(sS,'log',[1 3:5]);
%
% To ensure that all normalization operations have indeed been done:
%
% sS = som_normalize(sS);
%
% The same for only a few components:
%
% sS = som_normalize(sS,'',[1 3:5]);
%
% To apply the normalizations of a data struct sS to a new data set D:
%
% D = som_normalize(D,sS);
% or
% D = som_normalize(D,sS.comp_norm);
%
% To normalize a data set:
%
% D = som_normalize(D,'histD');
%
% Note that in this case the normalization information is lost.
%
% To check out the status of normalization in a struct use SOM_INFO:
%
% som_info(sS,3)
%
%
% SEE ALSO
%
% som_denormalize Undo normalizations of a data struct/set.
% som_norm_variable Normalization operations for a set of scalar values.
% som_info User-friendly information of SOM Toolbox structs.
% Copyright (c) 1998-2000 by the SOM toolbox programming team.
% http://www.cis.hut.fi/projects/somtoolbox/
% Version 2.0beta juuso 151199 150500
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% check arguments
error(nargchk(1, 3, nargin)); % check no. of input arguments is correct
% sD
struct_mode = isstruct(sD);
if struct_mode,
switch sD.type
case 'som_map', D = sD.codebook;
case 'som_data', D = sD.data;
otherwise, error('Illegal struct.')
end
else
D = sD;
end
[dlen dim] = size(D);
% comps
if nargin<3 | (ischar(comps) & strcmp(comps,'all')),
comps = [1:dim];
end
if isempty(comps), return; end
if size(comps,1)>1, comps = comps'; end % make it a row vector
% method
csNorm = cell(dim,1);
if nargin<2 | isempty(method),
if ~struct_mode,
warning('No normalization method given. Data left unchanged.');
return;
end
method = '';
else
% check out the given method
% (and if necessary, copy it for each specified component)
if ischar(method),
switch method,
case {'var','range','log','histD','histC','logistic'},
sN = som_set('som_norm','method',method);
otherwise,
error(['Unrecognized method: ' method]);
end
for i=comps, csNorm{i} = sN; end
elseif isstruct(method),
switch method(1).type,
case {'som_map','som_data'}, csNorm = method(1).comp_norm;
case {'som_norm'}, for i=comps, csNorm{i} = method; end
otherwise,
error('Invalid struct given as normalization method.')
end
elseif iscellstr(method),
[dummy,sN] = som_norm_variable(1,method,'init');
for i=comps, csNorm{i} = sN; end
elseif iscell(method),
csNorm = method;
else
error('Illegal method argument.')
end
% check the size of csNorm is the same as data dimension
if length(csNorm) ~= dim,
error('Given number of normalizations does not match data dimension.')
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% initialize
% make sure all the current normalizations for current
% components have been done
if struct_mode,
alldone = 1;
for i = comps,
for j=1:length(sD.comp_norm{i}),
sN = sD.comp_norm{i}(j);
if ~strcmp(sN.status,'done'),
alldone = 0;
[x,sN] = som_norm_variable(D(:,i), sN, 'do');
D(:,i) = x;
sD.comp_norm{i}(j) = sN;
end
end
end
if isempty(method),
if alldone,
warning('No ''undone'' normalizations found. Data left unchanged.');
else
fprintf(1,'Normalizations have been redone.\n');
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% action
% add the new normalizations to the old ones
for i = comps,
if ~isempty(csNorm{i}),
[x,sN] = som_norm_variable(D(:,i), csNorm{i}, 'do');
D(:,i) = x;
if struct_mode,
if isempty(sD.comp_norm{i}), sD.comp_norm{i} = sN;
else sD.comp_norm{i} = [sD.comp_norm{i}, sN]; end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% output
if struct_mode,
switch sD.type
case 'som_map', sD.codebook = D;
case 'som_data', sD.data = D;
otherwise, error('Illegal struct.')
end
else
sD = D;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
som
clf
clc
clear
close all
% 关闭警告,可注释
warning('off');
figure;
set(gcf,'outerposition',get(0,'screensize'));
%此程序没有进行测试直接进行训练
ds=load('D:\sxt\matlab\projects\soms\fda24hduomubiao2.mat');
ds00=ds.fdaallzhuanhua224hduomubiaolabel(1:181,1:2);%优1
ds01=ds.fdaallzhuanhua224hduomubiaolabel(182:350,1:2);%中2
ds02=ds.fdaallzhuanhua224hduomubiaolabel(351:448,1:2);%差3
ds03=ds.fdaallzhuanhua224hduomubiaolabel(449:592,1:2);%差4
%
% % 划分训练集,按4:1划分,训练集占80%
% ds00_tr=[];
% for i=1:40
% if mod(i,5) ~= 0% mod(a,b)是求余的意思,如mod(5,3)=2,mod(20,7)=6,500里是5的倍数的个数是100个,正好用做测试集,而且符合随机抽取的要求 不符合要求的正好存在400个当作训练集
% ds00_tr=[ds00_tr;ds00(i,:)];
% end
% end
% ds01_tr=[];
% for i=1:59
% if mod(i,5) ~= 0
% ds01_tr=[ds01_tr;ds01(i,:)];
% end
% end
% ds02_tr=[];
% for i=1:43
% if mod(i,5) ~= 0
% ds02_tr=[ds02_tr;ds02(i,:)];
% end
% end
%
% % 划分测试集,按4:1划分,测试集占20%
% ds00_te=ds00(5:5:40,:);%抽取的初始值决定了不会产生重合数据
% ds01_te=ds01(5:5:59,:);
% ds02_te=ds02(5:5:43,:);
%
% % 将训练集合并,tr为所有训练数据
tr=[ds00;ds01;ds02;ds03];
%
% % 将测试集合并,te为所有测试数据
% te=[ds00_te;ds01_te;ds02_te];
% 数据标准化
TR=zscore(tr); %标准化处理数据
%TE=normalize(te,tr); %对测试样本进行标准化处理,将输入的某个值按照训练集的特征进行标准化
%SOM算法数据初始化,对训练数据做标记,训练数据结构体命名为sD
% sD = som_data_struct(TR,'name','TRAIN');
% sD = som_label(sD,'add',[1:400],'G'); %正常状态标记为N
% sD = som_label(sD,'add',[401:600],'M'); %故障状态1标记为F1
% sD = som_label(sD,'add',[601:800],'B'); %故障状态2标记为F2
% sD = som_normalize(sD,'var');
sD = som_data_struct(TR,'name','TRAIN');
sD = som_label(sD,'add',[1:181],'G'); %正常状态标记为N
sD = som_label(sD,'add',[182:350],'M'); %故障状态1标记为F1
sD = som_label(sD,'add',[351:448],'B'); %故障状态2标记为F2
sD = som_label(sD,'add',[449:592],'W'); %故障状态2标记为F2
sD = som_normalize(sD,'var');
% %SOM算法数据初始化,对测试数据做标记,测试数据结构体命名为sD_te
% sD_te = som_data_struct(TE,'name','TEST');
% sD_te = som_label(sD_te,'add',[1:8],'G'); %正常状态标记为N
% sD_te = som_label(sD_te,'add',[9:19],'M'); %故障状态1标记为F1
% sD_te = som_label(sD_te,'add',[20:27],'B'); %故障状态2标记为F2
% sD_te = som_normalize(sD_te,'var');
%对训练样本进行训练
sMap = som_make(sD,'algorithm','seq'); %训练som网络 若不设置msize则算法自行确定网络结构的大小
sMap = som_label(sMap,'clear','all');
sMap = som_autolabel(sMap,sD,'freq');
somsize=sMap.topol.msize(1);
% sMap = som_autolabel(sMap,sD_te,'freq');
%计算训练数据结果
h1 = som_hits(sMap,sD.data(1:181,:));%每一个数据都落到一个区域,得到聚类的结果
h2 = som_hits(sMap,sD.data(182:350,:));
h3 = som_hits(sMap,sD.data(351:448,:));
h4 = som_hits(sMap,sD.data(449:592,:));
% %计算测试数据结果
% h1_te = som_hits(sMap,sD_te.data(1:1:8,:));%通过训练集的训练得到模型
% h2_te = som_hits(sMap,sD_te.data(8:1:20,:));
% h3_te = som_hits(sMap,sD_te.data(21:1:27,:));
%数据可视化
som_show(sMap,'empty','状态投影');
som_show_add('label',sMap,'Textsize',9,'TextColor','k','Subplot',1); %显示添加的标签
%som_show_add('hit',[h1,h2,h3],'MarkerColor',[0/255 255/255 128/255;252/255 145/255 58/255;255/255 78/255 80/255],'Subplot',1) %显示训练样本的碰撞直方图
%som_show_add('hit',[h1_te,h2_te,h3_te],'MarkerColor',[0 1 0.5;1 0.5 0;1 0 0],'Subplot',1) %显示测试样本的碰撞直方图
som_show_add('hit',[h1,h2,h3,h4],'MarkerColor',[1 0 0;1 1 0;0 0 1;0 0 0],'Subplot',1) %显示测试样本的碰撞直方图 红色 黄色 蓝色
散点图
% clc
% clear
% close all
% [X_num,X_txt,X_raw]=xlsread('C:\Users\Administrator.000\Desktop\新数据整理结果.xlsx','去异常train1','A3:AN628');
% x_data=X_num(:,1:37);
% y=X_num(:,39);%64h的乙醇体积比
%
% x=x_data(:,28);%改这个变量
% str='果糖(酒母)';
% per=10;
% minvalue=min(x);
% maxvalue=max(x);
% % minvalue=3.8;
% % maxvalue=4.8;
% scatter(x,y,10);
% axis( [minvalue maxvalue 13 16.8] )
% set(gca,'ytick',13:0.2:16.8);
% ylabel('乙醇体积比')
% xlabel(str)
% title(str)
% hold on
% width=(maxvalue-minvalue)/per;
% mininum=zeros(per,1);
% maximum=zeros(per,1);
% meanvalue=zeros(per,1);
% tabel=cell(per+1,4);
% tabel{1,1}=str;tabel{1,2}='最小值';tabel{1,3}='最大值';tabel{1,4}='均值';
% for i=1:per
% index=find (x>=(width*(i-1)+minvalue) & x<(width*i)+minvalue); %找出在这个区间内的x索引
% per_data=y(index);
% mininum(i)=min(per_data);
% maximum(i)=max(per_data);
% meanvalue(i)=mean(per_data);
% %填表
% tabel{1+i,1}=['(',num2str(width*(i-1)+minvalue),',',num2str(width*i+minvalue), ')'];
% tabel{1+i,2}=mininum(i);
% tabel{1+i,3}=maximum(i);
% tabel{1+i,4}=meanvalue(i);
% %画图
% plot([(minvalue+width*i),(minvalue+width*i)],[13,17],'g--','LineWidth', 1);
% hold on
% plot([minvalue+width*(i-1),(minvalue+width*i)],[meanvalue(i),meanvalue(i)],'r','LineWidth', 2);
% hold on
% end
%%
%从第一个变量开始画散点图
clc
clear
close all
% load('train1_index_mi64.mat')
% [X_num,X_txt,X_raw]=xlsread('C:\Users\Administrator.000\Desktop\新数据整理结果.xlsx','去异常train1','A3:AN420');
% [X_num,X_txt,X_raw]=xlsread('C:\Users\Administrator.000\Desktop\新数据整理结果.xlsx','删减一点点异常','A3:AO599');
% [X_num,X_txt,X_raw]=xlsread('C:\Users\Administrator.000\Desktop\铁岭数据整理\数据整合.xlsx','总数据_去小异常','A3:AP579');
[X_num1,X_txt1,X_raw1]=xlsread('D:\sxt\GraduatePrograms\Tieling\projects\scatter\alldata\sample40h(1).xlsx','Sheet1','A2:AR197');
[X_num2,X_txt2,X_raw2]=xlsread('D:\sxt\GraduatePrograms\Tieling\projects\scatter\alldata\sample40h(2).xlsx','Sheet1','A2:AR151');
[X_num3,X_txt3,X_raw3]=xlsread('D:\sxt\GraduatePrograms\Tieling\projects\scatter\alldata\sample40h(3).xlsx','Sheet1','A2:AR89');
x_data1=X_num1(:,1:43);%选择所有的行,前37列作为x轴的变量
x_data2=X_num2(:,1:43);%选择所有的行,前37列作为x轴的变量
x_data3=X_num3(:,1:43);
ff=0;
name=["液化醪量","酒母醪量","糖化酶量", "1H温度", "2H温度", "3H温度", "4H温度", "5H温度", "6H温度", "7H温度", "8H温度", "9H温度", "10H温度", "11H温度", "12H温度", "13H温度", "14H温度", "15H温度",...
"16H温度","17H温度","18H温度","19H温度","20H温度","21H温度", "22H温度", "23H温度", "24H温度", "25H温度", "26H温度", "27H温度", "28H温度", "29H温度", "30H温度","31H温度", "32H温度","33H温度","34H温度",...
"35H温度","36H温度","37H温度","38H温度","39H温度","40H温度"];
% k_index=int8(train1_index_mi64);
k_index=1:43;
for kq=1:43%遍历37个变量
k=k_index(kq);
x1=x_data1(:,k);%将k对应的变量取出来
x2=x_data2(:,k);%将k对应的变量取出来
x3=x_data3(:,k);%将k对应的变量取出来
%使用肖维勒方法(等置信概率)剔除异常值
[m1,n1]=size(x1);
[m2,n2]=size(x2);
[m3,n3]=size(x3);
Yi=[];%用来放置异常值
Yi2=[];%用来放置异常值
Yi3=[];%用来放置异常值
w1=1+0.4*log(m1);
w2=1+0.4*log(m2);
w3=1+0.4*log(m3);
for q1=1:n1
xd1=x1(:,q1);
YiChang=abs(xd1-mean(xd1))>w1*std(xd1);
Yi(:,q1)=YiChang;
end
for q2=1:n2
xd2=x2(:,q2);
YiChang2=abs(xd2-mean(xd2))>w2*std(xd2);
Yi2(:,q2)=YiChang2;
end
for q3=1:n3
xd3=x3(:,q3);
YiChang3=abs(xd3-mean(xd3))>w3*std(xd3);
Yi3(:,q3)=YiChang3;
end
[u1,v1] =find(Yi()==1);
uu1=unique(u1);%剔除重复的行数
x1(uu1,:)=[];%令异常值所在行为空
y1=X_num1(:,end);%最终的乙醇体积比
y1(uu1,:)=[];%将同一行的输出值剔除,保证x、y具有相同的长度
[u2,v2] =find(Yi2()==1);
uu2=unique(u2);%剔除重复的行数
x2(uu2,:)=[];%令异常值所在行为空
y2=X_num2(:,end);%最终的乙醇体积比
y2(uu2,:)=[];%将同一行的输出值剔除,保证x、y具有相同的长度
[u3,v3] =find(Yi3()==1);
uu3=unique(u3);%剔除重复的行数
x3(uu3,:)=[];%令异常值所在行为空
y3=X_num3(:,end);%最终的乙醇体积比
y3(uu3,:)=[];%将同一行的输出值剔除,保证x、y具有相同的长度
str=name(1,k); %将k对应的变量取出来,赋予具体的值
per=5;%将去除的变量划分为10部分
% if k==32|| k==9 %乙酸(酒母)或者琥珀酸的变量数据划分为4部分
% per=4;
% end
minvalue1=min(x1);
maxvalue1=max(x1);
minvalue2=min(x2);
maxvalue2=max(x2);
minvalue3=min(x3);
maxvalue3=max(x3);
if minvalue1>=minvalue2
minvalue= minvalue2;
else
minvalue =minvalue1;
end
if maxvalue1>=maxvalue2
maxvalue= maxvalue1;
else
maxvalue =maxvalue2;
end
%将已经确定的最大最小值和第三类结果对比
if minvalue>=minvalue3
minvalue= maxvalue3;
else
end
if maxvalue>=maxvalue3
else
maxvalue =maxvalue3;
end
figure(kq)%绘制当前变量对应乙醇体积比的散点图
scatter(x1,y1,10,'r');%第一类蓝
hold on;
scatter(x2,y2,10,'g');%第二类红
hold on
scatter(x3,y3,10,'b');%第三类黄
%legend('原始数据','更新数据')%散点图的话不好标注图列如何保证添加图例的代码只运行一次,如何控制添加图例的大小
x=[x1;x2;x3];
y=[y1;y2;y3];
axis( [minvalue maxvalue 12.87015 16.6583] ) %规定x、y轴的范围
set(gca,'ytick',12.8702:0.2:16.6583 ); %将当前图形y轴刻度标志为:13:0.2:16.8 6.8 13.4
% axis( [minvalue maxvalue 12 16.6] )
% set(gca,'ytick',12:0.2:16.6);
ylabel('乙醇体积比(出罐)')
xlabel(str)
title(str)
hold on %保持原图并多次叠绘
width=(maxvalue-minvalue)/per; %划分10个区间的宽度
mininum=zeros(per,1);%生成一个10*1的零矩阵放置每部分数据的最小值
maximum=zeros(per,1);
meanvalue=zeros(per,1);
tabel=cell(per+1,4);%创建了一个元胞数组,仅为预定义,后面需要利用赋值法对元胞的每个单元进行初始化赋值
% tabel{1,1}=str;tabel{1,2}='最小值';tabel{1,3}='最大值';tabel{1,4}='均值';
for i=1:per %绘制出每部分数据的散点图
if (width*i+minvalue)==maxvalue
index=find (x>=(width*(i-1)+minvalue) & x<=(width*i)+minvalue); %找出在这个区间内的x索引
else
index=find (x>=(width*(i-1)+minvalue) & x<(width*i)+minvalue); %找出在这个区间内的x索引
end
if isempty(index)==0
per_data=y(index);
mininum(i)=min(per_data);%DP3在绘制第5部分数据时出现error:左侧和右侧的元素数目不同因为index根本没有取到index因为第五部分确实没有值
maximum(i)=max(per_data);
meanvalue(i)=mean(per_data);
%填表
% tabel{1+i,1}=['(',num2str(width*(i-1)+minvalue),',',num2str(width*i+minvalue), ')'];
% tabel{1+i,2}=mininum(i);
% tabel{1+i,3}=maximum(i);
% tabel{1+i,4}=meanvalue(i);
%画图
plot([(minvalue+width*i),(minvalue+width*i)],[12.8702,16.6583],'g--','LineWidth', 1);
hold on
plot([minvalue+width*(i-1),(minvalue+width*i)],[meanvalue(i),meanvalue(i)],'r','LineWidth', 2);
hold on
%将第二部分数据加到当前figure
end
end
hold off
saveas(figure(kq),['D:\sxt\GraduatePrograms\Tieling\projects\scatter\alldata\',int2str(kq),'.png']) %把图片保存到指定目录并编号
end