Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

done #13

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
256 changes: 243 additions & 13 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,139 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 107,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" TL TM TR ML MM MR BL BM BR class\n",
"0 x x x x o o x o o True\n",
"1 x x x x o o o x o True\n",
"2 x x x x o o o o x True\n",
"3 x x x x o o o b b True\n",
"4 x x x x o o b o b True\n"
]
}
],
"source": [
"# your code here\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"df = pd.read_csv('tic-tac-toe.csv')\n",
"\n",
"print(df.head())\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" TL TM TR ML MM MR BL BM BR class\n",
"0 x x x x o o x o o True\n",
"1 x x x x o o o x o True\n",
"2 x x x x o o o o x True\n",
"3 x x x x o o o b b True\n",
"4 x x x x o o b o b True\n",
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 958 entries, 0 to 957\n",
"Data columns (total 10 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 TL 958 non-null object\n",
" 1 TM 958 non-null object\n",
" 2 TR 958 non-null object\n",
" 3 ML 958 non-null object\n",
" 4 MM 958 non-null object\n",
" 5 MR 958 non-null object\n",
" 6 BL 958 non-null object\n",
" 7 BM 958 non-null object\n",
" 8 BR 958 non-null object\n",
" 9 class 958 non-null bool \n",
"dtypes: bool(1), object(9)\n",
"memory usage: 68.4+ KB\n",
"None\n",
"TL 0\n",
"TM 0\n",
"TR 0\n",
"ML 0\n",
"MM 0\n",
"MR 0\n",
"BL 0\n",
"BM 0\n",
"BR 0\n",
"class 0\n",
"dtype: int64\n"
]
}
],
"source": [
"print(df.head())\n",
"print(df.info())\n",
"print(df.isnull().sum())"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"# Apply explicit mapping for grid features\n",
"# Map 'x' -> 1, 'o' -> -1, 'b' -> 0\n",
"value_map = {'x': 1, 'o': -1, 'b': 0}\n",
"for col in df.columns[:-1]: # Skip the last column which is the target ('class')\n",
" df[col] = df[col].map(value_map)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"X = df.drop(columns=['class'])\n",
"y = df['class']"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" TL TM TR ML MM MR BL BM BR\n",
"302 -1 1 1 -1 1 -1 0 1 0\n",
"467 -1 0 -1 -1 1 0 1 1 1\n",
"294 1 0 0 0 1 0 -1 -1 1\n",
"548 0 -1 1 -1 0 1 0 0 1\n",
"465 -1 0 -1 1 -1 0 1 1 1\n",
"302 True\n",
"467 True\n",
"294 True\n",
"548 True\n",
"465 True\n",
"Name: class, dtype: bool\n"
]
}
],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"\n",
"# Print the first few rows to verify\n",
"print(x_train.head())\n",
"print(y_train.head())"
]
},
{
Expand All @@ -60,11 +188,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 112,
"metadata": {},
"outputs": [],
"source": [
"# your code here\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from keras import layers\n",
"from keras import models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Step 1: Split the Traning and test Data"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"model = models.Sequential()\n",
"\n",
"model.add(layers.Dense(64, activation='relu', input_shape=(x_train.shape[1],)))\n",
"model.add(layers.Dense(32, activation='relu'))\n",
"model.add(layers.Dense(1, activation='sigmoid'))\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Step 2: Compile the model"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [],
"source": [
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])"
]
},
{
Expand All @@ -78,11 +248,59 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 118,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/20\n",
"31/31 [==============================] - 5s 155ms/step - loss: 0.0098 - accuracy: 1.0000 - val_loss: 0.0443 - val_accuracy: 0.9870\n",
"Epoch 2/20\n",
"31/31 [==============================] - 5s 152ms/step - loss: 0.0091 - accuracy: 1.0000 - val_loss: 0.0409 - val_accuracy: 0.9870\n",
"Epoch 3/20\n",
"31/31 [==============================] - 2s 74ms/step - loss: 0.0081 - accuracy: 1.0000 - val_loss: 0.0415 - val_accuracy: 0.9870\n",
"Epoch 4/20\n",
"31/31 [==============================] - 2s 81ms/step - loss: 0.0073 - accuracy: 1.0000 - val_loss: 0.0390 - val_accuracy: 0.9870\n",
"Epoch 5/20\n",
"31/31 [==============================] - 2s 49ms/step - loss: 0.0066 - accuracy: 1.0000 - val_loss: 0.0393 - val_accuracy: 0.9870\n",
"Epoch 6/20\n",
"31/31 [==============================] - 1s 30ms/step - loss: 0.0061 - accuracy: 1.0000 - val_loss: 0.0383 - val_accuracy: 0.9870\n",
"Epoch 7/20\n",
"31/31 [==============================] - 2s 51ms/step - loss: 0.0058 - accuracy: 1.0000 - val_loss: 0.0373 - val_accuracy: 0.9870\n",
"Epoch 8/20\n",
"31/31 [==============================] - 1s 35ms/step - loss: 0.0054 - accuracy: 1.0000 - val_loss: 0.0383 - val_accuracy: 0.9870\n",
"Epoch 9/20\n",
"31/31 [==============================] - 2s 70ms/step - loss: 0.0048 - accuracy: 1.0000 - val_loss: 0.0367 - val_accuracy: 0.9870\n",
"Epoch 10/20\n",
"31/31 [==============================] - 2s 58ms/step - loss: 0.0045 - accuracy: 1.0000 - val_loss: 0.0350 - val_accuracy: 0.9870\n",
"Epoch 11/20\n",
"31/31 [==============================] - 2s 60ms/step - loss: 0.0041 - accuracy: 1.0000 - val_loss: 0.0350 - val_accuracy: 0.9870\n",
"Epoch 12/20\n",
"31/31 [==============================] - 1s 44ms/step - loss: 0.0039 - accuracy: 1.0000 - val_loss: 0.0353 - val_accuracy: 0.9870\n",
"Epoch 13/20\n",
"31/31 [==============================] - 3s 91ms/step - loss: 0.0036 - accuracy: 1.0000 - val_loss: 0.0334 - val_accuracy: 0.9870\n",
"Epoch 14/20\n",
"31/31 [==============================] - 2s 64ms/step - loss: 0.0034 - accuracy: 1.0000 - val_loss: 0.0331 - val_accuracy: 0.9870\n",
"Epoch 15/20\n",
"31/31 [==============================] - 2s 72ms/step - loss: 0.0032 - accuracy: 1.0000 - val_loss: 0.0333 - val_accuracy: 0.9870\n",
"Epoch 16/20\n",
"31/31 [==============================] - 3s 86ms/step - loss: 0.0030 - accuracy: 1.0000 - val_loss: 0.0321 - val_accuracy: 0.9870\n",
"Epoch 17/20\n",
"31/31 [==============================] - 3s 109ms/step - loss: 0.0028 - accuracy: 1.0000 - val_loss: 0.0332 - val_accuracy: 0.9870\n",
"Epoch 18/20\n",
"31/31 [==============================] - 2s 80ms/step - loss: 0.0027 - accuracy: 1.0000 - val_loss: 0.0330 - val_accuracy: 0.9870\n",
"Epoch 19/20\n",
"31/31 [==============================] - 2s 62ms/step - loss: 0.0025 - accuracy: 1.0000 - val_loss: 0.0314 - val_accuracy: 0.9870\n",
"Epoch 20/20\n",
"31/31 [==============================] - 1s 29ms/step - loss: 0.0024 - accuracy: 1.0000 - val_loss: 0.0308 - val_accuracy: 0.9870\n"
]
}
],
"source": [
"# your code here"
"# your code here\n",
"history = model.fit(x_train, y_train, epochs=26, batch_size=16, validation_split=0.2)\n"
]
},
{
Expand All @@ -104,11 +322,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 116,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6/6 [==============================] - 0s 54ms/step - loss: 0.0615 - accuracy: 0.9792\n",
"Test Accuracy: 0.9792\n"
]
}
],
"source": [
"# your code here"
"# your code here\n",
"test_loss, test_accuracy = model.evaluate(x_test, y_test)\n",
"print(f'Test Accuracy: {test_accuracy:.4f}')"
]
},
{
Expand All @@ -120,11 +349,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 117,
"metadata": {},
"outputs": [],
"source": [
"# your answer here"
"# your answer here\n",
"#THe use of ReLU as the activation function for the middle layers. Use Softmax for the output layer because each output has a single lable and all the label probabilities add up to 1."
]
}
],
Expand All @@ -144,7 +374,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down