矩阵权重计算(tkinter)

直接效果展示

 

                       矩阵权重计算(tkinter)_第1张图片

# -*- coding: utf-8 -*-
"""
Created on Wed Nov  6 08:56:57 2019
@author: 01
"""
import statsmodels
import tkinter.messagebox
#import webbrowser
import numpy as np
from tkinter import *
import tkinter.filedialog
from tkinter.filedialog import askopenfilename
import tkinter.font as tkFont
#import pandas as pd
#import statsmodels.api as sm
from math import sqrt
from sklearn import decomposition
root1 = tkinter.Tk()
root1.title('登录')
root1.geometry('580x600+100+50')
      #name = input("请输入用户名:")
      # pwd = input("请输入密码:")
pic = r'E:\dist/12.gif'
canvas = tkinter.Canvas(root1)
image_file = tkinter.PhotoImage(file=pic)
image = canvas.create_image(0,0,anchor='nw',image=image_file)
canvas.place(x=0,y=0,height=860, width=619)
aa = StringVar()
aa.set('用户名')
aaa = tkinter.Label(root1, textvariable=aa)
aaa.place(x=400, y=150)
      
      
name = tkinter.Entry(root1,width=12,name="ss")
name.place(x =400 , y=100,width = 150, height=30,anchor = NW)
      
       
bb = StringVar()
bb.set('登录密码')
bbb = tkinter.Label(root1, textvariable=bb)
bbb.place(x=400, y=250)  

