teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

5

追記

2018/05/06 13:41

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -38,4 +38,58 @@
38
38
  ```txt
39
39
  ImportError: [joblib] Attempting to do parallel computing without protecting your import on a system that does not support forking. To use parallel-computing in a script, you must protect your main loop using "if __name__ == '__main__'". Please see the joblib documentation on Parallel for more information
40
40
  ```
41
- joblibはどこに・・・
41
+ joblibはどこに・・・
42
+
43
+ ---
44
+
45
+ mkgreiさんに教えて頂いた[リンク](https://stackoverflow.com/questions/40803684/parallel-error-with-gridsearchcv-works-fine-with-other-methods)により質問文のコードを改造した処
46
+ Windows環境でもエラーは発生しないようにできました。
47
+
48
+ ```Python
49
+ import numpy as np
50
+ from sklearn import datasets
51
+ from sklearn.model_selection import GridSearchCV
52
+ from sklearn.linear_model import LogisticRegression
53
+ from sklearn.decomposition import PCA
54
+ from sklearn.svm import SVC
55
+ from sklearn.pipeline import Pipeline
56
+ from sklearn.model_selection import train_test_split
57
+ from sklearn.model_selection import RandomizedSearchCV
58
+
59
+
60
+ def main() ->None:
61
+ digits = datasets.load_digits()
62
+ X, y = digits.data, digits.target
63
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
64
+
65
+ clf1 = LogisticRegression()
66
+ clf2 = SVC()
67
+ estimators = [('pca', PCA()),
68
+ ('clf', clf1)]
69
+ pipe1 = Pipeline(estimators)
70
+ param1 = {'clf__C': [1e-5, 1e-3, 1e-2, 1, 1e2, 1e5, 1e10],
71
+ 'pca__whiten': [True, False]}
72
+ gs = GridSearchCV(pipe1, param1)
73
+ gs.fit(X_train, y_train)
74
+ print(gs.score(X_test, y_test))
75
+ estimators = [('pca', PCA()),
76
+ ('clf', SVC())]
77
+ pipe2 = Pipeline(estimators)
78
+ gamma_range_exp = np.arange(-10.0, 0.0, 3)
79
+ gamma_range = 10 ** gamma_range_exp
80
+
81
+ param2 = {'clf__C': [1e-5, 1e-3, 1e-2, 1, 1e2, 1e5, 1e10],
82
+ 'clf__kernel': ['rbf', 'linear'],
83
+ 'clf__gamma': gamma_range,
84
+ 'pca__whiten': [True, False],
85
+ 'pca__n_components': [30, 20, 10]}
86
+
87
+ print('start')
88
+ gs = RandomizedSearchCV(pipe2, param2, n_jobs=-1, verbose=2)
89
+ gs.fit(X_train, y_train)
90
+
91
+
92
+ if __name__ == '__main__':
93
+ main()
94
+
95
+ ```

4

追記

2018/05/06 13:41

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -34,7 +34,7 @@
34
34
  numpy: 1.13.3
35
35
  sklearn: 0.19.1
36
36
  OS: Windows 10
37
-
37
+ PyCharmより実行。
38
38
  ```txt
39
39
  ImportError: [joblib] Attempting to do parallel computing without protecting your import on a system that does not support forking. To use parallel-computing in a script, you must protect your main loop using "if __name__ == '__main__'". Please see the joblib documentation on Parallel for more information
40
40
  ```

3

追記

2018/05/06 12:58

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -28,4 +28,14 @@
28
28
  'pca__whiten':[True,False],
29
29
  'pca__n_components': [30, 20, 10]}
30
30
 
31
- ```
31
+ ```
32
+ ◇実行環境
33
+ python: 3.6.5
34
+ numpy: 1.13.3
35
+ sklearn: 0.19.1
36
+ OS: Windows 10
37
+
38
+ ```txt
39
+ ImportError: [joblib] Attempting to do parallel computing without protecting your import on a system that does not support forking. To use parallel-computing in a script, you must protect your main loop using "if __name__ == '__main__'". Please see the joblib documentation on Parallel for more information
40
+ ```
41
+ joblibはどこに・・・

2

追記

2018/05/06 12:55

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -17,4 +17,15 @@
17
17
 
18
18
  ```
19
19
  追記
20
- [VotingClassifierを使いつつGridSearchCV/RandomizedSearchCVでパラメータチューニング](https://qiita.com/yagays/items/a503117bd06bb938fdb9)
20
+ [VotingClassifierを使いつつGridSearchCV/RandomizedSearchCVでパラメータチューニング](https://qiita.com/yagays/items/a503117bd06bb938fdb9)
21
+
22
+ ---
23
+
24
+ ```Python
25
+ param2 ={'clf__C':[1e-5, 1e-3, 1e-2, 1, 1e2, 1e5, 1e10],
26
+ 'clf__kernel':['rbf', 'linear'],
27
+ 'clf__gamma': gamma_range,
28
+ 'pca__whiten':[True,False],
29
+ 'pca__n_components': [30, 20, 10]}
30
+
31
+ ```

1

追記

2018/05/06 11:16

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -15,4 +15,6 @@
15
15
  'pca__whiten':[True,False],
16
16
  'pca__n_components': [30, 20, 10]}
17
17
 
18
- ```
18
+ ```
19
+ 追記
20
+ [VotingClassifierを使いつつGridSearchCV/RandomizedSearchCVでパラメータチューニング](https://qiita.com/yagays/items/a503117bd06bb938fdb9)