Anonymous avatar Anonymous committed 0996de8

...

Comments (0)

Files changed (1)

ca2/Intro_3.ipynb

     {
      "cell_type": "code",
      "collapsed": false,
-     "input": [],
+     "input": [
+      "iris = datasets.load_iris()\n",
+      "# \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\n",
+      "data = iris.data\n",
+      "# \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430 (\u043d\u043e\u043c\u0435\u0440 \u043a\u043b\u0430\u0441\u0441\u0430)\n",
+      "target = iris.target\n",
+      "\n",
+      "N, n = data.shape"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 3
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "kfold = cross_validation.StratifiedKFold(target, 7)"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 23
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "i, j = 1, 2\n",
+      "info_list = []\n",
+      "for train, test in kfold:\n",
+      "    X_train, X_test = data[train], data[test]\n",
+      "    Y_train, Y_test = target[train], target[test]\n",
+      "    Xij_train = np.c_[X_train[:, i], X_train[:, j]]\n",
+      "    Xij_test = np.c_[X_test[:, i], X_test[:, j]]\n",
+      "    clf = neighbors.KNeighborsClassifier(n_neighbors=3)\n",
+      "    clf.fit(Xij_train, Y_train)\n",
+      "    S = metrics.precision_score(Y_test, clf.predict(Xij_test))\n",
+      "    info = ((i,j), (Xij_train, Y_train), (Xij_test, Y_test), clf, S)\n",
+      "    info_list.append(info)\n",
+      "    print S\n",
+      "    \n",
+      "    \n",
+      "    "
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "0.909090909091\n",
+        "1.0\n",
+        "0.909090909091\n",
+        "0.958333333333\n",
+        "1.0\n",
+        "0.958333333333\n",
+        "0.925925925926\n"
+       ]
+      }
+     ],
+     "prompt_number": 25
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def find_best_clf(ij):\n",
+      "    kfold = cross_validation.StratifiedKFold(target, 7)    \n",
+      "    info_list = []\n",
+      "    for train, test in kfold:\n",
+      "        X_train, X_test = data[train], data[test]\n",
+      "        Y_train, Y_test = target[train], target[test]\n",
+      "        Xij_train = np.c_[X_train[:, i], X_train[:, j]]\n",
+      "        Xij_test = np.c_[X_test[:, i], X_test[:, j]]\n",
+      "        clf = neighbors.KNeighborsClassifier(n_neighbors=3)\n",
+      "        clf.fit(Xij_train, Y_train)\n",
+      "        S = metrics.precision_score(Y_test, clf.predict(Xij_test))\n",
+      "        info = (S, clf)\n",
+      "        info_list.append(info)\n",
+      "    info_list.sort()\n",
+      "    bests = info_list[-2:]\n",
+      "    return bests\n",
+      "    "
+     ],
      "language": "python",
      "metadata": {},
      "outputs": []
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.