zhangchaodesign/Ranking-SVM
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 1,6 "id": "51cc7bd6",7 "metadata": {},8 "outputs": [],9 "source": [10 "import numpy as np\n",11 "import pandas as pd\n",12 "import matplotlib.pyplot as plt"13 ]14 },15 {16 "cell_type": "code",17 "execution_count": 2,18 "id": "371b9552",19 "metadata": {},20 "outputs": [],21 "source": [22 "from sklearn import svm\n",23 "from sklearn.svm import LinearSVC\n",24 "from sklearn.inspection import DecisionBoundaryDisplay"25 ]26 },27 {28 "cell_type": "code",29 "execution_count": 3,30 "id": "e96bb530",31 "metadata": {},32 "outputs": [],33 "source": [34 "rdata = pd.read_csv(\"./data.csv\")"35 ]36 },37 {38 "cell_type": "code",39 "execution_count": 4,40 "id": "ba99b21d",41 "metadata": {},42 "outputs": [43 {44 "data": {45 "text/html": [46 "<div>\n",47 "<style scoped>\n",48 " .dataframe tbody tr th:only-of-type {\n",49 " vertical-align: middle;\n",50 " }\n",51 "\n",52 " .dataframe tbody tr th {\n",53 " vertical-align: top;\n",54 " }\n",55 "\n",56 " .dataframe thead th {\n",57 " text-align: right;\n",58 " }\n",59 "</style>\n",60 "<table border=\"1\" class=\"dataframe\">\n",61 " <thead>\n",62 " <tr style=\"text-align: right;\">\n",63 " <th></th>\n",64 " <th>size</th>\n",65 " <th>age</th>\n",66 " <th>cute</th>\n",67 " <th>sassy</th>\n",68 " </tr>\n",69 " </thead>\n",70 " <tbody>\n",71 " <tr>\n",72 " <th>0</th>\n",73 " <td>1.0</td>\n",74 " <td>0.56</td>\n",75 " <td>-0.14</td>\n",76 " <td>0.31</td>\n",77 " </tr>\n",78 " <tr>\n",79 " <th>1</th>\n",80 " <td>1.0</td>\n",81 " <td>89.00</td>\n",82 " <td>0.29</td>\n",83 " <td>1.00</td>\n",84 " </tr>\n",85 " <tr>\n",86 " <th>2</th>\n",87 " <td>1.0</td>\n",88 " <td>0.67</td>\n",89 " <td>-0.14</td>\n",90 " <td>0.00</td>\n",91 " </tr>\n",92 " <tr>\n",93 " <th>3</th>\n",94 " <td>1.0</td>\n",95 " <td>0.56</td>\n",96 " <td>1.00</td>\n",97 " <td>0.15</td>\n",98 " </tr>\n",99 " <tr>\n",100 " <th>4</th>\n",101 " <td>0.5</td>\n",102 " <td>1.00</td>\n",103 " <td>0.29</td>\n",104 " <td>0.15</td>\n",105 " </tr>\n",106 " </tbody>\n",107 "</table>\n",108 "</div>"109 ],110 "text/plain": [111 " size age cute sassy\n",112 "0 1.0 0.56 -0.14 0.31\n",113 "1 1.0 89.00 0.29 1.00\n",114 "2 1.0 0.67 -0.14 0.00\n",115 "3 1.0 0.56 1.00 0.15\n",116 "4 0.5 1.00 0.29 0.15"117 ]118 },119 "execution_count": 4,120 "metadata": {},121 "output_type": "execute_result"122 }123 ],124 "source": [125 "rdata"126 ]127 },128 {129 "cell_type": "code",130 "execution_count": 5,131 "id": "a32c3b08",132 "metadata": {},133 "outputs": [],134 "source": [135 "\n",136 "# rdata['views']"137 ]138 },139 {140 "cell_type": "code",141 "execution_count": 6,142 "id": "de86b900",143 "metadata": {},144 "outputs": [],145 "source": [146 "dmat = np.zeros([5, 4])\n",147 "dmat[:,0]=rdata['size']\n",148 "dmat[:,1]=rdata['age']\n",149 "dmat[:,2]=rdata['cute']\n",150 "dmat[:,3]=rdata['sassy']\n",151 "\n",152 "\n",153 "# dmat = np.zeros([4, 2])\n",154 "# dmat[:,0]=rdata['Details']\n",155 "# dmat[:,1]=rdata['Cleverness']"156 ]157 },158 {159 "cell_type": "code",160 "execution_count": 7,161 "id": "e7cd9d18",162 "metadata": {},163 "outputs": [164 {165 "data": {166 "text/plain": [167 "array([[ 1. , 0.56, -0.14, 0.31],\n",168 " [ 1. , 89. , 0.29, 1. ],\n",169 " [ 1. , 0.67, -0.14, 0. ],\n",170 " [ 1. , 0.56, 1. , 0.15],\n",171 " [ 0.5 , 1. , 0.29, 0.15]])"172 ]173 },174 "execution_count": 7,175 "metadata": {},176 "output_type": "execute_result"177 }178 ],179 "source": [180 "dmat"181 ]182 },183 {184 "cell_type": "code",185 "execution_count": 8,186 "id": "a4cb0570",187 "metadata": {},188 "outputs": [189 {190 "name": "stdout",191 "output_type": "stream",192 "text": [193 "[[ 0. 88.44 0.43 0.69]\n",194 " [ 0. 0.11 0. -0.31]\n",195 " [ 0. -88.33 -0.43 -1. ]\n",196 " [ 0. 0. 1.14 -0.16]\n",197 " [ 0. -88.44 0.71 -0.85]\n",198 " [ 0. -0.11 1.14 0.15]\n",199 " [ 0. -88.44 -0.43 -0.69]\n",200 " [ 0. -0.11 0. 0.31]\n",201 " [ 0. 88.33 0.43 1. ]\n",202 " [ 0. 0. -1.14 0.16]\n",203 " [ 0. 88.44 -0.71 0.85]\n",204 " [ 0. 0.11 -1.14 -0.15]]\n",205 "[1 1 1 1 1 1 2 2 2 2 2 2]\n"206 ]207 }208 ],209 "source": [210 "positive = []\n",211 "negative = []\n",212 "for i in range(1,4):\n",213 " for j in range(i):\n",214 " positive.append(dmat[i,:]-dmat[j,:])\n",215 " negative.append(dmat[j,:]-dmat[i,:])\n",216 " \n",217 "positive_label = 1;\n",218 "negative_label = 2;\n",219 " \n",220 "X = np.array(positive+negative)\n",221 "y = np.array([positive_label]*len(positive)+[negative_label]*len(negative))\n",222 "print(X)\n",223 "print(y)"224 ]225 },226 {227 "cell_type": "code",228 "execution_count": 9,229 "id": "982c5f3b",230 "metadata": {},231 "outputs": [],232 "source": [233 "# len(positive+negative)\n",234 "# len(positive)\n"235 ]236 },237 {238 "cell_type": "code",239 "execution_count": null,240 "id": "26f98302",241 "metadata": {},242 "outputs": [],243 "source": []244 },245 {246 "cell_type": "code",247 "execution_count": 10,248 "id": "0c5d3aa2",249 "metadata": {},250 "outputs": [251 {252 "name": "stderr",253 "output_type": "stream",254 "text": [255 "/Users/chao/anaconda3/envs/nlp/lib/python3.8/site-packages/sklearn/svm/_classes.py:32: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning.\n",256 " warnings.warn(\n"257 ]258 },259 {260 "data": {261 "text/html": [262 "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>Pipeline(steps=[('standardscaler', StandardScaler()),\n",263 " ('linearsvc',\n",264 " LinearSVC(fit_intercept=False, random_state=0, tol=1e-05))])</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" ><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">Pipeline</label><div class=\"sk-toggleable__content\"><pre>Pipeline(steps=[('standardscaler', StandardScaler()),\n",265 " ('linearsvc',\n",266 " LinearSVC(fit_intercept=False, random_state=0, tol=1e-05))])</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" ><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">StandardScaler</label><div class=\"sk-toggleable__content\"><pre>StandardScaler()</pre></div></div></div><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" ><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">LinearSVC</label><div class=\"sk-toggleable__content\"><pre>LinearSVC(fit_intercept=False, random_state=0, tol=1e-05)</pre></div></div></div></div></div></div></div>"267 ],268 "text/plain": [269 "Pipeline(steps=[('standardscaler', StandardScaler()),\n",270 " ('linearsvc',\n",271 " LinearSVC(fit_intercept=False, random_state=0, tol=1e-05))])"272 ]273 },274 "execution_count": 10,275 "metadata": {},276 "output_type": "execute_result"277 }278 ],279 "source": [280 "from sklearn.svm import LinearSVC\n",281 "from sklearn.pipeline import make_pipeline\n",282 "from sklearn.preprocessing import StandardScaler\n",283 "\n",284 "clf = make_pipeline(\n",285 " StandardScaler(),\n",286 " LinearSVC(random_state=0, \n",287 " tol=1e-5, \n",288 " fit_intercept=False # intercept should be false if we want plane to pass through origin\n",289 " )\n",290 ")\n",291 "\n",292 "clf.fit(X, y)"293 ]294 },295 {296 "cell_type": "markdown",297 "id": "771827c6",298 "metadata": {},299 "source": [300 "### Coefficients should be hyperplane weights, I believe"301 ]302 },303 {304 "cell_type": "code",305 "execution_count": 11,306 "id": "870a6983",307 "metadata": {},308 "outputs": [309 {310 "name": "stdout",311 "output_type": "stream",312 "text": [313 "coefficients for decision plane\n",314 "[[ 0. -0.82268916 -0.81484124 1.25408526]]\n"315 ]316 }317 ],318 "source": [319 "print(\"coefficients for decision plane\")\n",320 "print(clf.named_steps['linearsvc'].coef_)"321 ]322 },323 {324 "cell_type": "code",325 "execution_count": 12,326 "id": "3fa9d986",327 "metadata": {},328 "outputs": [329 {330 "data": {331 "text/plain": [332 "{'memory': None,\n",333 " 'steps': [('standardscaler', StandardScaler()),\n",334 " ('linearsvc', LinearSVC(fit_intercept=False, random_state=0, tol=1e-05))],\n",335 " 'verbose': False,\n",336 " 'standardscaler': StandardScaler(),\n",337 " 'linearsvc': LinearSVC(fit_intercept=False, random_state=0, tol=1e-05),\n",338 " 'standardscaler__copy': True,\n",339 " 'standardscaler__with_mean': True,\n",340 " 'standardscaler__with_std': True,\n",341 " 'linearsvc__C': 1.0,\n",342 " 'linearsvc__class_weight': None,\n",343 " 'linearsvc__dual': 'warn',\n",344 " 'linearsvc__fit_intercept': False,\n",345 " 'linearsvc__intercept_scaling': 1,\n",346 " 'linearsvc__loss': 'squared_hinge',\n",347 " 'linearsvc__max_iter': 1000,\n",348 " 'linearsvc__multi_class': 'ovr',\n",349 " 'linearsvc__penalty': 'l2',\n",350 " 'linearsvc__random_state': 0,\n",351 " 'linearsvc__tol': 1e-05,\n",352 " 'linearsvc__verbose': 0}"353 ]354 },355 "execution_count": 12,356 "metadata": {},357 "output_type": "execute_result"358 }359 ],360 "source": [361 "clf.get_params()"362 ]363 },364 {365 "cell_type": "code",366 "execution_count": null,367 "id": "f79bbbd3",368 "metadata": {},369 "outputs": [],370 "source": []371 },372 {373 "cell_type": "code",374 "execution_count": 13,375 "id": "8fb39ede",376 "metadata": {},377 "outputs": [378 {379 "ename": "AssertionError",380 "evalue": "",381 "output_type": "error",382 "traceback": [383 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",384 "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",385 "Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m(\u001b[38;5;28;01mFalse\u001b[39;00m)\n",386 "\u001b[0;31mAssertionError\u001b[0m: "387 ]388 }389 ],390 "source": [391 "assert(False)"392 ]393 },394 {395 "cell_type": "markdown",396 "id": "699d15b3",397 "metadata": {},398 "source": [399 "## Probably ignore below"400 ]401 },402 {403 "cell_type": "code",404 "execution_count": null,405 "id": "09f8538e",406 "metadata": {},407 "outputs": [],408 "source": [409 "\n",410 "clf = svm.SVC(kernel=\"linear\", C=1000)\n",411 "clf.fit(X, y)\n",412 "\n",413 "plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)\n",414 "ax = plt.gca()\n",415 "DecisionBoundaryDisplay.from_estimator(\n",416 " clf,\n",417 " X,\n",418 " plot_method=\"contour\",\n",419 " colors=\"k\",\n",420 " levels=[-1, 0, 1],\n",421 " alpha=0.5,\n",422 " linestyles=[\"--\", \"-\", \"--\"],\n",423 " ax=ax,\n",424 ")\n",425 "# plot support vectors\n",426 "ax.scatter(\n",427 " clf.support_vectors_[:, 0],\n",428 " clf.support_vectors_[:, 1],\n",429 " s=100,\n",430 " linewidth=1,\n",431 " facecolors=\"none\",\n",432 " edgecolors=\"k\",\n",433 ")\n",434 "plt.show()"435 ]436 },437 {438 "cell_type": "code",439 "execution_count": null,440 "id": "7bb6bf92",441 "metadata": {},442 "outputs": [],443 "source": [444 "# clf.kernel\n",445 "clf.intercept_"446 ]447 },448 {449 "cell_type": "code",450 "execution_count": null,451 "id": "9a42c5f9",452 "metadata": {},453 "outputs": [],454 "source": [455 "from sklearn.datasets import make_blobs\n",456 "Xt, yt = make_blobs(n_samples=40, centers=2, random_state=6)"457 ]458 },459 {460 "cell_type": "code",461 "execution_count": null,462 "id": "651f6bd5",463 "metadata": {},464 "outputs": [],465 "source": [466 "Xt"467 ]468 },469 {470 "cell_type": "code",471 "execution_count": null,472 "id": "8eb8476f",473 "metadata": {},474 "outputs": [],475 "source": []476 }477 ],478 "metadata": {479 "kernelspec": {480 "display_name": "nlp",481 "language": "python",482 "name": "python3"483 },484 "language_info": {485 "codemirror_mode": {486 "name": "ipython",487 "version": 3488 },489 "file_extension": ".py",490 "mimetype": "text/x-python",491 "name": "python",492 "nbconvert_exporter": "python",493 "pygments_lexer": "ipython3",494 "version": "3.8.18"495 }496 },497 "nbformat": 4,498 "nbformat_minor": 5499}500 