{"cells":[{"metadata":{"_uuid":"fbbd08adf945810e393b1ee5589a71d6a9603527"},"cell_type":"markdown","source":"### Here I'll show one pretty simple way to convert the \"drawings\" into a numpy array that you can use to train your models.\n\n### Dependencies"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport ast\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"### Load data"},{"metadata":{"trusted":true,"_uuid":"1873f6484de7b01000a640973951b63a3c84fb53"},"cell_type":"code","source":"train = pd.DataFrame()\nfor file in os.listdir('../input/train_simplified/'):\n    train = train.append(pd.read_csv('../input/train_simplified/' + file, index_col='key_id', nrows=1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6dc86fbe18e8cffbbc07a7334b44163f7152fd19"},"cell_type":"code","source":"# You may choose the shape you want.\ndef drawing_to_np(drawing, shape=(64, 64)):\n    # evaluates the drawing array\n    drawing = eval(drawing)\n    fig, ax = plt.subplots()\n    # Close figure so it won't get displayed while transforming the set\n    plt.close(fig)\n    for x,y in drawing:\n        ax.plot(x, y, marker='.')\n        ax.axis('off')        \n    fig.canvas.draw()\n    # Convert images to numpy array\n    np_drawing = np.array(fig.canvas.renderer._renderer)\n    # If you want to take only one channel, you can try somethin like:\n    # np_drawing = np_drawing[:, :, 1]    \n    return cv2.resize(np_drawing, shape) # Resize array","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"842ad1bd83e753820f7cfe4600486619b3f8a775"},"cell_type":"markdown","source":"### Applying the function"},{"metadata":{"trusted":true,"_uuid":"e51d70a7f91d3a8d4deaf2d1b3e2500ad2ba3563","_kg_hide-output":true},"cell_type":"code","source":"# One way you could apply the transformation to you dataset.\ntrain['drawing_np'] = train['drawing'].map(drawing_to_np)\ntrain['drawing_np2'] = train['drawing'].apply(drawing_to_np)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a81aade735617c85dc34d7f005e17bed550eca5a","_kg_hide-input":true},"cell_type":"code","source":"train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"eed3a9683dde045d232aa56b53d08d5fc83bb1b1"},"cell_type":"markdown","source":"### Let's look at the new features.\n\nFirst the original drawing"},{"metadata":{"trusted":true,"_uuid":"be8ed354256a29d6f578dbf7f8cd20bdc411b271"},"cell_type":"code","source":"drawings = [ast.literal_eval(pts) for pts in train['drawing'].head(1).values]\n\nplt.figure(figsize=(10, 10))\nfor i, drawing in enumerate(drawings):\n    plt.subplot(330 + (i+1))\n    for x,y in drawing:\n        plt.plot(x, y, marker='.')\n        plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8fb8eeb5d2b6e7b466db123c961a116fa778d1b"},"cell_type":"markdown","source":"Now the drawing with the generated with the function applied with the 1st form."},{"metadata":{"trusted":true,"_uuid":"2bb1298c26bfb3f3c421e500b5f8199d35e9e518"},"cell_type":"code","source":"# Function to plot images.\ndef plot_image(image_array):\n    fig2 = plt.figure()\n    ax2 = fig2.add_subplot(111, frameon=False)\n    ax2.imshow(image_array)\n    plt.axis('off')\n    plt.show()\n    print('Image shape:', image_array.shape)\n\nsample_1 = train['drawing_np'].values[0]\nplot_image(sample_1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"eb19b4bd6f6005e7cd2b8b9a23d2856e3f05983e"},"cell_type":"markdown","source":"Now the drawing with the generated with the function applied with the 2nd form."},{"metadata":{"trusted":true,"_uuid":"7622db47b12129381a02f5d97c8b72228fa66b65"},"cell_type":"code","source":"sample_2 = train['drawing_np2'].values[0]\nplot_image(sample_2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"efb8329fb578dbc8cb7e0c03ffc0d023785107f1"},"cell_type":"markdown","source":"Now you can use your new features to feed your models, good luck!"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}