{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里用来显示 调用scikit-learn实现逻辑回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 0.  0.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 0.  0.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 0.  1.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 0.  0.]]\n",
      "测试集准确率:90.000000%\n"
     ]
    }
   ],
   "source": [
    "data = np.loadtxt(\"../data/2-logistic_regression/data1.txt\", delimiter=\",\",dtype= np.float64)\n",
    "X = data[:, 0:-1]\n",
    "y = data[:, -1]\n",
    "\n",
    "# 划分为训练集和测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
    "\n",
    "# 归一化\n",
    "scaler = StandardScaler()\n",
    "scaler.fit(x_train)\n",
    "x_train = scaler.fit_transform(x_train)\n",
    "x_test = scaler.fit_transform(x_test)\n",
    "\n",
    "# 逻辑回归\n",
    "model = LogisticRegression()\n",
    "model.fit(x_train, y_train)\n",
    "\n",
    "# 预测\n",
    "predict = model.predict(x_test)\n",
    "right = sum(predict == y_test)\n",
    "\n",
    "predict = np.hstack((predict.reshape(-1, 1), y_test.reshape(-1, 1)))  # 将预测值和真实值放在一块,好观察\n",
    "print(predict)\n",
    "print('测试集准确率:%f%%' % (right * 100.0 / predict.shape[0]))  # 计算在测试集上的准确度\n"
   ]
  }
 ],
 "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
}