Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion your-code/bonus.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
320 changes: 313 additions & 7 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,253 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"import numpy as np\n",
"import pandas as pd \n",
"from sklearn.model_selection import train_test_split\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.utils import to_categorical"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# your code here\n",
"tic = pd.read_csv('tic-tac-toe.csv')\n",
"tic.replace('x', 1, inplace=True)\n",
"tic.replace('0', -1, inplace=True)\n",
"tic.replace('b', 0, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>TL</th>\n",
" <th>TM</th>\n",
" <th>TR</th>\n",
" <th>ML</th>\n",
" <th>MM</th>\n",
" <th>MR</th>\n",
" <th>BL</th>\n",
" <th>BM</th>\n",
" <th>BR</th>\n",
" <th>class</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>0</td>\n",
" <td>o</td>\n",
" <td>0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>953</th>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>954</th>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>955</th>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>956</th>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>957</th>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>o</td>\n",
" <td>o</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>958 rows × 10 columns</p>\n",
"</div>"
],
"text/plain": [
" TL TM TR ML MM MR BL BM BR class\n",
"0 1 1 1 1 o o 1 o o True\n",
"1 1 1 1 1 o o o 1 o True\n",
"2 1 1 1 1 o o o o 1 True\n",
"3 1 1 1 1 o o o 0 0 True\n",
"4 1 1 1 1 o o 0 o 0 True\n",
".. .. .. .. .. .. .. .. .. .. ...\n",
"953 o 1 1 1 o o o 1 1 False\n",
"954 o 1 o 1 1 o 1 o 1 False\n",
"955 o 1 o 1 o 1 1 o 1 False\n",
"956 o 1 o o 1 1 1 o 1 False\n",
"957 o o 1 1 1 o o 1 1 False\n",
"\n",
"[958 rows x 10 columns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tic"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# your code here\n",
"y = np.array(tic['class'])\n",
"x = np.array(tic.drop(labels='class', axis=1))"
]
},
{
Expand All @@ -60,11 +302,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 1.],\n",
" [0., 1.],\n",
" [1., 0.],\n",
" ...,\n",
" [0., 1.],\n",
" [0., 1.],\n",
" [0., 1.]], dtype=float32)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code here"
"x_train, x_test, y_train, y_test = train_test_split(x, y,test_size = 0.2, random_state=29)\n",
"to_categorical(y_train)"
]
},
{
Expand All @@ -82,7 +342,53 @@
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"# your code here\n",
"#Create a Sequential model.\n",
"model = Sequential([\n",
" Dense(18, activation='relu'),\n",
" Dense(18, activation='relu'),\n",
" Dense(2, activation='softmax'),\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Compile the model using adam as the optimizer\n",
"\n",
"model.compile(\n",
" optimizer='adam',\n",
" loss='saparse_categorical_crossentropy',\n",
" metrics=['accuracy'],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Fit the training data\n",
"history = model.fit(X_train, y_train, epochs=50, batch_size=40)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"#Evaluate your neural network model with the test data.\n",
"model.evaluate(\n",
" x_test,\n",
" y_test\n",
")"
]
},
{
Expand Down Expand Up @@ -144,7 +450,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down