質問編集履歴

1

codeボタン使用してcodeいれる

2020/05/30 13:01

投稿

masayam4
masayam4

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,275 @@
1
+ ```python
2
+
3
+
4
+
5
+
6
+
7
+ import sys
8
+
9
+ import tkinter as tk
10
+
11
+ import tkinter.filedialog as tkfile
12
+
13
+ from tkinter import filedialog
14
+
15
+ from tkinter import ttk
16
+
17
+ import pandas as pd
18
+
19
+ from sklearn.model_selection import train_test_split
20
+
21
+ import os, tkinter, tkinter.filedialog, tkinter.messagebox
22
+
23
+
24
+
25
+
26
+
27
+ class Application(tk.Frame):
28
+
29
+ def __init__(self, master):
30
+
31
+ super().__init__(master)
32
+
33
+ self.master = master
34
+
35
+ self.master.title('ana_ver2')
36
+
37
+ self.pack()
38
+
39
+ self.create_widgets() #widget作る時に必要コード
40
+
41
+
42
+
43
+
44
+
45
+ def create_widgets(self):
46
+
47
+ self.input_box = tkinter.Entry(width=40)
48
+
49
+ self.input_box.place(x=10, y=100)
50
+
51
+
52
+
53
+ #ラベルの作成
54
+
55
+ self.input_label = tkinter.Label(text="結果")
56
+
57
+ self.input_label.place(x=10, y=70)
58
+
59
+
60
+
61
+ #ボタンの作成
62
+
63
+ self.button = tkinter.Button(text="参照",command=file_select)
64
+
65
+ self.button.place(x=10, y=130)
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+ self.button2 = tkinter.Button(text="ana",command=ana_do)
76
+
77
+ self.button2.place(x=10, y=160)
78
+
79
+
80
+
81
+ self.input_box2 = tkinter.Entry(width=40)
82
+
83
+ self.input_box2.place(x=10, y=180)
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+ #ボタンがクリックされたら実行
94
+
95
+ def file_select():
96
+
97
+ idir = 'C:\python_test' #初期フォルダ
98
+
99
+ filetype = [("","*")] #拡張子の選択
100
+
101
+ file_path = tkinter.filedialog.askopenfilename(filetypes = filetype, initialdir = idir)
102
+
103
+ app.input_box.insert(tkinter.END, file_path) #結果を表示
104
+
105
+
106
+
107
+ ana_do(file_path)
108
+
109
+
110
+
111
+
112
+
113
+ def ana_do(FP):
114
+
115
+
116
+
117
+ df_a = pd.read_csv(FP)
118
+
119
+
120
+
121
+
122
+
123
+ del df_a['default']
124
+
125
+ del df_a['id']
126
+
127
+
128
+
129
+ #文字列のカラムを数値に置き換えて最終的にfloat変換(自動化完了)
130
+
131
+ #------------------------------------------------------------------------------
132
+
133
+ col_name=df_a.columns#カラム名取得
134
+
135
+
136
+
137
+ #カラム名数処理繰り返し
138
+
139
+ for i in range(len(col_name)):
140
+
141
+
142
+
143
+ #カラムobjectの時の処理  要素抽出→置き換え
144
+
145
+ if df_a[col_name[i]].dtypes==object:
146
+
147
+ list_a = df_a[col_name[i]].unique()
148
+
149
+
150
+
151
+ for u in range(len(list_a)):
152
+
153
+ df_a[col_name[i]] = df_a[col_name[i]].replace(list_a[u], str(u))
154
+
155
+
156
+
157
+ #カラム毎にfloat変換
158
+
159
+ df_a[col_name[i]] =df_a[col_name[i]].astype(float)
160
+
161
+
162
+
163
+ #------------------------------------------------------------------------------
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+ X =df_a[["age","job","marital","education","balance","housing","loan","contact","duration","campaign","pdays","previous","poutcome"]]
172
+
173
+ y =df_a["y"]
174
+
175
+
176
+
177
+
178
+
179
+ #学習データとtestデータに分ける
180
+
181
+ from sklearn.model_selection import train_test_split
182
+
183
+ # train_test_splitを使って、データセットをトレーニングデータとテストデータに分割する。 7:3
184
+
185
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=0)
186
+
187
+
188
+
189
+ #線形回帰モデル読み込み
190
+
191
+ from sklearn.linear_model import LinearRegression
192
+
193
+ lr=LinearRegression()
194
+
195
+
196
+
197
+ lr.fit(X_train,y_train)
198
+
199
+ lr.predict(X_test)
200
+
201
+
202
+
203
+ #print(lr.coef_)#重み
204
+
205
+ #print(lr.score(X_train,y_train)) #決定係数 0.22全然だめ
206
+
207
+ #print(lr.score(X_test,y_test)) #決定係数
208
+
209
+
210
+
211
+
212
+
213
+ #モデル読み込み
214
+
215
+ from sklearn.linear_model import LogisticRegression
216
+
217
+ Lr=LogisticRegression()
218
+
219
+
220
+
221
+ #モデルに適合
222
+
223
+ Lr.fit(X_train,y_train)
224
+
225
+ #テストデータ予測
226
+
227
+ y_a =Lr.predict(X_test)
228
+
229
+
230
+
231
+ #誤判定件数確認
232
+
233
+ from sklearn.metrics import confusion_matrix
234
+
235
+ #print(confusion_matrix(y_test,y))
236
+
237
+
238
+
239
+ out_1 = ('ロジスティック回帰 誤分類の個数:', (y_a != y_test).sum())
240
+
241
+
242
+
243
+ #正答率を確認
244
+
245
+ from sklearn.metrics import accuracy_score
246
+
247
+ out_2 = ('ロジスティック回帰 正答率:' , accuracy_score(y_test,y_a))
248
+
249
+
250
+
251
+ app.input_box2.insert(tkinter.END, out_1)
252
+
253
+
254
+
255
+
256
+
257
+ root = tk.Tk()
258
+
259
+ root.title(u"ana_ver3")
260
+
261
+ root.geometry("500x600")
262
+
263
+ app = Application(master=root)
264
+
265
+ app.mainloop()
266
+
267
+
268
+
269
+ ```
270
+
271
+
272
+
1
273
  tkinterにて ボタン1 → 該当csvログ取得 → ログを渡す → ボタン2 → csvログ読み込み、解析処理
