{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"* Cifar-10","metadata":{"_uuid":"71d2d91efb467e10a5e98ed4d1c6594c47711e08"}},{"cell_type":"code","source":"# ! pip install py7zr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:22:52.083963Z","iopub.execute_input":"2026-01-06T08:22:52.084537Z","iopub.status.idle":"2026-01-06T08:22:52.101817Z","shell.execute_reply.started":"2026-01-06T08:22:52.084509Z","shell.execute_reply":"2026-01-06T08:22:52.100441Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! pip install py7zr==0.20.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:22:52.103074Z","iopub.execute_input":"2026-01-06T08:22:52.103503Z","iopub.status.idle":"2026-01-06T08:23:00.973516Z","shell.execute_reply.started":"2026-01-06T08:22:52.103469Z","shell.execute_reply":"2026-01-06T08:23:00.972309Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\nfrom PIL import Image\nimport io","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:23:00.974872Z","iopub.execute_input":"2026-01-06T08:23:00.975225Z","iopub.status.idle":"2026-01-06T08:23:01.305012Z","shell.execute_reply.started":"2026-01-06T08:23:00.975186Z","shell.execute_reply":"2026-01-06T08:23:01.303485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os.path\nimport sys\nimport re\nimport itertools\nimport csv\nimport datetime\nimport pickle\nfrom collections import defaultdict, Counter\nimport random\nimport gc\nimport zipfile\nimport warnings\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.ticker import NullFormatter\nimport seaborn as sns\nimport pandas as pd\nimport numpy as np\nimport scipy\nimport gensim\n# from scipy.sparse import linalg\n# from sklearn.metrics import f1_score, classification_report, confusion_matrix\n# from sklearn.model_selection import train_test_split\n# from sklearn.metrics import pairwise\n# from sklearn.metrics import f1_score, classification_report, confusion_matrix\n# from sklearn import decomposition\n# from sklearn import mixture\n# from sklearn import preprocessing\n# from sklearn import cluster\n# from sklearn import manifold\n# import networkx as nx","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:23:01.306218Z","iopub.execute_input":"2026-01-06T08:23:01.306903Z","iopub.status.idle":"2026-01-06T08:23:26.557474Z","shell.execute_reply.started":"2026-01-06T08:23:01.306866Z","shell.execute_reply":"2026-01-06T08:23:26.556524Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ! pip install japanize-matplotlib\n# ! pip install pyvis","metadata":{"trusted":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2026-01-06T08:23:26.558391Z","iopub.execute_input":"2026-01-06T08:23:26.559378Z","iopub.status.idle":"2026-01-06T08:23:26.564198Z","shell.execute_reply.started":"2026-01-06T08:23:26.559344Z","shell.execute_reply":"2026-01-06T08:23:26.563087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from pyvis.network import Network\n# import japanize_matplotlib\n# plt.rcParams['font.family']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:23:26.567036Z","iopub.execute_input":"2026-01-06T08:23:26.567873Z","iopub.status.idle":"2026-01-06T08:23:26.584963Z","shell.execute_reply.started":"2026-01-06T08:23:26.567847Z","shell.execute_reply":"2026-01-06T08:23:26.583843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -la ../input","metadata":{"trusted":true,"_uuid":"0647fd23a18f9410198e5c5ac857df88cf9dd876","execution":{"iopub.status.busy":"2026-01-06T08:23:26.586010Z","iopub.execute_input":"2026-01-06T08:23:26.586717Z","iopub.status.idle":"2026-01-06T08:23:26.744683Z","shell.execute_reply.started":"2026-01-06T08:23:26.586690Z","shell.execute_reply":"2026-01-06T08:23:26.743497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import datetime\nnow = datetime.datetime.now()\nprint(now)","metadata":{"trusted":true,"_uuid":"1226d9f0d5c79cabd74bc7a166b8c60e234dd4b2","execution":{"iopub.status.busy":"2026-01-06T08:23:26.746068Z","iopub.execute_input":"2026-01-06T08:23:26.746344Z","iopub.status.idle":"2026-01-06T08:23:26.753644Z","shell.execute_reply.started":"2026-01-06T08:23:26.746315Z","shell.execute_reply":"2026-01-06T08:23:26.752427Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## cifar-10","metadata":{"_uuid":"509ea07dec78cc1e5d81a9d656bf57fa75a10b2e"}},{"cell_type":"code","source":"train_7z_file = \"/kaggle/input/cifar-10/train.7z\"\ntest_7z_file = \"/kaggle/input/cifar-10/test.7z\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:23:26.754863Z","iopub.execute_input":"2026-01-06T08:23:26.755214Z","iopub.status.idle":"2026-01-06T08:23:26.774438Z","shell.execute_reply.started":"2026-01-06T08:23:26.755185Z","shell.execute_reply":"2026-01-06T08:23:26.773316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nwith py7zr.SevenZipFile(train_7z_file, 'r') as archive:\n    # 1. 全ファイルをメモリ上の辞書として展開 {'filename': BytesIO}\n    # ※0.20.0以降はこの挙動で安定しています\n    all_data = archive.readall()\n    \n    # 2. ファイル名を取得して数値順にソート\n    train_files = [n for n in all_data.keys() if n.endswith('.png')]\n    train_files.sort(key=lambda s: int(s.split('/')[-1].split('.')[0]))\n    \n    # 3. リストに格納\n    trainImg_list = []\n    for name in train_files:\n        bio = all_data[name]\n        with Image.open(bio) as img:\n            trainImg_list.append(np.asarray(img))\n\ntrain_files[:5]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:23:26.775586Z","iopub.execute_input":"2026-01-06T08:23:26.775900Z","iopub.status.idle":"2026-01-06T08:24:13.146181Z","shell.execute_reply.started":"2026-01-06T08:23:26.775878Z","shell.execute_reply":"2026-01-06T08:24:13.144950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train0 = np.stack(trainImg_list)\nx_train0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:24:13.147194Z","iopub.execute_input":"2026-01-06T08:24:13.147516Z","iopub.status.idle":"2026-01-06T08:24:13.293371Z","shell.execute_reply.started":"2026-01-06T08:24:13.147489Z","shell.execute_reply":"2026-01-06T08:24:13.292360Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.save('x_train_cifar10.npy', x_train0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:24:13.294463Z","iopub.execute_input":"2026-01-06T08:24:13.294792Z","iopub.status.idle":"2026-01-06T08:24:13.386718Z","shell.execute_reply.started":"2026-01-06T08:24:13.294766Z","shell.execute_reply":"2026-01-06T08:24:13.385640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train_files = pd.DataFrame(train_files)\ndf_train_files.to_csv('train_files.csv')\ndf_train_files","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:24:13.387822Z","iopub.execute_input":"2026-01-06T08:24:13.388050Z","iopub.status.idle":"2026-01-06T08:24:13.490919Z","shell.execute_reply.started":"2026-01-06T08:24:13.388031Z","shell.execute_reply":"2026-01-06T08:24:13.490063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nwith py7zr.SevenZipFile(test_7z_file, 'r') as archive:\n    # 1. 全ファイルをメモリ上の辞書として展開 {'filename': BytesIO}\n    # ※0.20.0以降はこの挙動で安定しています\n    all_data = archive.readall()\n    \n    # 2. ファイル名を取得して数値順にソート\n    test_files = [n for n in all_data.keys() if n.endswith('.png')]\n    test_files.sort(key=lambda s: int(s.split('/')[-1].split('.')[0]))\n    \n    # 3. リストに格納\n    testImg_list = []\n    for name in test_files:\n        bio = all_data[name]\n        with Image.open(bio) as img:\n            testImg_list.append(np.asarray(img))\n\ntest_files[:5]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:24:13.492017Z","iopub.execute_input":"2026-01-06T08:24:13.492365Z","iopub.status.idle":"2026-01-06T08:40:08.309884Z","shell.execute_reply.started":"2026-01-06T08:24:13.492343Z","shell.execute_reply":"2026-01-06T08:40:08.308761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test = np.stack(testImg_list)\nx_test","metadata":{"trusted":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:08.311803Z","iopub.execute_input":"2026-01-06T08:40:08.312727Z","iopub.status.idle":"2026-01-06T08:40:09.136573Z","shell.execute_reply.started":"2026-01-06T08:40:08.312691Z","shell.execute_reply":"2026-01-06T08:40:09.135621Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.save('x_test_cifar10.npy', x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:09.137482Z","iopub.execute_input":"2026-01-06T08:40:09.137892Z","iopub.status.idle":"2026-01-06T08:40:09.714418Z","shell.execute_reply.started":"2026-01-06T08:40:09.137860Z","shell.execute_reply":"2026-01-06T08:40:09.713661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test_files = pd.DataFrame(test_files)\ndf_test_files.to_csv('test_files.csv')\ndf_test_files","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:09.715324Z","iopub.execute_input":"2026-01-06T08:40:09.715772Z","iopub.status.idle":"2026-01-06T08:40:10.127158Z","shell.execute_reply.started":"2026-01-06T08:40:09.715740Z","shell.execute_reply":"2026-01-06T08:40:10.126074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sampleSubmission = pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv', index_col=0)\nsampleSubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:10.128555Z","iopub.execute_input":"2026-01-06T08:40:10.128919Z","iopub.status.idle":"2026-01-06T08:40:10.267572Z","shell.execute_reply.started":"2026-01-06T08:40:10.128892Z","shell.execute_reply":"2026-01-06T08:40:10.266213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train_label = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv', index_col=0)\ndf_train_label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:10.271388Z","iopub.execute_input":"2026-01-06T08:40:10.271795Z","iopub.status.idle":"2026-01-06T08:40:10.303272Z","shell.execute_reply.started":"2026-01-06T08:40:10.271770Z","shell.execute_reply":"2026-01-06T08:40:10.302133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id_key = dict([ee for ee in enumerate(np.unique(df_train_label.label.values))])\nid_key","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:10.304395Z","iopub.execute_input":"2026-01-06T08:40:10.304852Z","iopub.status.idle":"2026-01-06T08:40:10.340148Z","shell.execute_reply.started":"2026-01-06T08:40:10.304814Z","shell.execute_reply":"2026-01-06T08:40:10.339132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"key_id = dict([(ee[1], ee[0]) for ee in enumerate(np.unique(df_train_label.label.values))])\nkey_id","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:10.341225Z","iopub.execute_input":"2026-01-06T08:40:10.341481Z","iopub.status.idle":"2026-01-06T08:40:10.391297Z","shell.execute_reply.started":"2026-01-06T08:40:10.341437Z","shell.execute_reply":"2026-01-06T08:40:10.389395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# テストデータの確認、特に並び順\nnrows=10\nncols=10\nfig, subs = plt.subplots(nrows=nrows, ncols=ncols, figsize=(15, 10))\n\nfor ii in range(nrows):\n    for jj in range(ncols):\n        iplt = subs[ii, jj]\n        img_array = x_test[ii*ncols + jj]\n        iplt.imshow(img_array)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-06T08:40:10.393573Z","iopub.execute_input":"2026-01-06T08:40:10.394067Z","iopub.status.idle":"2026-01-06T08:40:19.298415Z","shell.execute_reply.started":"2026-01-06T08:40:10.394040Z","shell.execute_reply":"2026-01-06T08:40:19.296568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"_uuid":"e48652f7dd5815e13d964e3446fad3480223c675"},"outputs":[],"execution_count":null}]}