{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_change = np.random.standard_normal((8,5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.06194442, -0.89430958, -1.02997097, -1.02483934,  0.23420467],\n",
       "       [-0.02107647, -1.02134077, -0.16862601, -0.76812315,  0.23088378],\n",
       "       [-0.60858787,  1.12495596, -0.00839582, -0.24655694,  0.14392832],\n",
       "       [-1.05389325,  0.46955421, -0.00651997,  0.53729582,  0.58068627],\n",
       "       [-0.4846138 , -0.23159692, -0.58753953,  0.75245075,  0.15800634],\n",
       "       [ 1.09804041,  0.68333738, -1.46563512,  0.91608796,  0.34104707],\n",
       "       [-0.01162036, -0.64541231,  0.36110001, -1.45975835,  0.43416529],\n",
       "       [-0.90001005,  0.98124291, -0.29299421,  0.78426655,  0.35322308]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逻辑判断, 如果涨跌幅大于0.5就标记为True 否则为False\n",
    "stock_change > 0.5  # 对每个元素做相应的逻辑判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bool索引\n",
    "temp = stock_change > 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.12495596, 0.53729582, 0.58068627, 0.75245075, 1.09804041,\n",
       "       0.68333738, 0.91608796, 0.98124291, 0.78426655])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change[temp]  # 取出temp中为true的元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断stock_change[0:2, 0:5]是否全是上涨的\n",
    "np.all(stock_change[0:2, 0:5] > 0)  # np.all 判断一个数组内的元素是否都是ture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前5只股票这段期间是否有上涨的\n",
    "np.any(stock_change[:5, :] > 0)  # 判断一个数组内的元素是否有true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  判断前四个股票前四天的涨跌幅 大于0的置为1，否则为0\n",
    "temp = stock_change[:4,:4] > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 1, 0, 0],\n",
       "       [0, 1, 0, 1]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(temp, 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1],\n",
       "       [0, 1, 0, 1],\n",
       "       [1, 1, 0, 0],\n",
       "       [1, 0, 0, 1]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前四个股票前四天的涨跌幅 大于0.5或者小于-0.5的，换为1，否则为0\n",
    "temp = np.logical_or(stock_change[:4,:4] > 0.5, stock_change[:4,:4] <-0.5)\n",
    "np.where(temp, 1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 统计运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 前四只股票前四天的最大涨幅\n",
    "temp = stock_change[:4, :5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.06194442, -0.89430958, -1.02997097, -1.02483934,  0.23420467],\n",
       "       [-0.02107647, -1.02134077, -0.16862601, -0.76812315,  0.23088378],\n",
       "       [-0.60858787,  1.12495596, -0.00839582, -0.24655694,  0.14392832],\n",
       "       [-1.05389325,  0.46955421, -0.00651997,  0.53729582,  0.58068627]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.23420467, 0.23088378, 1.12495596, 0.58068627])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求每一行的最大值\n",
    "np.max(temp, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.02483934, -0.16862601, -0.00839582,  0.46955421])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求每一行的中位数\n",
    "np.median(temp, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 4, 1, 4])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求每一行最大值的下标\n",
    "np.argmax(temp, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组之间的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 2, 1, 4],\n",
       "       [5, 6, 1, 2, 3, 1]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4, 3, 2, 5],\n",
       "       [6, 7, 2, 3, 4, 2]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组与标量的运算  每个元素进行对应的加减乘除\n",
    "arr + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr_2 = np.linspace(1,12, 12).reshape((2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.,  4.,  5.,  6.],\n",
       "       [ 7.,  8.,  9., 10., 11., 12.]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.,  4.,  6.,  6.,  6., 10.],\n",
       "       [12., 14., 10., 12., 14., 13.]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同形状数组（同型数组）加减乘除   对应元素进行加减乘除\n",
    "arr + arr_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "# \n",
    "arr_3 = np.array([[4], [6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4],\n",
       "       [6]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 2, 1, 4],\n",
       "       [5, 6, 1, 2, 3, 1]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  6,  7,  6,  5,  8],\n",
       "       [11, 12,  7,  8,  9,  7]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr + arr_3  # 先以复制列的形式对arr_3拓展成（2，6）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr_4 = np.array([1,2,3,4,5,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6,  6,  6, 10],\n",
       "       [ 6,  8,  4,  6,  8,  7]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr + arr_4   # 先以复制列的形式对arr_4拓展成（2，6）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "score = [[80, 86],\n",
    "[82, 80],\n",
    "[85, 78],\n",
    "[90, 90],\n",
    "[86, 82],\n",
    "[82, 90],\n",
    "[78, 80],\n",
    "[92, 94]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "score = np.array(score)  # 形状(8,2)    (2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构造右矩阵（权重矩阵）\n",
    "w = np.array([[0.3], [0.7]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84.2],\n",
       "       [80.6],\n",
       "       [80.1],\n",
       "       [90. ],\n",
       "       [83.2],\n",
       "       [87.6],\n",
       "       [79.4],\n",
       "       [93.4]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵乘法运算\n",
    "np.matmul(score, w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84.2],\n",
       "       [80.6],\n",
       "       [80.1],\n",
       "       [90. ],\n",
       "       [83.2],\n",
       "       [87.6],\n",
       "       [79.4],\n",
       "       [93.4]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(score, w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[800, 860],\n",
       "       [820, 800],\n",
       "       [850, 780],\n",
       "       [900, 900],\n",
       "       [860, 820],\n",
       "       [820, 900],\n",
       "       [780, 800],\n",
       "       [920, 940]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(score, 10)  # 可以进行矩阵和标量的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
