Machine_learning.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. import tkinter
  2. import webbrowser
  3. from tkinter.filedialog import askopenfilename, asksaveasfilename
  4. import tkinter.messagebox
  5. import os
  6. import chardet
  7. from tkinter.scrolledtext import ScrolledText
  8. import Learn_Numpy
  9. def Main():
  10. global top,ML,Form_List,PATH,bg,bbg,fg
  11. PATH = os.getcwd()
  12. Form_List = []
  13. ML = Learn_Numpy.Machine_Learner()
  14. top = tkinter.Tk()
  15. bg = '#FFFAFA' # 主颜色
  16. bbg = '#FFFAFA' # 按钮颜色
  17. fg = '#000000' # 文字颜色
  18. top["bg"] = bg
  19. FONT = ('黑体', 11) # 设置字体
  20. top.title('CoTan机器学习')
  21. top.resizable(width=False, height=False)
  22. top.geometry('+10+10') # 设置所在位置
  23. width_B = 13 # 标准宽度
  24. height_B = 2
  25. a_y = 0
  26. a_x = 0
  27. tkinter.Button(top, bg=bbg, fg=fg, text='导入CSV', command=Add_CSV, font=FONT, width=width_B,
  28. height=height_B).grid(column=a_x, row=a_y,
  29. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  30. tkinter.Button(top, bg=bbg, fg=fg, text='导入Py', command=Add_Python, font=FONT, width=width_B,
  31. height=height_B).grid(column=a_x + 1, row=a_y,
  32. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  33. tkinter.Button(top, bg=bbg, fg=fg, text='导出CSV', command=to_CSV, font=FONT, width=width_B,
  34. height=height_B).grid(column=a_x + 2, row=a_y,
  35. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  36. global name_Input
  37. a_y += 1
  38. tkinter.Label(top, text='表格名称:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,
  39. row=a_y) # 设置说明
  40. name_Input = tkinter.Entry(top, width=width_B)
  41. name_Input.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  42. a_y += 1
  43. tkinter.Button(top, bg=bbg, fg=fg, text='删除表格', font=FONT, width=width_B,
  44. height=height_B).grid(column=a_x, row=a_y,
  45. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  46. tkinter.Button(top, bg=bbg, fg=fg, text='查看表格', command=Show, font=FONT, width=width_B,
  47. height=height_B).grid(column=a_x + 1, row=a_y,
  48. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  49. tkinter.Button(top, bg=bbg, fg=fg, text='查看单一表格', command=Show_One, font=FONT, width=width_B,
  50. height=height_B).grid(column=a_x + 2, row=a_y,
  51. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  52. global Form_BOX, Index_BOX, Column_BOX, to_HTML_Type, Seq_Input, Code_Input,str_must
  53. a_y += 1
  54. to_HTML_Type = tkinter.IntVar() # 正,负,0
  55. lable = ['选项卡型', '可移动型', '自适应型'] # 复选框
  56. for i in range(3):
  57. tkinter.Radiobutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text=lable[i],
  58. variable=to_HTML_Type,
  59. value=i).grid(column=a_x + i, row=a_y, sticky=tkinter.W)
  60. str_must = tkinter.IntVar()
  61. a_y += 1
  62. tkinter.Label(top, text='编码方式:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,
  63. row=a_y) # 设置说明
  64. Code_Input = tkinter.Entry(top, width=width_B)
  65. Code_Input.grid(column=a_x + 1, row=a_y, sticky=tkinter.E + tkinter.W)
  66. buttom = tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg,
  67. text='字符串类型',
  68. variable=str_must)
  69. buttom.grid(column=a_x + 2, row=a_y, sticky=tkinter.W)
  70. a_y += 1
  71. tkinter.Label(top, text='CSV分隔符:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,
  72. row=a_y) # 设置说明
  73. Seq_Input = tkinter.Entry(top, width=width_B)
  74. Seq_Input.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  75. a_y += 1
  76. Form_BOX = tkinter.Listbox(top, width=width_B * 3, height=height_B * 10) # 显示符号
  77. Form_BOX.grid(column=a_x, row=a_y, columnspan=3, rowspan=10, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  78. a_x += 3
  79. tkinter.Label(top, text='', bg=bg, fg=fg, font=FONT, width=1).grid(column=a_x, row=a_y) # 设置说明
  80. a_x += 1
  81. a_y = 0
  82. tkinter.Label(top, text='【机器学习】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(column=a_x,
  83. columnspan=3,
  84. row=a_y,
  85. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  86. global ML_BOX, ML_OUT,X_OUT,Y_OUT
  87. a_y += 1
  88. X_OUT = tkinter.StringVar()
  89. Put = tkinter.Entry(top, width=width_B * 2, textvariable=X_OUT)
  90. Put.grid(column=a_x, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  91. Put['state'] = 'readonly'
  92. tkinter.Button(top, bg=bbg, fg=fg, text='选用特征集', command=set_Feature, font=FONT, width=width_B,
  93. height=height_B).grid(column=a_x + 2, row=a_y, sticky=tkinter.E + tkinter.W)
  94. Y_OUT = tkinter.StringVar()
  95. a_y += 1
  96. Put = tkinter.Entry(top, width=width_B * 2, textvariable=Y_OUT)
  97. Put.grid(column=a_x, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  98. Put['state'] = 'readonly'
  99. tkinter.Button(top, bg=bbg, fg=fg, text='选用标签集', command=set_Label, font=FONT, width=width_B,
  100. height=height_B).grid(column=a_x + 2, row=a_y, sticky=tkinter.E + tkinter.W)
  101. ML_OUT = tkinter.StringVar()
  102. a_y += 1
  103. Put = tkinter.Entry(top, width=width_B * 2, textvariable=ML_OUT)
  104. Put.grid(column=a_x, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  105. Put['state'] = 'readonly'
  106. tkinter.Button(top, bg=bbg, fg=fg, text='选用学习器', command=set_Learner, font=FONT, width=width_B,
  107. height=height_B).grid(column=a_x + 2, row=a_y, sticky=tkinter.E + tkinter.W)
  108. global Split_Input
  109. a_y += 1
  110. tkinter.Label(top, text='测试数据分割:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,
  111. row=a_y)
  112. Split_Input = tkinter.Entry(top, width=width_B * 2)
  113. Split_Input.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  114. a_y += 1
  115. ML_BOX = tkinter.Listbox(top, width=width_B * 3, height=height_B * 5)
  116. ML_BOX.grid(column=a_x, row=a_y, columnspan=3, rowspan=5, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  117. a_y += 5
  118. tkinter.Button(top, bg=bbg, fg=fg, text='导入学习器', font=FONT, width=width_B, height=height_B).grid(
  119. column=a_x, row=a_y,
  120. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  121. tkinter.Button(top, bg=bbg, fg=fg, text='查看数据', command=Show_Args, font=FONT, width=width_B, height=height_B).grid(
  122. column=a_x + 1, row=a_y,
  123. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  124. tkinter.Button(top, bg=bbg, fg=fg, text='删除学习器', command=Del_Leaner, font=FONT, width=width_B,
  125. height=height_B).grid(column=a_x + 2, row=a_y,
  126. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  127. a_y += 1
  128. tkinter.Button(top, bg=bbg, fg=fg, text='训练机器', command=Fit_Learner, font=FONT, width=width_B,
  129. height=height_B).grid(column=a_x, row=a_y,
  130. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  131. tkinter.Button(top, bg=bbg, fg=fg, text='测试机器', command=Score_Learner, font=FONT, width=width_B,
  132. height=height_B).grid(column=a_x + 1, row=a_y,
  133. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  134. tkinter.Button(top, bg=bbg, fg=fg, text='数据预测', command=Predict_Learner, font=FONT, width=width_B,
  135. height=height_B).grid(column=a_x + 2, row=a_y,
  136. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  137. a_y += 1
  138. tkinter.Button(top, bg=bbg, fg=fg, text='单一变量特征选择', command=Add_SelectKBest, font=FONT, width=width_B, height=height_B).grid(column=a_x, row=a_y,columnspan=2,
  139. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  140. tkinter.Button(top, bg=bbg, fg=fg, text='映射标准化', command=Add_Mapzoom, font=FONT, width=width_B,
  141. height=height_B).grid(column=a_x+2, row=a_y,
  142. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  143. a_y += 1
  144. tkinter.Button(top, bg=bbg, fg=fg, text='方差特征选择',command=Add_Variance, font=FONT, width=width_B, height=height_B).grid(
  145. column=a_x, row=a_y,
  146. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  147. tkinter.Button(top, bg=bbg, fg=fg, text='使用学习器筛选', command=Add_SelectFrom_Model, font=FONT, width=width_B,
  148. height=height_B).grid(column=a_x + 1, row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  149. tkinter.Button(top, bg=bbg, fg=fg, text='模糊量化标准化', command=Add_Fuzzy_quantization, font=FONT, width=width_B, height=height_B).grid(
  150. column=a_x + 2, row=a_y,
  151. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  152. a_y += 1
  153. tkinter.Button(top, bg=bbg, fg=fg, text='Z-score',command=Add_Z_Score, font=FONT, width=width_B, height=height_B).grid(
  154. column=a_x, row=a_y,
  155. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  156. tkinter.Button(top, bg=bbg, fg=fg, text='离差标准化', command=Add_MinMaxScaler, font=FONT, width=width_B, height=height_B).grid(
  157. column=a_x + 1, row=a_y,
  158. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  159. tkinter.Button(top, bg=bbg, fg=fg, text='Log变换', command=Add_LogScaler, font=FONT, width=width_B,
  160. height=height_B).grid(column=a_x + 2, row=a_y,
  161. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  162. a_y += 1
  163. tkinter.Button(top, bg=bbg, fg=fg, text='atan变换',command=Add_atanScaler, font=FONT, width=width_B, height=height_B).grid(
  164. column=a_x, row=a_y,
  165. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  166. tkinter.Button(top, bg=bbg, fg=fg, text='小数定标准化', command=Add_decimalScaler, font=FONT, width=width_B, height=height_B).grid(
  167. column=a_x + 1, row=a_y,
  168. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  169. tkinter.Button(top, bg=bbg, fg=fg, text='Sigmod变换', command=Add_sigmodScaler, font=FONT, width=width_B,
  170. height=height_B).grid(column=a_x + 2, row=a_y,
  171. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  172. a_y += 1
  173. tkinter.Button(top, bg=bbg, fg=fg, text='正则化',command=Add_Regularization, font=FONT, width=width_B, height=height_B).grid(
  174. column=a_x, row=a_y,
  175. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  176. tkinter.Button(top, bg=bbg, fg=fg, text='二值离散', command=Add_Binarizer, font=FONT, width=width_B, height=height_B).grid(
  177. column=a_x + 1, row=a_y,
  178. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  179. tkinter.Button(top, bg=bbg, fg=fg, text='多值离散', command=Add_Discretization, font=FONT, width=width_B,
  180. height=height_B).grid(column=a_x + 2, row=a_y,
  181. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  182. a_y += 1
  183. tkinter.Button(top, bg=bbg, fg=fg, text='独热编码',command=Add_OneHotEncoder, font=FONT, width=width_B, height=height_B).grid(
  184. column=a_x, row=a_y,
  185. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  186. tkinter.Button(top, bg=bbg, fg=fg, text='数字编码', command=Add_Label, font=FONT, width=width_B, height=height_B).grid(
  187. column=a_x + 1, row=a_y,
  188. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  189. tkinter.Button(top, bg=bbg, fg=fg, text='缺失填充', command=Add_Missed, font=FONT, width=width_B,
  190. height=height_B).grid(column=a_x + 2, row=a_y,
  191. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  192. a_y += 1
  193. tkinter.Button(top, bg=bbg, fg=fg, text='PCA降维',command=Add_PCA, font=FONT, width=width_B, height=height_B).grid(
  194. column=a_x, row=a_y,
  195. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  196. tkinter.Button(top, bg=bbg, fg=fg, text='RPCA降维', command=Add_RPCA, font=FONT, width=width_B, height=height_B).grid(
  197. column=a_x + 1, row=a_y,
  198. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  199. tkinter.Button(top, bg=bbg, fg=fg, text='KPCA升维', command=Add_KPCA, font=FONT, width=width_B,
  200. height=height_B).grid(column=a_x + 2, row=a_y,
  201. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  202. a_y += 1
  203. tkinter.Button(top, bg=bbg, fg=fg, text='LDA降维', command=Add_LDA, font=FONT, width=width_B, height=height_B).grid(
  204. column=a_x, row=a_y,
  205. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  206. tkinter.Button(top, bg=bbg, fg=fg, text='NMF降维', command=Add_NMF, font=FONT, width=width_B, height=height_B).grid(
  207. column=a_x+1, row=a_y,
  208. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  209. tkinter.Button(top, bg=bbg, fg=fg, text='t-SNE', command=Add_TSNE, font=FONT, width=width_B, height=height_B).grid(
  210. column=a_x+2, row=a_y,
  211. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  212. a_y += 1
  213. tkinter.Button(top, bg=bbg, fg=fg, text='线性回归', command=Add_Line, font=FONT, width=width_B, height=height_B).grid(
  214. column=a_x, row=a_y,
  215. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  216. tkinter.Button(top, bg=bbg, fg=fg, text='岭回归', command=Add_Ridge, font=FONT, width=width_B, height=height_B).grid(
  217. column=a_x + 1, row=a_y,
  218. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  219. tkinter.Button(top, bg=bbg, fg=fg, text='Lasso', command=Add_Lasso, font=FONT, width=width_B, height=height_B).grid(
  220. column=a_x + 2, row=a_y,
  221. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  222. a_y += 1
  223. tkinter.Button(top, bg=bbg, fg=fg, text='逻辑回归', command=Add_LogisticRegression, font=FONT, width=width_B,
  224. height=height_B).grid(column=a_x, row=a_y,
  225. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  226. tkinter.Button(top, bg=bbg, fg=fg, text='K邻近预测', command=Add_Knn, font=FONT, width=width_B, height=height_B).grid(
  227. column=a_x + 1, row=a_y,
  228. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  229. tkinter.Button(top, bg=bbg, fg=fg, text='K邻近分类', command=Add_Knn_Class, font=FONT, width=width_B,
  230. height=height_B).grid(column=a_x + 2, row=a_y,
  231. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  232. a_y += 1
  233. tkinter.Button(top, bg=bbg, fg=fg, text='梯度回归树回归', command=Add_GradientTree, font=FONT, width=width_B,
  234. height=height_B).grid(column=a_x, row=a_y,
  235. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  236. tkinter.Button(top, bg=bbg, fg=fg, text='决策树回归',command=Add_Tree, font=FONT, width=width_B, height=height_B).grid(
  237. column=a_x + 1, row=a_y,
  238. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  239. tkinter.Button(top, bg=bbg, fg=fg, text='决策树分类',command=Add_Tree_Class, font=FONT, width=width_B, height=height_B).grid(
  240. column=a_x + 2, row=a_y,
  241. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  242. a_y += 1
  243. tkinter.Button(top, bg=bbg, fg=fg, text='梯度回归树分类', command=Add_GradientTree_class, font=FONT, width=width_B,
  244. height=height_B).grid(column=a_x, row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  245. tkinter.Button(top, bg=bbg, fg=fg, text='随机森林回归', command=Add_Forest, font=FONT, width=width_B,
  246. height=height_B).grid(column=a_x+1, row=a_y,
  247. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  248. tkinter.Button(top, bg=bbg, fg=fg, text='随机森林分类',command=Add_Forest_class, font=FONT, width=width_B, height=height_B).grid(
  249. column=a_x + 2, row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  250. a_x += 3
  251. tkinter.Label(top, text='', bg=bg, fg=fg, font=FONT, width=1).grid(column=a_x, row=a_y) # 设置说明
  252. a_x += 1
  253. a_y = 0
  254. tkinter.Button(top, bg=bbg, fg=fg, text='多层感知机回归', command=Add_MLP, font=FONT, width=width_B,
  255. height=height_B).grid(column=a_x, row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  256. tkinter.Button(top, bg=bbg, fg=fg, text='多层感知机分类', command=Add_MLP_class, font=FONT, width=width_B,
  257. height=height_B).grid(column=a_x+1, row=a_y,
  258. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  259. tkinter.Button(top, bg=bbg, fg=fg, text='随机森林分类',command=Add_Forest_class, font=FONT, width=width_B, height=height_B).grid(
  260. column=a_x + 2, row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  261. a_y += 1
  262. tkinter.Button(top, bg=bbg, fg=fg, text='支持向量机分类:SVC', command=Add_SVC, font=FONT, width=width_B, height=height_B).grid(
  263. column=a_x, row=a_y,columnspan=2,
  264. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  265. tkinter.Button(top, bg=bbg, fg=fg, text='回归:SVR', command=Add_SVR, font=FONT, width=width_B, height=height_B).grid(
  266. column=a_x + 2, row=a_y,
  267. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  268. a_y += 1
  269. tkinter.Button(top, bg=bbg, fg=fg, text='k-means', command=Add_KMeans, font=FONT, width=width_B,
  270. height=height_B).grid(column=a_x, row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  271. tkinter.Button(top, bg=bbg, fg=fg, text='凝聚聚类', command=Add_Agglomerative, font=FONT, width=width_B,
  272. height=height_B).grid(column=a_x+1, row=a_y,
  273. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  274. tkinter.Button(top, bg=bbg, fg=fg, text='DBSCAN',command=Add_DBSCAN, font=FONT, width=width_B, height=height_B).grid(
  275. column=a_x + 2, row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N)
  276. a_x += 3
  277. tkinter.Label(top, text='', bg=bg, fg=fg, font=FONT, width=1).grid(column=a_x, row=a_y) # 设置说明
  278. a_x += 1
  279. a_y = 0
  280. tkinter.Label(top, text='【学习器配置】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(column=a_x,
  281. columnspan=3,
  282. row=a_y,
  283. sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  284. global Args_Learner
  285. a_y += 1
  286. Args_Learner = tkinter.Text(top, width=width_B * 3, height=height_B * 11)
  287. Args_Learner.grid(column=a_x, row=a_y, columnspan=3, rowspan=11,
  288. sticky=tkinter.E + tkinter.W + tkinter.N + tkinter.S)
  289. top.mainloop()
  290. def Del_Leaner():
  291. Learn = get_Learner(True)
  292. set_Learne = get_Learner(False) # 获取学习器Learner
  293. if set_Learne != Learn:
  294. ML.Del_Leaner(Learn)
  295. Update_Leaner()
  296. def Show_Args():
  297. learner = get_Learner(True)
  298. if tkinter.messagebox.askokcancel('提示', f'是否将数据生成表格。\n(可绘制成散点图对比数据)'):
  299. Dic = asksaveasfilename(title='选择保存的CSV', filetypes=[("CSV", ".csv")])
  300. else:
  301. Dic = ''
  302. Data = ML.Show_Args(learner, Dic)
  303. title = f'CoTan数据处理 查看数据:{learner}'
  304. Creat_TextSheet(f'对象:{learner}\n\n{Data[0]}\n\n\n{Data[1]}', title)
  305. Update_BOX()
  306. def get_Args_Learner():
  307. global Args_Learner
  308. return Args_Learner.get('0.0', tkinter.END)
  309. def Score_Learner():
  310. learner = get_Learner()
  311. socore = ML.Score(get_Name(False,True),get_Name(False,False), learner)
  312. tkinter.messagebox.showinfo('测试完成', f'针对测试数据评分结果为:{socore}')
  313. def Predict_Learner():
  314. learner = get_Learner()
  315. Data = ML.Predict(get_Name(False,True),learner)
  316. title = f'CoTan数据处理 学习器:{learner}'
  317. Creat_TextSheet(Data, title)
  318. Update_BOX()
  319. def Fit_Learner():
  320. learner = get_Learner()
  321. try:
  322. split = float(Split_Input.get())
  323. if split < 0 or 1 < split: raise Exception
  324. except:
  325. split = 0.3
  326. socore = ML.Fit(get_Name(False,True),get_Name(False,False), learner, Text=get_Args_Learner(), split=split)
  327. tkinter.messagebox.showinfo('训练完成', f'针对训练数据({(1 - split) * 100}%)评分结果为:{socore[0]}\n'
  328. f'针对测试数据评分({split * 100}%)结果为:{socore[1]}')
  329. def set_Feature():
  330. global X_OUT
  331. X_OUT.set(get_Name())
  332. def set_Label():
  333. global Y_OUT
  334. Y_OUT.set(get_Name())
  335. def set_Learner():
  336. global ML_OUT
  337. ML_OUT.set(get_Learner(True))
  338. def get_Learner(Type=False):
  339. global Learn_Dic, ML_BOX, ML_OUT
  340. if Type:
  341. try:
  342. return list(Learn_Dic.keys())[ML_BOX.curselection()[0]]
  343. except:
  344. # raise
  345. try:
  346. return list(Learn_Dic.keys)[0]
  347. except:
  348. return None
  349. else:
  350. try:
  351. return ML_OUT.get()
  352. except:
  353. return None
  354. def Add_DBSCAN():
  355. Add_leaner('DBSCAN')
  356. def Add_Agglomerative():
  357. Add_leaner('Agglomerative')
  358. def Add_KMeans():
  359. Add_leaner('k-means')
  360. def Add_MLP_class():
  361. Add_leaner('MLP_class')
  362. def Add_MLP():
  363. Add_leaner('MLP')
  364. def Add_SVR():
  365. Add_leaner('SVR')
  366. def Add_SVC():
  367. Add_leaner('SVC')
  368. def Add_GradientTree():
  369. Add_leaner('GradientTree')
  370. def Add_GradientTree_class():
  371. Add_leaner('GradientTree_class')
  372. # def Add_NMF():
  373. # Add_leaner('NMF')
  374. def Add_TSNE():
  375. Add_leaner('t-SNE')
  376. def Add_NMF():
  377. Add_leaner('NMF')
  378. def Add_LDA():
  379. Add_leaner('LDA')
  380. def Add_KPCA():
  381. Add_leaner('KPCA')
  382. def Add_RPCA():
  383. Add_leaner('RPCA')
  384. def Add_PCA():
  385. Add_leaner('PCA')
  386. def Add_Missed():
  387. Add_leaner('Missed')
  388. def Add_Label():
  389. Add_leaner('Label')
  390. def Add_OneHotEncoder():
  391. Add_leaner('OneHotEncoder')
  392. def Add_Discretization():
  393. Add_leaner('Discretization')
  394. def Add_Binarizer():
  395. Add_leaner('Binarizer')
  396. def Add_Regularization():
  397. Add_leaner('Regularization')
  398. def Add_Fuzzy_quantization():
  399. Add_leaner('Fuzzy_quantization')
  400. def Add_Mapzoom():
  401. Add_leaner('Mapzoom')
  402. def Add_sigmodScaler():
  403. Add_leaner('sigmodScaler')
  404. def Add_decimalScaler():
  405. Add_leaner('decimalScaler')
  406. def Add_atanScaler():
  407. Add_leaner('atanScaler')
  408. def Add_LogScaler():
  409. Add_leaner('LogScaler')
  410. def Add_MinMaxScaler():
  411. Add_leaner('MinMaxScaler')
  412. def Add_Z_Score():
  413. Add_leaner('Z-Score')
  414. def Add_Forest():
  415. Add_leaner('Forest')
  416. def Add_Forest_class():
  417. Add_leaner('Forest_class')
  418. def Add_Tree_Class():
  419. Add_leaner('Tree_class')
  420. def Add_Tree():
  421. Add_leaner('Tree')
  422. def Add_SelectKBest():
  423. Add_leaner('SelectKBest')
  424. def Add_Knn_Class():
  425. Add_leaner('Knn_class')
  426. def Add_LogisticRegression():
  427. Add_leaner('LogisticRegression')
  428. def Add_Lasso():
  429. Add_leaner('Lasso')
  430. def Add_Variance():
  431. Add_leaner('Variance')
  432. def Add_Knn():
  433. Add_leaner('Knn')
  434. def Add_Ridge():
  435. Add_leaner('Ridge')
  436. def Add_Line():
  437. Add_leaner('Line')
  438. def Add_SelectFrom_Model(): # 添加Lenear的核心
  439. ML.Add_SelectFrom_Model(get_Learner(), Text=get_Args_Learner())
  440. Update_Leaner()
  441. def Add_leaner(Type): # 添加Lenear的核心
  442. ML.Add_Learner(Type, Text=get_Args_Learner())
  443. Update_Leaner()
  444. def Update_Leaner():
  445. global Learn_Dic, ML_BOX
  446. Learn_Dic = ML.Return_Learner()
  447. ML_BOX.delete(0, tkinter.END)
  448. ML_BOX.insert(tkinter.END, *Learn_Dic.keys())
  449. def Show_One():
  450. global PATH, to_HTML_Type
  451. Dic = f'{PATH}/$Show_Sheet.html'
  452. try:
  453. name = get_Name()
  454. if name == None: raise Exception
  455. ML.to_Html_One(name, Dic)
  456. webbrowser.open(Dic)
  457. except:
  458. # pass
  459. raise
  460. def Show():
  461. global PATH, to_HTML_Type
  462. Dic = f'{PATH}/$Show_Sheet.html'
  463. try:
  464. name = get_Name()
  465. if name == None: raise Exception
  466. ML.to_Html(name, Dic, to_HTML_Type.get())
  467. webbrowser.open(Dic)
  468. except:
  469. pass
  470. def to_CSV():
  471. global top, Seq_Input, Code_Input, str_must, Index_must
  472. Dic = asksaveasfilename(title='选择保存的CSV', filetypes=[("CSV", ".csv")])
  473. Seq = Seq_Input.get()
  474. name = get_Name()
  475. ML.to_CSV(Dic, name, Seq)
  476. Update_BOX()
  477. def Add_CSV():
  478. global top, Seq_Input, Code_Input, str_must, name_Input
  479. Dic = askopenfilename(title='选择载入的CSV', filetypes=[("CSV", ".csv")])
  480. if Dic == '': return False
  481. Seq = Seq_Input.get()
  482. Codeing = Code_Input.get()
  483. str_ = bool(str_must.get())
  484. name = name_Input.get().replace(' ', '')
  485. if name == '':
  486. name = os.path.splitext(os.path.split(Dic)[1])[0]
  487. print(name)
  488. if Codeing == '':
  489. with open(Dic, 'rb') as f:
  490. Codeing = chardet.detect(f.read())['encoding']
  491. if Seq == '': Seq = ','
  492. ML.read_csv(Dic, name, Codeing, str_, Seq,)
  493. Update_BOX()
  494. def Add_Python():
  495. global top, Seq_Input, Code_Input, str_must, Index_must
  496. Dic = askopenfilename(title='选择载入的py', filetypes=[("Python", ".py"), ("Txt", ".txt")])
  497. name = name_Input.get().replace(' ', '')
  498. if name == '':
  499. name = os.path.splitext(os.path.split(Dic)[1])[0]
  500. with open(Dic, 'r') as f:
  501. ML.Add_Python(f.read(), name)
  502. Update_BOX()
  503. def get_Name(Type=True, x=True): # 获得名字统一接口
  504. global Form_List, Form_BOX, X_OUT
  505. if Type:
  506. try:
  507. return Form_List[Form_BOX.curselection()[0]]
  508. except:
  509. try:
  510. return Form_List[0]
  511. except:
  512. return None
  513. else:
  514. try:
  515. if x:
  516. return X_OUT.get()
  517. else:
  518. return Y_OUT.get()
  519. except:
  520. return None
  521. def Update_BOX():
  522. global top, Form_BOX, Form_List
  523. Form_List = list(ML.get_Form().keys())
  524. Form_BOX.delete(0, tkinter.END)
  525. Form_BOX.insert(tkinter.END, *Form_List)
  526. def Creat_TextSheet(data, name):
  527. global bg
  528. new_top = tkinter.Toplevel(bg=bg)
  529. new_top.title(name)
  530. new_top.geometry('+10+10') # 设置所在位置
  531. text = ScrolledText(new_top, font=('黑体', 13), height=50)
  532. text.pack(fill=tkinter.BOTH)
  533. text.insert('0.0', data)
  534. text.config(state=tkinter.DISABLED)
  535. new_top.resizable(width=False, height=False)
  536. if __name__ == '__main__':
  537. Main()