{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\ntrain = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#SIMPLIFICATION NORMALIZE =  cossimilarity becomes DOT ( Xtrain, Qn.T)\n#Qt=np.linalg.inv(np.diag(Sigma)) #.dot(U.T) #.dot(Q)\n#pd.DataFrame( (U/Sigma).dot(Q) ).sort_values(0)\nfrom sklearn import preprocessing\nXtrain = preprocessing.normalize(train.drop('label',axis=1), norm='l2')\nXtest=preprocessing.normalize(test.drop('id',axis=1), norm='l2')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Q=Xtest[0]\ntop10=pd.DataFrame(np.dot(Xtrain,Q)).sort_values(0)[-10:]\n[str(x)[-1:] for x in top10.index]\ntop10[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results=[] #speedup with list ipo pandas\nfor xi in range(len(test)):\n    Q=Xtest[xi]\n    top10=pd.DataFrame(np.dot(Xtrain,Q)).sort_values(0)[-10:]\n    results.append([train.iat[x,0] for x in top10.index]) #+[top10[-1:].values[0]]  #correct error with  train.iloc[x].label  str(x)[-1:] \n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results=pd.DataFrame(results)\nresults","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def most_frequent(List): \n    return max(set(List), key = List.count) \n\nresults['pred']=-1\n\nfor xi in range( len(test) ):\n    results.iat[xi,10]=most_frequent(list( results.iloc[xi] ) )\nresults","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results['id']=test['id']\nresults['label']=results['pred']\nresults[['id','label']].to_csv(path_or_buf ='cossim.csv', index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}