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

質問編集履歴

4

ライブラリ追加

2020/01/03 03:52

投稿

T.abe0531
T.abe0531

スコア7

title CHANGED
File without changes
body CHANGED
@@ -178,4 +178,10 @@
178
178
  # print("PREDICT: ", pred)
179
179
  return pred
180
180
 
181
- ```
181
+ ```
182
+
183
+ ### ライブラリなど
184
+ windows10
185
+ python3.6
186
+ pyxel
187
+ scikit-learn 0.22

3

エラーとコードの追加

2020/01/03 03:52

投稿

T.abe0531
T.abe0531

スコア7

title CHANGED
File without changes
body CHANGED
@@ -17,13 +17,39 @@
17
17
  今回分からないエラーは
18
18
 
19
19
  ```エラー
20
- File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_base.py", line 612, in kneighbors
20
+ Image has saved correctly.
21
+ C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\deprecation.py:144: FutureWarning: The sklearn.neighbors.classification module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.neighbors. Anything that cannot be imported from sklearn.neighbors is now part of the private API.
22
+ warnings.warn(message, FutureWarning)
23
+ C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\deprecation.py:144: FutureWarning: The sklearn.neighbors.kd_tree module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.neighbors. Anything that cannot be imported from sklearn.neighbors is now part of the private API.
24
+ warnings.warn(message, FutureWarning)
25
+ C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\deprecation.py:144: FutureWarning: The sklearn.neighbors.dist_metrics module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.neighbors. Anything that cannot be imported from sklearn.neighbors is now part of the private API.
26
+ warnings.warn(message, FutureWarning)
27
+ C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py:318: UserWarning: Trying to unpickle estimator KNeighborsClassifier from version 0.21.3 when using version 0.22.1. This might lead to breaking code or invalid results. Use at your own risk.
28
+ UserWarning)
29
+ Traceback (most recent call last):
30
+ File "paint.py", line 87, in <module>
31
+ App()
32
+ File "paint.py", line 31, in __init__
33
+ pyxel.run(self.update, self.draw)
34
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\pyxel\app.py", line 255, in run
35
+ main_loop()
36
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\pyxel\app.py", line 247, in main_loop
37
+ self._update_frame()
38
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\pyxel\app.py", line 438, in _update_frame
39
+ self._update()
40
+ File "paint.py", line 57, in update
41
+ self.pred_digit = model.load_predict()
42
+ File "C:\Users\ユーザー名\Desktop\AI\pyxelDigitRecognition\model.py", line 43, in load_predict
43
+ pred = loaded_model.predict(img)[0] # np.array to int
44
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_classification.py", line 173, in predict
45
+ neigh_dist, neigh_ind = self.kneighbors(X)
46
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_base.py", line 580, in kneighbors
21
47
  n_samples_fit = self.n_samples_fit_
22
48
  AttributeError: 'KNeighborsClassifier' object has no attribute 'n_samples_fit_'
23
49
  ```
24
50
 
25
51
  コード
26
- ```python
52
+ ```paint.py
27
53
  # 3rd party
28
54
  import numpy as np
29
55
  from PIL import Image
@@ -104,4 +130,52 @@
104
130
 
105
131
  App()
106
132
 
133
+ ```
134
+ ```model.py
135
+ import numpy as np
136
+ import pickle
137
+ from PIL import Image
138
+ from sklearn.datasets import load_digits
139
+ from sklearn.model_selection import train_test_split
140
+ from sklearn.neighbors import KNeighborsClassifier
141
+
142
+
143
+ def train():
144
+ """Train k-nearest neighbors model with Digits dataset
145
+ """
146
+ digits = load_digits()
147
+ X = digits.data
148
+ y = digits.target
149
+
150
+ # print(X.shape) # (1797, 64)
151
+ # print(y.shape) # (1797,)
152
+
153
+ X_train,X_test,y_train,y_test = train_test_split(X, y)
154
+
155
+ knn = KNeighborsClassifier()
156
+ knn.fit(X_train, y_train)
157
+ # print(knn.score(X_test, y_test)) # 0.98 lol
158
+
159
+ # save model
160
+ with open('knn_digit.pkl', 'wb') as f:
161
+ pickle.dump(knn, f)
162
+
163
+
164
+ def load_predict() -> int:
165
+ """load a trained model and predict
166
+ """
167
+
168
+ with open('knn_digit.pkl', 'rb') as f:
169
+ loaded_model = pickle.load(f)
170
+
171
+ # Open image and extract features
172
+ img = Image.open("images/screen_shot.png")
173
+ img = img.convert('L') # convert (r,g,b) to gray scale (0-255)
174
+ img = (255 - np.array(img))//16 + 1 # convert to 0-15
175
+ img = img.reshape(1, 64)
176
+
177
+ pred = loaded_model.predict(img)[0] # np.array to int
178
+ # print("PREDICT: ", pred)
179
+ return pred
180
+
107
181
  ```

2

エラー

2020/01/03 03:36

投稿

T.abe0531
T.abe0531

スコア7

title CHANGED
File without changes
body CHANGED
@@ -17,6 +17,8 @@
17
17
  今回分からないエラーは
18
18
 
19
19
  ```エラー
20
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_base.py", line 612, in kneighbors
21
+ n_samples_fit = self.n_samples_fit_
20
22
  AttributeError: 'KNeighborsClassifier' object has no attribute 'n_samples_fit_'
21
23
  ```
22
24
 

1

リンク

2020/01/03 03:00

投稿

T.abe0531
T.abe0531

スコア7

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,8 @@
7
7
  4.数字を学習したAIがその画像を認識する
8
8
  5.結果を出力
9
9
 
10
+ 今回の参考サイトは
10
- 今回の参考サイトは[ペイントソフトを作って、ついでに手書き数字認識もする]です。(https://qiita.com/odanny/items/eee3d99522bb01fdd111?utm_campaign=popular_items&utm_medium=feed&utm_source=popular_items)
11
+ [ペイントソフトを作って、ついでに手書き数字認識もする](https://qiita.com/odanny/items/eee3d99522bb01fdd111?utm_campaign=popular_items&utm_medium=feed&utm_source=popular_items)
11
12
 
12
13
  基本的にサイトに書かれていることしかしていないので、以下のエラーが一体何なのかわからず困っています。
13
14
  K近傍法とやらのエラーっぽい感じなのはわかるのですが・・・。