2
274
 
3
275
  と動くコードを書きたいと思っているのですが、うまくいっておらず困っております。
@@ -7,101 +279,3 @@
7
279
  ログを渡したら自動で動いてしまうのかなと思ってはいるのですが、直し方が分かりません。
8
280
 
9
281
  素人な質問で申し訳ありませんが分かる方助けて頂きたいです。
10
-
11
-
12
-
13
-
14
-
15
- class Application(tk.Frame):
16
-
17
- def __init__(self, master):
18
-
19
- super().__init__(master)
20
-
21
- self.master = master
22
-
23
- self.master.title('ana_ver2')
24
-
25
- self.pack()
26
-
27
- self.create_widgets()
28
-
29
-
30
-
31
-
32
-
33
- def create_widgets(self):
34
-
35
- self.input_box = tkinter.Entry(width=40)
36
-
37
- self.input_box.place(x=10, y=100)
38
-
39
-
40
-
41
- self.input_label = tkinter.Label(text="結果")
42
-
43
- self.input_label.place(x=10, y=70)
44
-
45
-
46
-
47
- self.button = tkinter.Button(text="参照",command=file_select)
48
-
49
- self.button.place(x=10, y=130)
50
-
51
-
52
-
53
- self.button2 = tkinter.Button(text="ana",command=ana_do)
54
-
55
- self.button2.place(x=10, y=160)
56
-
57
-
58
-
59
- self.input_box2 = tkinter.Entry(width=40)
60
-
61
- self.input_box2.place(x=10, y=180)
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
- def file_select():
72
-
73
- idir = 'C:\python_test' #初期フォルダ
74
-
75
- filetype = [("","*")] #拡張子の選択
76
-
77
- file_path = tkinter.filedialog.askopenfilename(filetypes = filetype, initialdir = idir)
78
-
79
- app.input_box.insert(tkinter.END, file_path) #結果を表示
80
-
81
-
82
-
83
- ana_do(file_path)
84
-
85
-
86
-
87
-
88
-
89
- def ana_do(FP):
90
-
91
-
92
-
93
- df_a = pd.read_csv(FP)
94
-
95
- ---------解析処理-----------------
96
-
97
-
98
-
99
- root = tk.Tk()
100
-
101
- root.title(u"ana_ver3")
102
-
103
- root.geometry("500x600")
104
-
105
- app = Application(master=root)
106
-
107
- app.mainloop()