{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/aiforcegen3-challenge1/'\n\ntrain_df = pd.read_csv(path + 'train.csv')\ntest_df = pd.read_csv(path + 'test.csv')\ntrain_df['label'].value_counts().plot(kind='bar')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=np.asarray(train_df['label'])\nX=np.asarray(train_df.drop('label',axis=1))\nX=X/255.\nprint(X.shape,y.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# randomly generate data \nN = y.shape[0] # number of training sample \nd = 28*28 # data dimension \nC = 10 # number of classes ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def softmax_stable(Z):\n    \"\"\"\n    Compute softmax values for each sets of scores in Z.\n    each column of Z is a set of score.    \n    \"\"\"\n    e_Z = np.exp(Z - np.max(Z, axis = 0, keepdims = True))\n    A = e_Z / e_Z.sum(axis = 0)\n    return A\n\n## One-hot coding\ndef convert_labels(y, C = C):\n    Y=np.repeat(0,C*y.shape[0]).reshape(y.shape[0],C)\n    dem=0\n    for yi in Y:\n        yi[y[dem]]=1\n#         print(yi,y[dem])\n        dem=dem+1\n    return Y\n\nY = convert_labels(y, C)\nprint(Y.shape)\nprint(Y)\nprint(y)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def grad(X, Y, W):\n    A = softmax_stable((W.T.dot(X)))\n    E = A - Y\n    E = np.array([E])\n    X = np.array([X])\n    return X.T.dot(E)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def softmax_regression(X, y, W_init, eta, tol = 1e-4, max_count = 600000):\n    W = [W_init]    \n    C = W_init.shape[1]\n    Y = convert_labels(y, C)\n    N = X.shape[0]\n    d = X.shape[1]\n    Wa=W_init\n        \n    count = 0\n    check_w_after = 20\n    while count < max_count:\n        # mix data \n        mix_id = np.random.permutation(N)\n        for i in mix_id:\n            xi = X[i]\n            yi = Y[i]\n            W_new=W[-1]+eta*grad(xi,yi,W[-1])\n            count += 1\n            # stopping criteria\n            if count%check_w_after == 0:                \n                if np.linalg.norm(W_new - Wa) < tol:\n                    return W\n                Wa=W_new\n            W[-1]=W_new\n    return W\neta = 0.005 \nd = X.shape[1]\nW_init = np.random.randn(d, C)\n\nW = softmax_regression(X, y, W_init, eta)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=[]\nx_test=np.array(test_df)\nfor x in x_test:\n    a = softmax_stable(np.dot(W[-1].T, x))\n    y_pred.append(a.argmax())\ny_pred=np.asarray(y_pred)\nprint(y_pred.shape)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgid=np.array(range(0,x_test.shape[0])).astype(int)\nlabel=pd.Series(imgid,name='ImageId').astype(int)\nprd=pd.Series(y_pred,name='Label').astype(int)\nresults = pd.concat([label,prd],axis=1)\nresults.to_csv('output.csv',index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}