{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里显示 数字识别 使用scikit-learn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from scipy import io as spio\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测准确度为:94.380000%\n"
     ]
    }
   ],
   "source": [
    "data = spio.loadmat(\"../data/2-logistic_regression/data_digits.mat\")\n",
    "X = data['X']   # 获取X数据,每一行对应一个数字20x20px\n",
    "y = data['y']   # 这里读取mat文件y的shape=(5000, 1)\n",
    "y = np.ravel(y)  # 调用sklearn需要转化成一维的(5000,)\n",
    "\n",
    "model = LogisticRegression()\n",
    "model.fit(X, y) # 拟合\n",
    "\n",
    "predict = model.predict(X)  # 预测\n",
    "\n",
    "print (u\"预测准确度为:%f%%\" % np.mean((predict == y)*100))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}