質問編集履歴

2

コードの修正を行いました。

2020/11/23 02:32

投稿

python01
python01

スコア11

test CHANGED
File without changes
test CHANGED
@@ -8,10 +8,6 @@
8
8
 
9
9
 
10
10
 
11
- Pythonを使用しています。
12
-
13
-
14
-
15
11
  ### 発生している問題・エラーメッセージ
16
12
 
17
13
 
@@ -66,6 +62,8 @@
66
62
 
67
63
  ### 該当のソースコード
68
64
 
65
+ ```python
66
+
69
67
  import pandas as pd
70
68
 
71
69
  test_file = "data.csv"
@@ -80,7 +78,7 @@
80
78
 
81
79
 
82
80
 
83
- ■標準化■
81
+ #■標準化■
84
82
 
85
83
  from sklearn.preprocessing import StandardScaler
86
84
 
@@ -100,7 +98,7 @@
100
98
 
101
99
 
102
100
 
103
- ■機械学習■
101
+ #■機械学習■
104
102
 
105
103
  from sklearn.model_selection import train_test_split
106
104
 
@@ -108,7 +106,7 @@
108
106
 
109
107
 
110
108
 
111
- //ここで不良あり/なしの個数を確認
109
+ #ここで不良あり/なしの個数を確認
112
110
 
113
111
  train_y.value_counts()
114
112
 
@@ -116,11 +114,9 @@
116
114
 
117
115
 
118
116
 
119
- ■アルゴリズム選択■
117
+ #■アルゴリズム選択■
120
-
121
-
122
-
118
+
123
- ロジスティック回帰 (このコードではこのアルゴリズムを使用)
119
+ #ロジスティック回帰 (このコードではこのアルゴリズムを使用)
124
120
 
125
121
  from sklearn.linear_model import LogisticRegression
126
122
 
@@ -128,7 +124,7 @@
128
124
 
129
125
 
130
126
 
131
- ランダムフォレスト
127
+ #ランダムフォレスト
132
128
 
133
129
  from sklearn.ensemble import RandomForestClassifier
134
130
 
@@ -138,7 +134,7 @@
138
134
 
139
135
 
140
136
 
141
- 決定木
137
+ #決定木
142
138
 
143
139
  from sklearn.tree import DecisionTreeClassifier
144
140
 
@@ -146,7 +142,7 @@
146
142
 
147
143
 
148
144
 
149
- XGBoost
145
+ #XGBoost
150
146
 
151
147
  from xgboost import XGBClassifier
152
148
 
@@ -154,7 +150,7 @@
154
150
 
155
151
 
156
152
 
157
- ニューラルネットワーク
153
+ #ニューラルネットワーク
158
154
 
159
155
  from sklearn.neural_network import MLPClassifier
160
156
 
@@ -188,7 +184,7 @@
188
184
 
189
185
 
190
186
 
191
- ■学習データで精度確認■
187
+ #■学習データで精度確認■
192
188
 
193
189
  from sklearn.metrics import precision_recall_fscore_support
194
190
 
@@ -220,7 +216,7 @@
220
216
 
221
217
 
222
218
 
223
- ■テストデータで精度確認■
219
+ #■テストデータで精度確認■
224
220
 
225
221
  precision, recall, fscore, _ = precision_recall_fscore_support(test_y, pred2, average='binary')
226
222
 
@@ -240,7 +236,7 @@
240
236
 
241
237
 
242
238
 
243
- ■他のデータ(1万個のデータ)の予測■
239
+ #■他のデータ(1万個のデータ)の予測■
244
240
 
245
241
  df_make2 = pd.read_csv(test_file2,engine="python")
246
242
 
@@ -282,11 +278,13 @@
282
278
 
283
279
  print(f'再現率: {recall:.4f}')
284
280
 
285
- print(f'F値: {fscore:.4f}') ←ここでF値が10%ほどしか出ない
281
+ print(f'F値: {fscore:.4f}') #←ここでF値が10%ほどしか出ない
282
+
283
+
284
+
285
+
286
+
286
-
287
+ ```
287
-
288
-
289
-
290
288
 
291
289
 
292
290
 

1

参考でコードを入力しました。

2020/11/23 02:32

投稿

python01
python01

スコア11

test CHANGED
File without changes
test CHANGED
@@ -66,13 +66,227 @@
66
66
 
67
67
  ### 該当のソースコード
68
68
 
