質問編集履歴

4

ライブラリ追加

2020/01/03 03:52

投稿

T.abe0531
T.abe0531

スコア7

test CHANGED
File without changes
test CHANGED
@@ -359,3 +359,15 @@
359
359
 
360
360
 
361
361
  ```
362
+
363
+
364
+
365
+ ### ライブラリなど
366
+
367
+ windows10
368
+
369
+ python3.6
370
+
371
+ pyxel
372
+
373
+ scikit-learn 0.22

3

エラーとコードの追加

2020/01/03 03:52

投稿

T.abe0531
T.abe0531

スコア7

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,59 @@
36
36
 
37
37
  ```エラー
38
38
 
39
- File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_base.py", line 612, in kneighbors
39
+ Image has saved correctly.
40
+
41
+ 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.
42
+
43
+ warnings.warn(message, FutureWarning)
44
+
45
+ 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.
46
+
47
+ warnings.warn(message, FutureWarning)
48
+
49
+ 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.
50
+
51
+ warnings.warn(message, FutureWarning)
52
+
53
+ 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.
54
+
55
+ UserWarning)
56
+
57
+ Traceback (most recent call last):
58
+
59
+ File "paint.py", line 87, in <module>
60
+
61
+ App()
62
+
63
+ File "paint.py", line 31, in __init__
64
+
65
+ pyxel.run(self.update, self.draw)
66
+
67
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\pyxel\app.py", line 255, in run
68
+
69
+ main_loop()
70
+
71
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\pyxel\app.py", line 247, in main_loop
72
+
73
+ self._update_frame()
74
+
75
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\pyxel\app.py", line 438, in _update_frame
76
+
77
+ self._update()
78
+
79
+ File "paint.py", line 57, in update
80
+
81
+ self.pred_digit = model.load_predict()
82
+
83
+ File "C:\Users\ユーザー名\Desktop\AI\pyxelDigitRecognition\model.py", line 43, in load_predict
84
+
85
+ pred = loaded_model.predict(img)[0] # np.array to int
86
+
87
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_classification.py", line 173, in predict
88
+
89
+ neigh_dist, neigh_ind = self.kneighbors(X)
90
+
91
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_base.py", line 580, in kneighbors
40
92
 
41
93
  n_samples_fit = self.n_samples_fit_
42
94
 
@@ -48,7 +100,7 @@
48
100
 
49
101
  コード
50
102
 
51
- ```python
103
+ ```paint.py
52
104
 
53
105
  # 3rd party
54
106
 
@@ -211,3 +263,99 @@
211
263
 
212
264
 
213
265
  ```
266
+
267
+ ```model.py
268
+
269
+ import numpy as np
270
+
271
+ import pickle
272
+
273
+ from PIL import Image
274
+
275
+ from sklearn.datasets import load_digits
276
+
277
+ from sklearn.model_selection import train_test_split
278
+
279
+ from sklearn.neighbors import KNeighborsClassifier
280
+
281
+
282
+
283
+
284
+
285
+ def train():
286
+
287
+ """Train k-nearest neighbors model with Digits dataset
288
+
289
+ """
290
+
291
+ digits = load_digits()
292
+
293
+ X = digits.data
294
+
295
+ y = digits.target
296
+
297
+
298
+
299
+ # print(X.shape) # (1797, 64)
300
+
301
+ # print(y.shape) # (1797,)
302
+
303
+
304
+
305
+ X_train,X_test,y_train,y_test = train_test_split(X, y)
306
+
307
+
308
+
309
+ knn = KNeighborsClassifier()
310
+
311
+ knn.fit(X_train, y_train)
312
+
313
+ # print(knn.score(X_test, y_test)) # 0.98 lol
314
+
315
+
316
+
317
+ # save model
318
+
319
+ with open('knn_digit.pkl', 'wb') as f:
320
+
321
+ pickle.dump(knn, f)
322
+
323
+
324
+
325
+
326
+
327
+ def load_predict() -> int:
328
+
329
+ """load a trained model and predict
330
+
331
+ """
332
+
333
+
334
+
335
+ with open('knn_digit.pkl', 'rb') as f:
336
+
337
+ loaded_model = pickle.load(f)
338
+
339
+
340
+
341
+ # Open image and extract features
342
+
343
+ img = Image.open("images/screen_shot.png")
344
+
345
+ img = img.convert('L') # convert (r,g,b) to gray scale (0-255)
346
+
347
+ img = (255 - np.array(img))//16 + 1 # convert to 0-15
348
+
349
+ img = img.reshape(1, 64)
350
+
351
+
352
+
353
+ pred = loaded_model.predict(img)[0] # np.array to int
354
+
355
+ # print("PREDICT: ", pred)
356
+
357
+ return pred
358
+
359
+
360
+
361
+ ```

2

エラー

2020/01/03 03:36

投稿

T.abe0531
T.abe0531

スコア7

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,10 @@
36
36
 
37
37
  ```エラー
38
38
 
39
+ File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\neighbors\_base.py", line 612, in kneighbors
40
+
41
+ n_samples_fit = self.n_samples_fit_
42
+
39
43
  AttributeError: 'KNeighborsClassifier' object has no attribute 'n_samples_fit_'
40
44
 
41
45
  ```

1

リンク

2020/01/03 03:00

投稿

T.abe0531
T.abe0531

スコア7

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,9 @@
16
16
 
17
17
 
18
18
 
19
+ 今回の参考サイトは
20
+
19
- 今回の参考サイトは[ペイントソフトを作って、ついでに手書き数字認識もする]です。(https://qiita.com/odanny/items/eee3d99522bb01fdd111?utm_campaign=popular_items&utm_medium=feed&utm_source=popular_items)
21
+ [ペイントソフトを作って、ついでに手書き数字認識もする](https://qiita.com/odanny/items/eee3d99522bb01fdd111?utm_campaign=popular_items&utm_medium=feed&utm_source=popular_items)
20
22
 
21
23
 
22
24