{
"cells": [
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"import torch as t\n",
"from torch import nn\n",
"import torch.nn.functional as F\n",
"import pandas as pd\n",
"import sklearn as sc\n",
"from sklearn.feature_extraction import DictVectorizer as DV\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"plt.rcParams['font.family'] = 'serif'\n",
"plt.rcParams['font.serif'] = 'FreeSerif'\n",
"plt.rcParams['lines.linewidth'] = 2\n",
"plt.rcParams['lines.markersize'] = 12\n",
"plt.rcParams['xtick.labelsize'] = 24\n",
"plt.rcParams['ytick.labelsize'] = 24\n",
"plt.rcParams['legend.fontsize'] = 24\n",
"plt.rcParams['axes.titlesize'] = 26\n",
"plt.rcParams['axes.labelsize'] = 24"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"columnNames = ['WKingLetter','WKingPos', 'WRookLetter', 'WRookPos', 'BKingLetter', 'BKingPos',\n",
" 'optimal depth-of-win for White in 0 to 16 moves/draw'] \n",
"data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/chess/king-rook-vs-king/krkopt.data', \n",
" names = columnNames, header=None)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" WKingLetter | \n",
" WKingPos | \n",
" WRookLetter | \n",
" WRookPos | \n",
" BKingLetter | \n",
" BKingPos | \n",
" optimal depth-of-win for White in 0 to 16 moves/draw | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" a | \n",
" 1 | \n",
" b | \n",
" 3 | \n",
" c | \n",
" 2 | \n",
" draw | \n",
"
\n",
" \n",
" 1 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 1 | \n",
" c | \n",
" 2 | \n",
" draw | \n",
"
\n",
" \n",
" 2 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 1 | \n",
" d | \n",
" 1 | \n",
" draw | \n",
"
\n",
" \n",
" 3 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 1 | \n",
" d | \n",
" 2 | \n",
" draw | \n",
"
\n",
" \n",
" 4 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 2 | \n",
" c | \n",
" 1 | \n",
" draw | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" WKingLetter WKingPos WRookLetter WRookPos BKingLetter BKingPos \\\n",
"0 a 1 b 3 c 2 \n",
"1 a 1 c 1 c 2 \n",
"2 a 1 c 1 d 1 \n",
"3 a 1 c 1 d 2 \n",
"4 a 1 c 2 c 1 \n",
"\n",
" optimal depth-of-win for White in 0 to 16 moves/draw \n",
"0 draw \n",
"1 draw \n",
"2 draw \n",
"3 draw \n",
"4 draw "
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head()"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28056"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" WKingLetter | \n",
" WKingPos | \n",
" WRookLetter | \n",
" WRookPos | \n",
" BKingLetter | \n",
" BKingPos | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" a | \n",
" 1 | \n",
" b | \n",
" 3 | \n",
" c | \n",
" 2 | \n",
"
\n",
" \n",
" 1 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 1 | \n",
" c | \n",
" 2 | \n",
"
\n",
" \n",
" 2 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 1 | \n",
" d | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 1 | \n",
" d | \n",
" 2 | \n",
"
\n",
" \n",
" 4 | \n",
" a | \n",
" 1 | \n",
" c | \n",
" 2 | \n",
" c | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" WKingLetter WKingPos WRookLetter WRookPos BKingLetter BKingPos\n",
"0 a 1 b 3 c 2\n",
"1 a 1 c 1 c 2\n",
"2 a 1 c 1 d 1\n",
"3 a 1 c 1 d 2\n",
"4 a 1 c 2 c 1"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = data.drop(['optimal depth-of-win for White in 0 to 16 moves/draw'], axis=1)\n",
"X.head()"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"strings = ['WKingLetter', 'WRookLetter', 'BKingLetter']\n",
"for s in strings:\n",
" X[s].replace(to_replace=dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8), inplace=True)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" WKingLetter | \n",
" WKingPos | \n",
" WRookLetter | \n",
" WRookPos | \n",
" BKingLetter | \n",
" BKingPos | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 1 | \n",
" 2 | \n",
" 3 | \n",
" 3 | \n",
" 2 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 3 | \n",
" 1 | \n",
" 3 | \n",
" 2 | \n",
"
\n",
" \n",
" 2 | \n",
" 1 | \n",
" 1 | \n",
" 3 | \n",
" 1 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" 1 | \n",
" 1 | \n",
" 3 | \n",
" 1 | \n",
" 4 | \n",
" 2 | \n",
"
\n",
" \n",
" 4 | \n",
" 1 | \n",
" 1 | \n",
" 3 | \n",
" 2 | \n",
" 3 | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" WKingLetter WKingPos WRookLetter WRookPos BKingLetter BKingPos\n",
"0 1 1 2 3 3 2\n",
"1 1 1 3 1 3 2\n",
"2 1 1 3 1 4 1\n",
"3 1 1 3 1 4 2\n",
"4 1 1 3 2 3 1"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X.head()"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 17\n",
"1 17\n",
"2 17\n",
"3 17\n",
"4 17\n",
"Name: optimal depth-of-win for White in 0 to 16 moves/draw, dtype: int64"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"target = data['optimal depth-of-win for White in 0 to 16 moves/draw']\n",
"target.replace(to_replace=dict(zero=0, one=1, two=2, three=3, four=4, five=5, six=6, seven=7, eight=8,\n",
" nine=9, ten=10, eleven=11, twelve=12, thirteen=13, fourteen=14, fifteen=15, sixteen=16, draw=17), inplace=True)\n",
"target.head()"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Weight shapes: [torch.Size([1, 6]), torch.Size([1])]\n"
]
}
],
"source": [
"model = nn.Sequential()\n",
"model.add_module('l1', nn.Linear(6, 1))\n",
"model.add_module('l2', nn.Sigmoid())\n",
"print(\"Weight shapes:\", [w.shape for w in model.parameters()])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_data_train, X_data_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.5, \n",
" random_state=k)\n",
"x = torch.tensor(X_train[:3], dtype=torch.float32)\n",
"y = torch.tensor(y_train[:3], dtype=torch.float32)\n",
"\n",
"y_predicted = model(x)[:, 0]\n",
"\n",
"y_predicted"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}