質問編集履歴
2
コードの修正を行いました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,8 +3,6 @@
|
|
3
3
|
製造現場において、生産時のプロセス情報(生産設備の使用号機、生産中に製品が停止した時間)を基に
|
4
4
|
不良品が発生するかどうかの2値分類モデルを作りたいです。
|
5
5
|
|
6
|
-
Pythonを使用しています。
|
7
|
-
|
8
6
|
### 発生している問題・エラーメッセージ
|
9
7
|
|
10
8
|
不良品が発生する確率は100個中2~3個程度です。
|
@@ -32,6 +30,7 @@
|
|
32
30
|
```
|
33
31
|
|
34
32
|
### 該当のソースコード
|
33
|
+
```python
|
35
34
|
import pandas as pd
|
36
35
|
test_file = "data.csv"
|
37
36
|
test_file2 = "test_N.csv"
|
@@ -39,7 +38,7 @@
|
|
39
38
|
df_make = pd.read_csv(test_file,engine="python")
|
40
39
|
df_make.describe()
|
41
40
|
|
42
|
-
■標準化■
|
41
|
+
#■標準化■
|
43
42
|
from sklearn.preprocessing import StandardScaler
|
44
43
|
stdsc = StandardScaler()
|
45
44
|
df_make[['ラインの停止区間A',ラインの停止区間B']] = stdsc.fit_transform(df_make[['ラインの停止区間A','ラインの停止区間B']])
|
@@ -49,34 +48,33 @@
|
|
49
48
|
df_make = pd.get_dummies(df_make, columns=["使用装置"])
|
50
49
|
df_x = df_make.drop(["不良"], axis=1)
|
51
50
|
|
52
|
-
■機械学習■
|
51
|
+
#■機械学習■
|
53
52
|
from sklearn.model_selection import train_test_split
|
54
53
|
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)
|
55
54
|
|
56
|
-
|
55
|
+
#ここで不良あり/なしの個数を確認
|
57
56
|
train_y.value_counts()
|
58
57
|
|
59
58
|
|
60
|
-
■アルゴリズム選択■
|
59
|
+
#■アルゴリズム選択■
|
61
|
-
|
62
|
-
ロジスティック回帰 (このコードではこのアルゴリズムを使用)
|
60
|
+
#ロジスティック回帰 (このコードではこのアルゴリズムを使用)
|
63
61
|
from sklearn.linear_model import LogisticRegression
|
64
62
|
model = LogisticRegression(C=10)
|
65
63
|
|
66
|
-
ランダムフォレスト
|
64
|
+
#ランダムフォレスト
|
67
65
|
from sklearn.ensemble import RandomForestClassifier
|
68
66
|
model = RandomForestClassifier(random_state=random_seed)
|
69
67
|
model = RandomForestClassifier(3)
|
70
68
|
|
71
|
-
|
69
|
+
#決定木
|
72
70
|
from sklearn.tree import DecisionTreeClassifier
|
73
71
|
model = DecisionTreeClassifier(max_depth = 2)
|
74
72
|
|
75
|
-
XGBoost
|
73
|
+
#XGBoost
|
76
74
|
from xgboost import XGBClassifier
|
77
75
|
model = XGBClassifier(3)
|
78
76
|
|
79
|
-
ニューラルネットワーク
|
77
|
+
#ニューラルネットワーク
|
80
78
|
from sklearn.neural_network import MLPClassifier
|
81
79
|
model = MLPClassifier(hidden_layer_sizes=(200,200), random_state=random_seed)
|
82
80
|
|
@@ -93,7 +91,7 @@
|
|
93
91
|
plt.plot(np.array(train_y), color="black", linestyle="dotted",linewidth="0.2") #答え = 黒
|
94
92
|
|
95
93
|
|
96
|
-
■学習データで精度確認■
|
94
|
+
#■学習データで精度確認■
|
97
95
|
from sklearn.metrics import precision_recall_fscore_support
|
98
96
|
|
99
97
|
precision, recall, fscore, _ = precision_recall_fscore_support(train_y, pred, average='binary')
|
@@ -109,7 +107,7 @@
|
|
109
107
|
plt.plot(pred2, color="red") #予測線
|
110
108
|
plt.plot(np.array(test_y), color="black", linestyle="dotted",linewidth="0.2") #答え
|
111
109
|
|
112
|
-
■テストデータで精度確認■
|
110
|
+
#■テストデータで精度確認■
|
113
111
|
precision, recall, fscore, _ = precision_recall_fscore_support(test_y, pred2, average='binary')
|
114
112
|
score = model.score(test_x, test_y) #決定係数を確認する。1に近いほど精度が良い。
|
115
113
|
|
@@ -119,7 +117,7 @@
|
|
119
117
|
print(f'F値: {fscore:.4f}')
|
120
118
|
|
121
119
|
|
122
|
-
■他のデータ(1万個のデータ)の予測■
|
120
|
+
#■他のデータ(1万個のデータ)の予測■
|
123
121
|
df_make2 = pd.read_csv(test_file2,engine="python")
|
124
122
|
|
125
123
|
df_make2[['ラインの停止区間A',ラインの停止区間B']] = stdsc.fit_transform(df_make[['ラインの停止区間A','ラインの停止区間B']])
|
@@ -140,9 +138,10 @@
|
|
140
138
|
print(f'精度: {score:.4f}')
|
141
139
|
print(f'適合率: {precision:.4f}')
|
142
140
|
print(f'再現率: {recall:.4f}')
|
143
|
-
print(f'F値: {fscore:.4f}') ←ここでF値が10%ほどしか出ない
|
141
|
+
print(f'F値: {fscore:.4f}') #←ここでF値が10%ほどしか出ない
|
144
142
|
|
145
143
|
|
144
|
+
```
|
146
145
|
|
147
146
|
### 試したこと
|
148
147
|
|
1
参考でコードを入力しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -32,11 +32,118 @@
|
|
32
32
|
```
|
33
33
|
|
34
34
|
### 該当のソースコード
|
35
|
+
import pandas as pd
|
36
|
+
test_file = "data.csv"
|
37
|
+
test_file2 = "test_N.csv"
|
35
38
|
|
39
|
+
df_make = pd.read_csv(test_file,engine="python")
|
36
|
-
|
40
|
+
df_make.describe()
|
37
|
-
ソースコード
|
38
|
-
```
|
39
41
|
|
42
|
+
■標準化■
|
43
|
+
from sklearn.preprocessing import StandardScaler
|
44
|
+
stdsc = StandardScaler()
|
45
|
+
df_make[['ラインの停止区間A',ラインの停止区間B']] = stdsc.fit_transform(df_make[['ラインの停止区間A','ラインの停止区間B']])
|
46
|
+
|
47
|
+
df_y = df_make["不良"]
|
48
|
+
df_make = pd.get_dummies(df_make, columns=["製品の位置情報"])
|
49
|
+
df_make = pd.get_dummies(df_make, columns=["使用装置"])
|
50
|
+
df_x = df_make.drop(["不良"], axis=1)
|
51
|
+
|
52
|
+
■機械学習■
|
53
|
+
from sklearn.model_selection import train_test_split
|
54
|
+
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)
|
55
|
+
|
56
|
+
//ここで不良あり/なしの個数を確認
|
57
|
+
train_y.value_counts()
|
58
|
+
|
59
|
+
|
60
|
+
■アルゴリズム選択■
|
61
|
+
|
62
|
+
ロジスティック回帰 (このコードではこのアルゴリズムを使用)
|
63
|
+
from sklearn.linear_model import LogisticRegression
|
64
|
+
model = LogisticRegression(C=10)
|
65
|
+
|
66
|
+
ランダムフォレスト
|
67
|
+
from sklearn.ensemble import RandomForestClassifier
|
68
|
+
model = RandomForestClassifier(random_state=random_seed)
|
69
|
+
model = RandomForestClassifier(3)
|
70
|
+
|
71
|
+
決定木
|
72
|
+
from sklearn.tree import DecisionTreeClassifier
|
73
|
+
model = DecisionTreeClassifier(max_depth = 2)
|
74
|
+
|
75
|
+
XGBoost
|
76
|
+
from xgboost import XGBClassifier
|
77
|
+
model = XGBClassifier(3)
|
78
|
+
|
79
|
+
ニューラルネットワーク
|
80
|
+
from sklearn.neural_network import MLPClassifier
|
81
|
+
model = MLPClassifier(hidden_layer_sizes=(200,200), random_state=random_seed)
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
model.fit(train_x,train_y)
|
86
|
+
pred = model.predict(train_x)
|
87
|
+
|
88
|
+
import matplotlib.pyplot as plt
|
89
|
+
%matplotlib inline
|
90
|
+
|
91
|
+
import numpy as np
|
92
|
+
plt.plot(pred, color="red") #AIの予想 = 赤
|
93
|
+
plt.plot(np.array(train_y), color="black", linestyle="dotted",linewidth="0.2") #答え = 黒
|
94
|
+
|
95
|
+
|
96
|
+
■学習データで精度確認■
|
97
|
+
from sklearn.metrics import precision_recall_fscore_support
|
98
|
+
|
99
|
+
precision, recall, fscore, _ = precision_recall_fscore_support(train_y, pred, average='binary')
|
100
|
+
score = model.score(train_x, train_y)
|
101
|
+
|
102
|
+
print(f'精度: {score:.4f}')
|
103
|
+
print(f'適合率: {precision:.4f}')
|
104
|
+
print(f'再現率: {recall:.4f}')
|
105
|
+
print(f'F値: {fscore:.4f}')
|
106
|
+
|
107
|
+
pred2 = model.predict(test_x)
|
108
|
+
|
109
|
+
plt.plot(pred2, color="red") #予測線
|
110
|
+
plt.plot(np.array(test_y), color="black", linestyle="dotted",linewidth="0.2") #答え
|
111
|
+
|
112
|
+
■テストデータで精度確認■
|
113
|
+
precision, recall, fscore, _ = precision_recall_fscore_support(test_y, pred2, average='binary')
|
114
|
+
score = model.score(test_x, test_y) #決定係数を確認する。1に近いほど精度が良い。
|
115
|
+
|
116
|
+
print(f'精度: {score:.4f}')
|
117
|
+
print(f'適合率: {precision:.4f}')
|
118
|
+
print(f'再現率: {recall:.4f}')
|
119
|
+
print(f'F値: {fscore:.4f}')
|
120
|
+
|
121
|
+
|
122
|
+
■他のデータ(1万個のデータ)の予測■
|
123
|
+
df_make2 = pd.read_csv(test_file2,engine="python")
|
124
|
+
|
125
|
+
df_make2[['ラインの停止区間A',ラインの停止区間B']] = stdsc.fit_transform(df_make[['ラインの停止区間A','ラインの停止区間B']])
|
126
|
+
|
127
|
+
test_y2 = df_make2["不良"]
|
128
|
+
df_make2 = pd.get_dummies(df_make2, columns=["製品の位置情報"])
|
129
|
+
df_make2 = pd.get_dummies(df_make2, columns=["使用装置"])
|
130
|
+
test_x2 = df_make2.drop(["不良"], axis=1)
|
131
|
+
|
132
|
+
pred_test = model.predict(test_x2)
|
133
|
+
|
134
|
+
plt.plot(pred_test, color="red") #予測線
|
135
|
+
plt.plot(np.array(test_y2), color="black", linestyle="dotted",linewidth="0.2") #答え
|
136
|
+
|
137
|
+
precision, recall, fscore, _ = precision_recall_fscore_support(test_y2, pred_test, average='binary')
|
138
|
+
score = model.score(test_x2, test_y2)
|
139
|
+
|
140
|
+
print(f'精度: {score:.4f}')
|
141
|
+
print(f'適合率: {precision:.4f}')
|
142
|
+
print(f'再現率: {recall:.4f}')
|
143
|
+
print(f'F値: {fscore:.4f}') ←ここでF値が10%ほどしか出ない
|
144
|
+
|
145
|
+
|
146
|
+
|
40
147
|
### 試したこと
|
41
148
|
|
42
149
|
特徴量の増加:4個 → 7個
|