{"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":"markdown","source":"# Conway's Game of Life  \nIn train.csv we are given the 50000 simulations of Conway's Game of Life. Each row of the dataframe has information about the start and stop boards. The columns **start_0** to **start_624** represent the 25 by 25 start board where a 1 represents a live cell and 0 represents a dead cell. Similarly, the columns **stop_0** to **stop_624** represent the start board's state after **delta** time steps. \n\nTo determine the state of a board after 1 time step, each cell of the board undergoes 1 of the 4 transformations:\n- **Overpopulation**: if a living cell is surrounded by more than three living cells, it dies.\n- **Stasis**: if a living cell is surrounded by two or three living cells, it survives.\n- **Underpopulation**: if a living cell is surrounded by fewer than two living cells, it dies.\n- **Reproduction**: if a dead cell is surrounded by exactly three cells, it becomes a live cell.\n\nIn test.csv we are only given *delta* and **stop_0** to **stop_624**. Our goal is to predict the start board's original state, which is **delta** time steps before the stop board's state. ","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First we load in the train and test sets and examine their shapes","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/conways-reverse-game-of-life-2020/train.csv')\ntest = pd.read_csv('/kaggle/input/conways-reverse-game-of-life-2020/test.csv')\nprint(train.shape)\nprint(test.shape)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To visualize the difference between the board's stop and start state, we can first extract the columns of the start and stop boards and convert them into a 2D array with shape (25, 25). Here, we randomly take entry 23 from the train set for the visualization.","metadata":{}},{"cell_type":"code","source":"sample_start_23 = train.loc[23, train.columns.str.startswith('start')]\nsample_stop_23 = train.loc[23, train.columns.str.startswith('stop')]\nprint(sample_start_23)\nprint(sample_stop_23)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_start_23 = np.asarray(sample_start_23).reshape(25, 25)\nsample_stop_23 = np.asarray(sample_stop_23).reshape(25, 25)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_start_23","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Before we plot the images of the boards, we can extract the number of time steps that passed between the two boards for entry 23. ","metadata":{}},{"cell_type":"code","source":"train.loc[23, 'delta']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Below shows start board 23 and what it looks like after 1 time step","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(18, 7), dpi=300)\nax[0] = plt.subplot2grid((1,2), (0,0), colspan=1)\nax[1] = plt.subplot2grid((1,2), (0,1), colspan=1)\nax[0].imshow(sample_start_23)\nax[0].set_title('Start Board 23')\nax[1].imshow(sample_stop_23)\nax[1].set_title(f'Board after 1 time step')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}