ft = tkFont.Font(family='Fixdsys', size=10, weight=tkFont.BOLD)
ft1 = tkFont.Font(size=20, slant=tkFont.ITALIC)
ft2 = tkFont.Font(size=30, weight=tkFont.BOLD)



                
pwd =tkinter.Entry(root1,width=12)
pwd.place(x =400 , y=200,width = 150, height=30,anchor = NW)     
#登录界面===============================================
def jiunu():      
        global root1       
        a=name.get()
       
 
        words = pwd.get()
        print("a",words)
        
        while(words!="1018"): #如果密码正确,摧毁登陆界面
          messagebox.showinfo('提示','叫哥哥,哥哥给你说密码')
          messagebox.showinfo('警告',"是否安装木马病毒,请点击确定")
          messagebox.showinfo('警告',"你以为点×就能退出吗?")
          messagebox.showinfo('警告',"在点一次,就能进入")
          messagebox.showinfo('警告',"你还真点")
          tkinter.messagebox.showerror('错误','木马病毒植入成功')
          messagebox.showinfo('警告',"点击100次,可以解锁")
          i=1
          while(True):
               messagebox.showinfo('警告',"点击(%d)次,加油"%(i))
               i=i+1
               if (i==10):
                   messagebox.showinfo('警告',"看你诚意十足,饶你一命")
                   return 0
             
          return 0
        while(a!="小潘"): #如果密码正确,摧毁登陆界面
          return 0
        root1.destroy()
        def normalize(x):
	           return (x - np.min(x))/(np.max(x) - np.min(x))
        def standardize(x):
             	return (x - np.mean(x))/(np.std(x))
        def multipl(a, b):
            """
            :param a: 列表1
            :param b: 列表2
            :return: 相乘求和之后的值
            """
            sumofab = 0.0
            for i in range(len(a)):
                temp = a[i] * b[i]
                sumofab += temp
            return sumofab

        def corrcoef(x, y):
                """
                :param x: 列表1
                :param y: 列表2
                :return: 复相关系数R
                """
                n = len(x)
                # 求和
                sum1 = sum(x)
                sum2 = sum(y)
                # 求乘积之和
                sumofxy = multipl(x, y)
                # 求平方和
                sumofx2 = sum([pow(i, 2) for i in x])
                sumofy2 = sum([pow(j, 2) for j in y])
                num = sumofxy - (float(sum1) * float(sum2) / n)
                # 计算复相关系数
                den = sqrt((sumofx2 - float(sum1 ** 2) / n) * (sumofy2 - float(sum2 ** 2) / n))
                R=num / den
                return R

        def get_R_coefficient(data):
                """
                :param data: 评价指标的数据框
                :return:评价指标的复相关系数
                """
                R_list=[]
                for i in range(0,len(data.columns.values)):
                    columns_list = list(data.columns.values)
                    k1 = columns_list[i]
                    columns_list.remove(k1)
                    X = statsmodels.api.add_constant(data.loc[:,columns_list])
                    y = data.loc[:,k1]
                    est = api.OLS(y, X)
                    est2 = est.fit()
                    predict = est2.predict()
                    R=corrcoef(y, predict)
                    R_list.append(1/R)
            
                return R_list
        def choosepic():
            path_ = askopenfilename()
            file_path_text.set(path_)
            print(path_)
        def cv_imread():  
            a=file_path.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
                        }
            print(a)
            f1=open(a,"rb")
            case_train=np.loadtxt(f1,delimiter=',',skiprows=0)
            f1.close()
            case_train1=np.array(case_train)
            ## imdecode读取的是rgb,如果后续需要opencv处理的话,需要转换成bgr,转换后图片颜色会变化    ##cv_img=cv2.cvtColor(cv_img,cv2.COLOR_RGB2BGR)   
            print(case_train1)
            x,y=np.linalg.eig(case_train1)
            print(x)
            print(y)
            
            messagebox.showinfo('提示',"success,Please click the export button")
        # 创建tkinter主窗口
        def entropy_weight():  
            a=file_path.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
                        }
            print(a)
            data=pd.read_csv(a,index_col=None,header=None,encoding='utf8')
            #data = (data - data.min())/(data.max() - data.min())
            m,n=data.shape
            #第一步读取文件,如果未标准化,则标准化
            data=data.as_matrix(columns=None)
            #将dataframe格式转化为matrix格式
            k=1/np.log(m)
            yij=data.sum(axis=0)
            pij=data/yij
            #第二步,计算pij
            test=pij*np.log(pij)
            test=np.nan_to_num(test)
            ej=-k*(test.sum(axis=0))
            #计算每种指标的信息熵
            wi=(1-ej)/np.sum(1-ej)
            weight_vector_text.set(wi)
            print(wi)
        def PCAa(): 
            a=file_path.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
                        }
            print(a)
            data=pd.read_csv(a,index_col=None,header=None,encoding='utf8')
            #data = (data - data.min())/(data.max() - data.min())
            X=np.array(data)
            pca=decomposition.PCA(n_components=3)
            pca.fit(X)
            component=pca.components_
            variance_ratio=pca.explained_variance_ratio_
            component=abs(component.T)
            for i in range(0,3):
                component[:,i]=variance_ratio[i]*component[:,i]
            a=pd.DataFrame(component) 
            b=a.sum(axis=1)
            c=b/b.sum(axis=0)
            li=[]
            for a in  c:
                li.append(a)
            weight_vector_text.set(li)         
        def variation_ch():   
            a=file_path.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
                        }
            data=pd.read_csv(a,index_col=None,header=None,encoding='utf8')
            print(data)
            cha=[]
            for i in data[:][:]:
                cha.append(np.std(data[:][i],ddof=1)/np.mean(data[:][i]))
            print(cha)
            cha_vi=[]
            for i in cha:
                cha_vi.append(i/sum(cha))
            print(cha_vi)
            weight_vector_text.set(cha_vi)
        def Standard_method():   
            a=file_path.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
                        }
            data=pd.read_csv(a,index_col=None,header=None,encoding='utf8')
            print(data)
            b=normalize(data)
            print(b)
            c=standardize(b)
            print(c)
            ccc=[]
            for i in c[:][:]:               
                ccc.append(max(c[:][i])-min(c[:][i]))
            print(ccc)
            cccha_vi=[]
            for i in ccc:
                cccha_vi.append(i/sum(ccc))
            print(cccha_vi)
            weight_vector_text.set(cccha_vi)
        def Independent_coefficien():   
            a=file_path.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
                        }
            data=pd.read_csv(a,index_col=None,header=None,encoding='utf8')
            R_list=get_R_coefficient(data)
            print(R_list)
            sd=[]
            for i in R_list:
                sd.append(i/sum(R_list))
            print(sd)
            weight_vector_text.set(sd)
        def factor_analysis():  
            a=file_path.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
                        }
            data=pd.read_csv(a,index_col=None,header=None,encoding='utf8')
            print(data)
            cha=[]
            for i in data[:][:]:
                cha.append(np.std(data[:][i],ddof=1)/np.mean(data[:][i]))
            print(cha)
            cha_vi=[]
            for i in cha:
                cha_vi.append(i/sum(cha))
            print(cha_vi)
            weight_vector_text.set(cha_vi)
        def single_col():
            a= weight_vector_text.get()
            if (a==""):
                {
                      messagebox.showinfo('热豆腐系列',"路径都没选,你太心急了吧·······")
        # 创建tkinter主窗口  
        }
                
            ba=[float(score1.get()),float(score2.get()),float(score3.get()),float(score4.get()),float(score5.get())
            ,float(score6.get()),float(score7.get()),float(score8.get())
            ,float(score9.get()),float(score10.get()),float(score11.get()),
            float(score12.get()),float(score13.get()),int(score14.get()),float(score15.get()),float(score16.get())
            ,float(score17.get()),float(score18.get()),float(score19.get()),float(score20.get())
            ,float(score21.get())]
            print(ba)
            if ("[" in a):                            
                c=a.split("[")
                cc=c[1].split("]")[0]
                print(cc)
                ccc=cc.split(" ")
                sum=0
                print(len(ccc))
                for i in range(len(ccc)):
                   print("ccc[i]",ccc[i])
                   print("ba[i]",ba[i])
                   sum=float (ccc[i])*ba[i]+sum
            else:
                c=a.split("(")
                cc=c[1].split(")")[0]
                print(cc)
                ccc=cc.split(",")
                print(ccc)
                sum=0
                print(len(ccc))
                for i in range(len(ccc)):    
                   sum=float (ccc[i])*ba[i]+sum                       
            result_text.set((sum))
        def cv_imread1():  
            a=file_path.get()
            
            messagebox.showinfo('VIP',"VIP用户功能,请充值VIP解锁更多姿势")
        def click():
            webbrowser.open("https://user.qzone.qq.com/1074227276?source=namecardhoverqzone")
            
        root = tkinter.Tk()        
        root.title('图片处理')
        
        # 指定主窗口位置与大小
        root.geometry('650x800+100+50')   # width x height + widthoffset + 
        
        cbc1 = StringVar()
        cbc1.set('独立性权系数法')
        cbcc1 = tkinter.Label(root, textvariable=cbc1, font=ft2)
        cbcc1.place(x=60, y=60) 
        
        cb11 = tkinter.Button(root, text="点击计算独立性权系数法的权重", command=Independent_coefficien, width=10, height=2,bg='green',fg="white")  #德尔菲法,变异系数法,均方差法。
        cb11.place(x =190 , y=50,width = 180, height=40,anchor = NW)
        
        
        cbc = StringVar()
        cbc.set('PCA方法')
        cbcc = tkinter.Label(root, textvariable=cbc, font=ft2)
        cbcc.place(x=60, y=140) 
        
        cb1 = tkinter.Button(root, text="点击计算主成分分析权数法的权重", command=PCAa, width=10, height=2,bg='green',fg="white")  #德尔菲法,变异系数法,均方差法。
        cb1.place(x =190 , y=130,width = 180, height=40,anchor = NW)
        
        cc = StringVar()
        cc.set('层次分析法')
        
        
        
        
        
        ccc = tkinter.Label(root, textvariable=cc, font=ft2)
        ccc.place(x=60, y=220) 
        
        c1 = tkinter.Button(root, text="点击计算层次分析法的权重", command=cv_imread1, width=10, height=2,bg='blue',fg="white")  #德尔菲法,变异系数法,均方差法。
        c1.place(x =190 , y=210,width = 180, height=40,anchor = NW)
        
        cc1 = StringVar()
        cc1.set('德尔菲法')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=60, y=300)  
        c2 = tkinter.Button(root, text="点击计算德尔菲法的权重", command=cv_imread1, width=10, height=2,bg='blue',fg="white")  #德尔菲法,变异系数法,均方差法。
        c2.place(x =190 , y=290,width = 180, height=40,anchor = NW)
        cc2 = StringVar()
        cc2.set('变异系数法')
        ccc2 = tkinter.Label(root, textvariable=cc2, font=ft2)
        ccc2.place(x=60, y=380) 
        c3 = tkinter.Button(root, text="点击计算变异系数法的权重", command= variation_ch, width=10, height=2,bg='green',fg="white")  #德尔菲法,变异系数法,均方差法。
        c3.place(x =190 , y=370,width = 180, height=40,anchor = NW)
        cc3 = StringVar()
        cc3.set('均方差法')
        ccc4 = tkinter.Label(root, textvariable=cc3, font=ft2)
        ccc4.place(x=60, y=460) 
        c4 = tkinter.Button(root, text="标准离差法", command=Standard_method, width=10, height=2,bg='green',fg="white")  #德尔菲法,变异系数法,均方差法。
        c4.place(x =190 , y=450,width = 180, height=40,anchor = NW)
        
        cc4 = StringVar()
        cc4.set('熵权法')
        ccc5 = tkinter.Label(root, textvariable=cc4, font=ft2)
        ccc5.place(x=60, y=530) 
        c4 = tkinter.Button(root, text="点击计算熵权法的权重", command=entropy_weight , width=10, height=2,bg='green',fg="white")  #德尔菲法,变异系数法,均方差法。
        c4.place(x =190 , y=520,width = 180, height=40,anchor = NW)
        
        cc5 = StringVar()
        cc5.set('因子分析权数法')
        ccc6 = tkinter.Label(root, textvariable=cc5, font=ft2)
        ccc6.place(x=60, y=610) 
        c4 = tkinter.Button(root, text="点击计算因子分析权数法的权重", command= click, width=10, height=2,bg='black',fg="white")  #德尔菲法,变异系数法,均方差法。
        c4.place(x =190 , y=600,width = 180, height=40,anchor = NW)
        
        save = tkinter.Button(root, text="导入原始矩阵", command=choosepic, width=10, height=2,bg='red',fg="white")
        save.place(x =50 , y=680,width = 100, height=40,anchor = NW)
        save1 = tkinter.Button(root, text="导出矩阵权重", command=cv_imread, width=10, height=2,bg='red',fg="white")  #德尔菲法,变异系数法,均方差法。
        save1.place(x =250 , y=680,width = 100, height=40,anchor = NW)                  
        # 不允许改变窗口大小
        file_path_text = StringVar()
        path= StringVar()
        file_path = Entry(root, state='readonly', text=file_path_text)
        file_path.place(x =100,y=750,width = 250, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('路径')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=30, y=750)  
        
        weight_vector_text=StringVar()
        weight_vector = Entry(root, state='readonly', text=weight_vector_text)
        weight_vector.place(x =180,y=10,width = 450, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('生成的权重向量')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=60, y=10)  
        
        
       
        #////////////////////////计算单个成绩
        q1 = StringVar()
        score1 = Entry(root, name='readonly1', text=q1)
        score1.place(x =450,y=50,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=80) 
        q1.set("0")
        q2 = StringVar()
        score2 = Entry(root, name='readonly2', text=q2)
        score2.place(x =450,y=110,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=140) 
        q2.set("0")
        
        q3 = StringVar()
        score3 = Entry(root, name='readonly3', text=q3)
        score3.place(x =450,y=170,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=200)
        q3.set("0")
        
        
        q4 = StringVar()
        score4 = Entry(root, name='readonly4', text=q4)
        score4.place(x =450,y=230,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=260)
        q4.set("0")
        
        q5= StringVar()
        score5 = Entry(root, name='readonly5', text=q5)
        score5.place(x =450,y=290,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=320)
        q5.set("0")
        
        
        q6 = StringVar()
        score6 = Entry(root, name='readonly', text=q6)
        score6.place(x =450,y=350,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=380)
        q6.set("0")
        
        
        q7 = StringVar()
        score7 = Entry(root, name='readonly6', text=q7)
        score7.place(x =450,y=410,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=440)
        q7.set("0")
        
        q8 = StringVar()
        score8 = Entry(root, name='readonly7', text=q8)
        score8.place(x =450,y=470,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=500)
        q8.set("0")
        
        
        q9 = StringVar()
        score9 = Entry(root, name='readonly8', text=q9)
        score9.place(x =450,y=530,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=560)
        q9.set("0")
        
        
        q10 = StringVar()
        score10 = Entry(root, name='readonly9', text=q10)
        score10.place(x =450,y=590,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=468, y=620)
        q10.set("0")
        #////二列////////////////////////////////
        
        w1 = StringVar()
        score12 = Entry(root, name='readonly11', text=w1)
        score12.place(x =550,y=50,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=80) 
        w1.set("0")
        
        
        w2 = StringVar()
        score13 = Entry(root, name='readonly12', text=w2)
        score13.place(x =550,y=110,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=140) 
        w2.set("0")
        
        
        w3 = StringVar()
        score14 = Entry(root, name='readonly14', text=w3)
        score14.place(x =550,y=170,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=200)
        w3.set("0")
        
        
        w4 = StringVar()
        score15 = Entry(root, name='readonly112', text=w4)
        score15.place(x =550,y=230,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=260)
        w4.set("0")
        
        
        w5= StringVar()
        score16 = Entry(root, name='readonly124', text=w5)
        score16.place(x =550,y=290,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=320)
        w5.set("0")
        
        
        w6 = StringVar()
        score17 = Entry(root, name='readonly1445', text=w6)
        score17.place(x =550,y=350,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=380)
        w6.set("0")
        
        
        w7 = StringVar()
        score18 = Entry(root, name='fa', text=w7)
        score18.place(x =550,y=410,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=440)
        w7.set("0")
        
        
        w8 = StringVar()
        score19 = Entry(root, name='readonewly', text=w8)
        score19.place(x =550,y=470,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=500)
        w8.set("0")
        
        w9 = StringVar()
        score20 = Entry(root, name='qw', text=w9)
        score20.place(x =550,y=530,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=560)
        w9.set("0")
        
        w10 = StringVar()
        score21 = Entry(root, name='2', text=w10)
        score21.place(x =550,y=590,width = 50, height=30,anchor = NW)
        cc1 = StringVar()
        cc1.set('+')
        ccc1 = tkinter.Label(root, textvariable=cc1, font=ft2)
        ccc1.place(x=568, y=620)
        w10.set("0")
        
        
        
        qw = StringVar()
        score11 = Entry(root, name="ss", text=qw)
        score11.place(x =450,y=650,width = 150, height=30,anchor = NW)
        qw.set("0")
        
        save1 = tkinter.Button(root, text="=", command=single_col, width=10, height=2,bg='red',fg="white")  #德尔菲法,变异系数法,均方差法。
        save1.place(x =475 , y=695,width = 100, height=35,anchor = NW)         
        result_text = StringVar()
       
        result = Entry(root, state='readonly', text=result_text)
        result.place(x =450,y=750,width = 150, height=30,anchor = NW)
        
        cc1 = StringVar()
     
# 启动消息主循环
buff=Button(root1,text="登录",command=jiunu,fg="red")
buff.place(x =400 , y=300,width = 100, height=40,anchor = NW)
root1.mainloop()
 
 
 

 

你可能感兴趣的:(机器学习)