69
-
69
+ import pandas as pd
70
+
70
-
71
+ test_file = "data.csv"
72
+
73
+ test_file2 = "test_N.csv"
74
+
75
+
76
+
77
+ df_make = pd.read_csv(test_file,engine="python")
78
+
79
+ df_make.describe()
80
+
81
+
82
+
83
+ ■標準化■
84
+
85
+ from sklearn.preprocessing import StandardScaler
86
+
87
+ stdsc = StandardScaler()
88
+
89
+ df_make[['ラインの停止区間A',ラインの停止区間B']] = stdsc.fit_transform(df_make[['ラインの停止区間A','ラインの停止区間B']])
90
+
91
+
92
+
93
+ df_y = df_make["不良"]
94
+
95
+ df_make = pd.get_dummies(df_make, columns=["製品の位置情報"])
96
+
97
+ df_make = pd.get_dummies(df_make, columns=["使用装置"])
98
+
99
+ df_x = df_make.drop(["不良"], axis=1)
100
+
101
+
102
+
103
+ ■機械学習■
104
+
105
+ from sklearn.model_selection import train_test_split
106
+
107
+ train_x, test_x, train_y, test_y = train_test_split(df_x, df_y,stratify = df_y, test_size = 0.9, random_state=0)
108
+
109
+
110
+
111
+ //ここで不良あり/なしの個数を確認
112
+
113
+ train_y.value_counts()
114
+
115
+
116
+
117
+
118
+
119
+ ■アルゴリズム選択■
120
+
121
+
122
+
123
+ ロジスティック回帰 (このコードではこのアルゴリズムを使用)
124
+
125
+ from sklearn.linear_model import LogisticRegression
126
+
127
+ model = LogisticRegression(C=10)
128
+
129
+
130
+
131
+ ランダムフォレスト
132
+
133
+ from sklearn.ensemble import RandomForestClassifier
134
+
135
+ model = RandomForestClassifier(random_state=random_seed)
136
+
137
+ model = RandomForestClassifier(3)
138
+
139
+
140
+
141
+ 決定木
142
+
143
+ from sklearn.tree import DecisionTreeClassifier
144
+
145
+ model = DecisionTreeClassifier(max_depth = 2)
146
+
147
+
148
+
149
+ XGBoost
150
+
151
+ from xgboost import XGBClassifier
152
+
153
+ model = XGBClassifier(3)
154
+
155
+
156
+
157
+ ニューラルネットワーク
158
+
159
+ from sklearn.neural_network import MLPClassifier
160
+
161
+ model = MLPClassifier(hidden_layer_sizes=(200,200), random_state=random_seed)
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+ model.fit(train_x,train_y)
170
+
171
+ pred = model.predict(train_x)
172
+
173
+
174
+
175
+ import matplotlib.pyplot as plt
176
+
177
+ %matplotlib inline
178
+
179
+
180
+
181
+ import numpy as np
182
+
183
+ plt.plot(pred, color="red") #AIの予想 = 赤
184
+
185
+ plt.plot(np.array(train_y), color="black", linestyle="dotted",linewidth="0.2") #答え = 黒
186
+
187
+
188
+
189
+
190
+
71
- ```ここに言語名を入力
191
+ ■学習データで精度確認■
192
+
72
-
193
+ from sklearn.metrics import precision_recall_fscore_support
194
+
195
+
196
+
197
+ precision, recall, fscore, _ = precision_recall_fscore_support(train_y, pred, average='binary')
198
+
199
+ score = model.score(train_x, train_y)
200
+
201
+
202
+
203
+ print(f'精度: {score:.4f}')
204
+
205
+ print(f'適合率: {precision:.4f}')
206
+
207
+ print(f'再現率: {recall:.4f}')
208
+
209
+ print(f'F値: {fscore:.4f}')
210
+
211
+
212
+
213
+ pred2 = model.predict(test_x)
214
+
215
+
216
+
217
+ plt.plot(pred2, color="red") #予測線
218
+
219
+ plt.plot(np.array(test_y), color="black", linestyle="dotted",linewidth="0.2") #答え
220
+
221
+
222
+
73
- ソー
223
+ ■テトデタで精度確認■
224
+
74
-
225
+ precision, recall, fscore, _ = precision_recall_fscore_support(test_y, pred2, average='binary')
226
+
227
+ score = model.score(test_x, test_y) #決定係数を確認する。1に近いほど精度が良い。
228
+
229
+
230
+
231
+ print(f'精度: {score:.4f}')
232
+
233
+ print(f'適合率: {precision:.4f}')
234
+
235
+ print(f'再現率: {recall:.4f}')
236
+
237
+ print(f'F値: {fscore:.4f}')
238
+
239
+
240
+
241
+
242
+
75
- ```
243
+ ■他のデータ(1万個のデータ)の予測■
244
+
245
+ df_make2 = pd.read_csv(test_file2,engine="python")
246
+
247
+
248
+
249
+ df_make2[['ラインの停止区間A',ラインの停止区間B']] = stdsc.fit_transform(df_make[['ラインの停止区間A','ラインの停止区間B']])
250
+
251
+
252
+
253
+ test_y2 = df_make2["不良"]
254
+
255
+ df_make2 = pd.get_dummies(df_make2, columns=["製品の位置情報"])
256
+
257
+ df_make2 = pd.get_dummies(df_make2, columns=["使用装置"])
258
+
259
+ test_x2 = df_make2.drop(["不良"], axis=1)
260
+
261
+
262
+
263
+ pred_test = model.predict(test_x2)
264
+
265
+
266
+
267
+ plt.plot(pred_test, color="red") #予測線
268
+
269
+ plt.plot(np.array(test_y2), color="black", linestyle="dotted",linewidth="0.2") #答え
270
+
271
+
272
+
273
+ precision, recall, fscore, _ = precision_recall_fscore_support(test_y2, pred_test, average='binary')
274
+
275
+ score = model.score(test_x2, test_y2)
276
+
277
+
278
+
279
+ print(f'精度: {score:.4f}')
280
+
281
+ print(f'適合率: {precision:.4f}')
282
+
283
+ print(f'再現率: {recall:.4f}')
284
+
285
+ print(f'F値: {fscore:.4f}') ←ここでF値が10%ほどしか出ない
286
+
287
+
288
+
289
+
76
290
 
77
291
 
78
292