{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":9138787,"sourceType":"datasetVersion","datasetId":5519183}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-09T07:07:10.217212Z","iopub.execute_input":"2024-08-09T07:07:10.218417Z","iopub.status.idle":"2024-08-09T07:07:10.223875Z","shell.execute_reply.started":"2024-08-09T07:07:10.218366Z","shell.execute_reply":"2024-08-09T07:07:10.222292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head(","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:07:14.079937Z","iopub.execute_input":"2024-08-09T07:07:14.080346Z","iopub.status.idle":"2024-08-09T07:07:16.623427Z","shell.execute_reply.started":"2024-08-09T07:07:14.080313Z","shell.execute_reply":"2024-08-09T07:07:16.621829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = df.to_numpy()\nds.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:07:20.451532Z","iopub.execute_input":"2024-08-09T07:07:20.451971Z","iopub.status.idle":"2024-08-09T07:07:20.461843Z","shell.execute_reply.started":"2024-08-09T07:07:20.451935Z","shell.execute_reply":"2024-08-09T07:07:20.460054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y,X = np.split(ds, (1, ), axis = 1 )\ny = y.squeeze()\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:07:21.496944Z","iopub.execute_input":"2024-08-09T07:07:21.49764Z","iopub.status.idle":"2024-08-09T07:07:21.508888Z","shell.execute_reply.started":"2024-08-09T07:07:21.497584Z","shell.execute_reply":"2024-08-09T07:07:21.506527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtrain, Xvalidation, Xtest = np.split( X, (35000, 38000), axis = 0 )\nytrain, yvalidation, ytest = np.split( y, (35000, 38000), axis = 0 )\nXtrain = Xtrain / 255   # 标准化处理\nXvalidation = Xvalidation / 255\nXtest = Xtest / 255\nXtrain.shape, Xvalidation.shape, Xtest.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:07:22.666726Z","iopub.execute_input":"2024-08-09T07:07:22.667807Z","iopub.status.idle":"2024-08-09T07:07:22.803816Z","shell.execute_reply.started":"2024-08-09T07:07:22.667761Z","shell.execute_reply":"2024-08-09T07:07:22.802514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W = np.random.random( (784, 10) )\nW0 = np.random.random( )","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:07:24.086922Z","iopub.execute_input":"2024-08-09T07:07:24.088149Z","iopub.status.idle":"2024-08-09T07:07:24.093546Z","shell.execute_reply.started":"2024-08-09T07:07:24.088103Z","shell.execute_reply":"2024-08-09T07:07:24.092212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = 100\nxue = 1e0\nfor i in range(5000):\n    # 1、随机W和W0，进行线性计算，score\n    score = Xtrain @ W + W0\n    # 2、e**score, 计算归一化，概率P\n    es = np.exp(score)\n    P = (es.T / es.sum(axis=1) ).T\n    # 2.1、计算onehot\n    zeros = np.zeros( (10, 10) )\n    zeros[np.arange(10), np.arange(10)] = 1\n    onehot = zeros[ytrain]\n    # 求 k \n    kw = ((P-onehot).T @ Xtrain / 42000).T\n    kw0 = (P - onehot).mean(axis=0)\n    # 更新W\n    W = W - kw * xue\n    W0 = W0 - kw0 * xue\n    # 计算loss\n    loss = - np.log( (P * onehot).sum(axis=1) ).mean()\n#     if i % 100 == 99:\n#         print(loss)\n#         print(loss < t)\n#         t = loss","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:21:43.648704Z","iopub.execute_input":"2024-08-09T07:21:43.649114Z","iopub.status.idle":"2024-08-09T07:23:32.834032Z","shell.execute_reply.started":"2024-08-09T07:21:43.649079Z","shell.execute_reply":"2024-08-09T07:23:32.832556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SoftMax算法\n# 1、随机W和W0，进行线性计算，score\nscore = Xvalidation @ W + W0\nprint(W.shape, W0.shape, score.shape)\n# 2、e**score, 计算归一化，概率P\nt = np.exp(score)\nP = t / (t.sum(axis=1).reshape(-1,1)+1e-10)\n# 3、argmax计算 Y' （YP）\nYP = P.argmax(axis=1)\n# print(YP.shape)\n# # 计算正确率\n# print((YP == yvalidation).sum()/3000)","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:23:48.412296Z","iopub.execute_input":"2024-08-09T07:23:48.412671Z","iopub.status.idle":"2024-08-09T07:23:48.43217Z","shell.execute_reply.started":"2024-08-09T07:23:48.412643Z","shell.execute_reply":"2024-08-09T07:23:48.430253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread('/kaggle/input/mysets/IMG_0374.jpeg')\n# print(img.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:26:50.77086Z","iopub.execute_input":"2024-08-09T07:26:50.771566Z","iopub.status.idle":"2024-08-09T07:26:51.031628Z","shell.execute_reply.started":"2024-08-09T07:26:50.77153Z","shell.execute_reply":"2024-08-09T07:26:51.029845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im1 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n# im1 = cv2.resize(im1, 38, 38)\nim1.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:29:18.212586Z","iopub.execute_input":"2024-08-09T07:29:18.212963Z","iopub.status.idle":"2024-08-09T07:29:18.233447Z","shell.execute_reply.started":"2024-08-09T07:29:18.212933Z","shell.execute_reply":"2024-08-09T07:29:18.231809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imtest = im1.reshape(784)\nimtest.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:34:50.027833Z","iopub.execute_input":"2024-08-09T07:34:50.028281Z","iopub.status.idle":"2024-08-09T07:34:50.036533Z","shell.execute_reply.started":"2024-08-09T07:34:50.02825Z","shell.execute_reply":"2024-08-09T07:34:50.035085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imtest = imtest / 255\nscore = imtest @ W + W0\n","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:34:50.564514Z","iopub.execute_input":"2024-08-09T07:34:50.564909Z","iopub.status.idle":"2024-08-09T07:34:50.572705Z","shell.execute_reply.started":"2024-08-09T07:34:50.564878Z","shell.execute_reply":"2024-08-09T07:34:50.571498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score.argmax()","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:35:13.633437Z","iopub.execute_input":"2024-08-09T07:35:13.633818Z","iopub.status.idle":"2024-08-09T07:35:13.641403Z","shell.execute_reply.started":"2024-08-09T07:35:13.633789Z","shell.execute_reply":"2024-08-09T07:35:13.639965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest = test.to_numpy()\n# test = test / 255\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:41:44.459362Z","iopub.execute_input":"2024-08-09T07:41:44.460684Z","iopub.status.idle":"2024-08-09T07:41:45.974002Z","shell.execute_reply.started":"2024-08-09T07:41:44.460642Z","shell.execute_reply":"2024-08-09T07:41:45.972866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test / 255\nscore = test @ W + W0\nans = score.argmax(axis = 1)","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:41:45.976035Z","iopub.execute_input":"2024-08-09T07:41:45.976757Z","iopub.status.idle":"2024-08-09T07:41:46.098294Z","shell.execute_reply.started":"2024-08-09T07:41:45.976717Z","shell.execute_reply":"2024-08-09T07:41:46.096814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans.shape\n","metadata":{"execution":{"iopub.status.busy":"2024-08-09T07:41:58.048196Z","iopub.execute_input":"2024-08-09T07:41:58.048586Z","iopub.status.idle":"2024-08-09T07:41:58.055678Z","shell.execute_reply.started":"2024-08-09T07:41:58.048556Z","shell.execute_reply":"2024-08-09T07:41:58.054254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageid = np.arange(1, 28001)\nsub = pd.DataFrame(imageid, columns = ['ImageId'])\nsub['Label'] = ans\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-09T08:04:13.877887Z","iopub.execute_input":"2024-08-09T08:04:13.878323Z","iopub.status.idle":"2024-08-09T08:04:13.890609Z","shell.execute_reply.started":"2024-08-09T08:04:13.878289Z","shell.execute_reply":"2024-08-09T08:04:13.889478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index = False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SoftMax算法\n# 1、随机W和W0，进行线性计算，score\nscore = Xtrain @ W + W0\nprint(W.shape, W0.shape, score.shape)\n# 2、e**score, 计算归一化，概率P\nt = np.exp(score)\nP = t / (t.sum(axis=1).reshape(-1,1)+1e-10)\n# 3、argmax计算 Y' （YP）\nYP = P.argmax(axis=1)\nprint(YP.shape)\n# 计算正确率\nprint((YP == ytrain).sum()/35000)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nf = open('model', 'wb')\npickle.dump( (W, W0), f )\nf.close()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import \nf = open('model', 'rb')# read binary\nW, W0 = pickle.load(f)\nf.close()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}