numpy是什么:
numpy利器之一:Ndarray
numpy利器之二:索引和切片
numpy和matlab进行对比
numpy通函数:
- `{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 一个例子"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]\n",
"[[ 0 1 2]\n",
" [ 3 4 5]\n",
" [ 6 7 8]\n",
" [ 9 10 11]\n",
" [12 13 14]]\n"
]
},
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[ 0, 1, 2],\n",
" [ 3, 4, 5],\n",
" [ 6, 7, 8],\n",
" [ 9, 10, 11],\n",
" [12, 13, 14]])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nd1 = np.arange(15)\n",
"# 将数据的形状,改变\n",
"nd2 = nd1.reshape(5,3)\n",
"print(nd1)\n",
"print(nd2)\n",
"display(nd1,nd2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 数组的长度是多少\n",
"nd1.size"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2.size"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int32')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2.dtype"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 返回,数组的维度,几维的\n",
"# dimension\n",
"nd1.ndim"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2.ndim"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int32')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2.dtype"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2.itemsize"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2],\n",
" [ 3, 4, 5],\n",
" [ 6, 7, 8],\n",
" [ 9, 10, 11],\n",
" [12, 13, 14]], dtype=int8)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd3 = nd2.astype(np.int8)\n",
"nd3"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 返回数据字节长度\n",
"nd3.itemsize"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 基本操作,运算"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[10, 11, 12],\n",
" [13, 14, 15],\n",
" [16, 17, 18],\n",
" [19, 20, 21],\n",
" [22, 23, 24]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2 + 10"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 4],\n",
" [ 9, 16, 25],\n",
" [ 36, 49, 64],\n",
" [ 81, 100, 121],\n",
" [144, 169, 196]], dtype=int32)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2**2"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 4],\n",
" [ 9, 16, 25],\n",
" [ 36, 49, 64],\n",
" [ 81, 100, 121],\n",
" [144, 169, 196]], dtype=int32)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 进化\n",
"# 第一个阶段:代码熟练\n",
"# 第二个阶段:调bug\n",
"# 第三个阶段:英语,源码,纯英文(30K)\n",
"# 第四个阶段:语言表达 + 管理能力 + 为人处世 + 情商\n",
"np.power(nd2,2)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sin(0)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9999996829318346"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sin(1.57)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.pi"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sin(np.pi/2)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.cos(0)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.5707963267948966"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arcsin(1.0)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, False],\n",
" [False, False, False],\n",
" [False, False, False]])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2 < 8"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.48047277815645156"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.tanh(np.pi/6)"
]
}
],
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
`
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### NumPy提供熟悉的数学函数---通函数"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7.38905609893065"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# e = 2.718\n",
"np.exp(2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7.3890560989306495"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.e**2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7.387524"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2.718**2"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd1 = np.array([1,3,-2])\n",
"nd1.all()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"nd2 = np.array([0,0,3])\n",
"display(nd2.all(),nd2.any())"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([6, 6, 9])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def fun(a):\n",
" a += 3\n",
" return a\n",
"np.apply_along_axis(fun,0,nd2)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"最大值索引是: 6\n",
"最小值的索引是: 5\n"
]
}
],
"source": [
"nd3 = np.array([1,4,2,7,3,-6,9,8,1,4])\n",
"print('最大值索引是:',np.argmax(nd3))\n",
"print('最小值的索引是:',np.argmin(nd3))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[3],\n",
" [6],\n",
" [7]], dtype=int64)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = np.argwhere(nd3 > 4)\n",
"# 二维索引\n",
"index"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 6, 7], dtype=int64)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index.reshape(3)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 6, 7], dtype=int64)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# -1代表倒着数,正着数,3,倒着数-1 == 3\n",
"index.reshape(-1)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[7],\n",
" [9],\n",
" [8]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd3[index]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([7, 9, 8])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd3[index.reshape(-1)]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[7]]],\n",
"\n",
"\n",
" [[[9]]],\n",
"\n",
"\n",
" [[[8]]]])"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd3[index.reshape(3,1,1,1)]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1],\n",
" [ 7],\n",
" [-6],\n",
" [ 9],\n",
" [ 8],\n",
" [ 1]])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# np == numpy 是模块,不是具体对象\n",
"cond = np.argwhere((nd3 > 4) | (nd3 <=1))\n",
"\n",
"nd3[cond]"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "ValueError",
"evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()",
"output_type": "error",
"traceback": [
"\u001b[1;31m-------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margwhere\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m<=\u001b[0m\u001b[0mnd3\u001b[0m\u001b[1;33m<\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m" ,
"\u001b[1;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
]
}
],
"source": [
"# 人可以理解,计算机,不识别\n",
"# Python 如果你以后,加入Python语言开发\n",
"# 增加这样,公式运算\n",
"np.argwhere(1 <= nd3 <4)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 1, 7, 3, 9])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([1, 2, 3, 7, 9])"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2 = np.array([2,1,7,3,9])\n",
"display(nd2)\n",
"nd2.sort()\n",
"nd2"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 4, 2, 7, 3, -6, 9, 8, 1, 4])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd3"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([5, 0, 8, 2, 4, 1, 9, 3, 7, 6], dtype=int64)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([-6, 1, 1, 2, 3, 4, 4, 7, 8, 9])"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 返回的就是排序的索引\n",
"# 排序从小到大\n",
"cond = nd3.argsort()\n",
"display(cond)\n",
"nd3[cond]"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 4, 2, 7, 3, -6, 9, 8, 1, 4])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"5.5"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"3.3"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(nd3)\n",
"# 带有权重的平均值\n",
"display(np.average(nd3,weights=[0,0,0.5,0,0,0,0.5,0,0,0]))\n",
"display(nd3.mean())"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 5, 7, 1, 3])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([0, 1, 1, 1, 0, 1, 0, 1], dtype=int64)"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd3 = np.array([2,5,7,1,3])\n",
"display(nd3)\n",
"# [0,1,2,3,4,5,6,7]\n",
"# [0,1,1,1,0,1,0,1]\n",
"np.bincount(nd3)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 5, 7, 1, 3])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([2, 5, 5, 2, 3])"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"display(nd3)\n",
"\n",
"np.clip(nd3,2,5)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 5, 7, 1, 3])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 2, 7, 14, 15, 18], dtype=int32)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 2, 10, 70, 70, 210], dtype=int32)"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"display(nd3)\n",
"display(nd3.cumsum())\n",
"nd3.cumprod()#累乘"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 5, 7, 1, 3])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd3"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "ValueError",
"evalue": "axes don't match array",
"output_type": "error",
"traceback": [
"\u001b[1;31m-------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnd3\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m" ,
"\u001b[1;31mValueError\u001b[0m: axes don't match array"
]
}
],
"source": [
"# transpose 转置,数据维度大于等于2\n",
"nd3.transpose((5,1))"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[62, 3, 84, 26, 86, 63],\n",
" [73, 51, 4, 2, 64, 46],\n",
" [67, 36, 62, 4, 33, 93],\n",
" [78, 8, 0, 50, 74, 71]])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 矩阵\n",
"A = np.random.randint(0,100,size = (4,6))\n",
"# 二维,表示维度的时候:0,1\n",
"# 4行6列的一个矩阵\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](https://bkimg.cdn.bcebos.com/pic/f2deb48f8c5494ee24548cd127f5e0fe98257e80?x-bce-process=image/watermark,g_7,image_d2F0ZXIvYmFpa2UxNTA=,xp_5,yp_5)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[62, 73, 67, 78],\n",
" [ 3, 51, 36, 8],\n",
" [84, 4, 62, 0],\n",
" [26, 2, 4, 50],\n",
" [86, 64, 33, 74],\n",
" [63, 46, 93, 71]])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.transpose((1,0))"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 4, 5)\n"
]
}
],
"source": [
"B = np.random.randint(0,100,size = (3,4,5))\n",
"# 0,1,2\n",
"print(B.shape)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5, 3, 4)"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"C = B.transpose((2,0,1))\n",
"C.shape"
]
}
],
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,3,5,7,2,4,6,8]\n",
"l[2]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8 8\n"
]
}
],
"source": [
"print(l[7],l[-1])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 5]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l[1:3]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 5, 2, 6]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1,5,2,6有间隔的取数据\n",
"l[::2]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "list indices must be integers or slices, not list",
"output_type": "error",
"traceback": [
"\u001b[1;31m-------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# 1,4,8 --- 索引:0 5 7\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ml\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m" ,
"\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not list"
]
}
],
"source": [
"# 1,4,8 --- 索引:0 5 7\n",
"l[[0,5,7]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### numpy索引和切片 \n",
"### numpy和list类似\n",
"### numpy比list功能更加强大"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 3, 5, 7, 2, 4, 6, 8])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd1 = np.array(l)\n",
"nd1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 列表可以进行的索引和切片,numpy数组都可以"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 4, 8])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd1[[0,5,7]]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 28, 124, 21, 54, 120, 144, 69, 0, 15, 57, 137, 123, 45,\n",
" 105, 57, 63, 80, 91, 76, 32, 138, 71, 45, 80, 103, 7,\n",
" 119, 80, 50, 53, 72, 75, 148, 85, 98, 136, 34, 103, 62,\n",
" 106, 133, 134, 20, 135, 76, 131, 112, 143, 55, 26, 12, 79,\n",
" 135, 132, 9, 43, 5, 29, 140, 53, 59, 133, 60, 19, 149,\n",
" 60, 46, 50, 110, 84, 132, 4, 82, 28, 92, 112, 122, 85,\n",
" 86, 97, 141, 97, 37, 0, 30, 64, 59, 47, 58, 101, 119,\n",
" 9, 70, 92, 41, 129, 29, 143, 90, 143, 94, 146, 53, 147,\n",
" 1, 62, 38, 39])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2 = np.random.randint(0,151,size = 108)\n",
"nd2"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[28, 124, 21, 54, 120, 144, 69, 0, 15, 57, 137, 123, 45, 105, 57, 63, 80, 91, 76, 32, 138, 71, 45, 80, 103, 7, 119, 80, 50, 53, 72, 75, 148, 85, 98, 136, 34, 103, 62, 106, 133, 134, 20, 135, 76, 131, 112, 143, 55, 26, 12, 79, 135, 132, 9, 43, 5, 29, 140, 53, 59, 133, 60, 19, 149, 60, 46, 50, 110, 84, 132, 4, 82, 28, 92, 112, 122, 85, 86, 97, 141, 97, 37, 0, 30, 64, 59, 47, 58, 101, 119, 9, 70, 92, 41, 129, 29, 143, 90, 143, 94, 146, 53, 147, 1, 62, 38, 39]\n"
]
}
],
"source": [
"# numpy可以转换成列表\n",
"l2 = nd2.tolist()\n",
"print(l2)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \n"
]
}
],
"source": [
"print(type(nd2),type(l2))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([124, 144, 137, 123, 138, 148, 136, 133, 134, 135, 131, 143, 135,\n",
" 132, 140, 133, 149, 132, 122, 141, 129, 143, 143, 146, 147])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 108个考试的学生,满分是150分,需要找到考试优秀的学生(大于120分)和不及格的学生\n",
"# 涉及到,索引和切片\n",
"index = nd2 > 120\n",
"\n",
"# 索引 nd2[[2,7,13]]\n",
"# 花式索引nd2[[True,False,False,True]]\n",
"high = nd2[index]\n",
"high"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([28, 21, 54, 0, 15, 57, 45, 57, 32, 45, 7, 50, 53, 34, 20, 55, 26,\n",
" 12, 9, 43, 5, 29, 53, 59, 19, 46, 50, 4, 28, 37, 0, 30, 59, 47,\n",
" 58, 9, 41, 29, 53, 1, 38, 39])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = nd2 < 60\n",
"low = nd2[index]\n",
"low"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([124, 144, 137, 123, 138, 148, 136, 133, 134, 135, 131, 143, 135,\n",
" 132, 140, 133, 149, 132, 122, 141, 129, 143, 143, 146, 147, 28,\n",
" 21, 54, 0, 15, 57, 45, 57, 32, 45, 7, 50, 53, 34,\n",
" 20, 55, 26, 12, 9, 43, 5, 29, 53, 59, 19, 46, 50,\n",
" 4, 28, 37, 0, 30, 59, 47, 58, 9, 41, 29, 53, 1,\n",
" 38, 39])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.concatenate([high,low])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'>' not supported between instances of 'list' and 'int'",
"output_type": "error",
"traceback": [
"\u001b[1;31m-------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ml2\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m120\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m" ,
"\u001b[1;31mTypeError\u001b[0m: '>' not supported between instances of 'list' and 'int'"
]
}
],
"source": [
"l2 > 120"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "list indices must be integers or slices, not list",
"output_type": "error",
"traceback": [
"\u001b[1;31m-------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ml2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m" ,
"\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not list"
]
}
],
"source": [
"l2[[True,False]]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([28, 21, 54, 0, 15, 57, 45, 57, 32, 45, 7, 50, 53, 34, 20, 55, 26,\n",
" 12, 9, 43, 5, 29, 53, 59, 19, 46, 50, 4, 28, 37, 0, 30, 59, 47,\n",
" 58, 9, 41, 29, 53, 1, 38, 39])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 菩萨心肠,将那些分数在55到59之间的学生,提高到60分\n",
"low"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 28, 124, 21, 54, 120, 144, 69, 0, 15, 57, 137, 123, 45,\n",
" 105, 57, 63, 80, 91, 76, 32, 138, 71, 45, 80, 103, 7,\n",
" 119, 80, 50, 53, 72, 75, 148, 85, 98, 136, 34, 103, 62,\n",
" 106, 133, 134, 20, 135, 76, 131, 112, 143, 55, 26, 12, 79,\n",
" 135, 132, 9, 43, 5, 29, 140, 53, 59, 133, 60, 19, 149,\n",
" 60, 46, 50, 110, 84, 132, 4, 82, 28, 92, 112, 122, 85,\n",
" 86, 97, 141, 97, 37, 0, 30, 64, 59, 47, 58, 101, 119,\n",
" 9, 70, 92, 41, 129, 29, 143, 90, 143, 94, 146, 53, 147,\n",
" 1, 62, 38, 39])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"cond1 = nd2 <=59\n",
"cond2 = nd2 >=55\n",
"\n",
"cond = cond1 & cond2\n",
"nd2[cond] = 60"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 28, 124, 21, 54, 120, 144, 69, 0, 15, 60, 137, 123, 45,\n",
" 105, 60, 63, 80, 91, 76, 32, 138, 71, 45, 80, 103, 7,\n",
" 119, 80, 50, 53, 72, 75, 148, 85, 98, 136, 34, 103, 62,\n",
" 106, 133, 134, 20, 135, 76, 131, 112, 143, 60, 26, 12, 79,\n",
" 135, 132, 9, 43, 5, 29, 140, 53, 60, 133, 60, 19, 149,\n",
" 60, 46, 50, 110, 84, 132, 4, 82, 28, 92, 112, 122, 85,\n",
" 86, 97, 141, 97, 37, 0, 30, 64, 60, 47, 60, 101, 119,\n",
" 9, 70, 92, 41, 129, 29, 143, 90, 143, 94, 146, 53, 147,\n",
" 1, 62, 38, 39])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"cond=(nd2>=55)&(nd2<=59)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 28, 124, 21, 54, 120, 144, 69, 0, 15, 60, 137, 123, 45,\n",
" 105, 60, 63, 80, 91, 76, 32, 138, 71, 45, 80, 103, 7,\n",
" 119, 80, 50, 53, 72, 75, 148, 85, 98, 136, 34, 103, 62,\n",
" 106, 133, 134, 20, 135, 76, 131, 112, 143, 60, 26, 12, 79,\n",
" 135, 132, 9, 43, 5, 29, 140, 53, 60, 133, 60, 19, 149,\n",
" 60, 46, 50, 110, 84, 132, 4, 82, 28, 92, 112, 122, 85,\n",
" 86, 97, 141, 97, 37, 0, 30, 64, 60, 47, 60, 101, 119,\n",
" 9, 70, 92, 41, 129, 29, 143, 90, 143, 94, 146, 53, 147,\n",
" 1, 62, 38, 39])"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 54, 15, 105, 76, 80, 50, 85, 62, 135, 60, 132, 140, 19,\n",
" 110, 28, 86, 0, 60, 92, 90, 147])"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nd2[3::5]"
]
}
],
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import cv2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"img = cv2.imread('./nba2.jpg')\n",
"\n",
"# 计算机识别人脸,模型:算法,特征\n",
"# 已经训练好的\n",
"# 安装了opencv-python下载好了\n",
"# opencv安装到对应的Python环境下面\n",
"# 识别正脸,人脸检测\n",
"# 检测出人脸的区域\n",
"face_zones = face_detector.detectMultiScale(img)\n",
"\n",
"for x,y,w,h in face_zones:\n",
"# cv2 蓝绿红\n",
"# jpg 颜色范围:0 ~ 255\n",
" cv2.rectangle(img,pt1 = (x,y),pt2 = (x+w,y+h),color = [0,0,255],thickness = 2)\n",
"\n",
"cv2.imshow('bai',img)\n",
"\n",
"cv2.waitKey(0)\n",
"\n",
"cv2.destroyAllWindows()"
]
}
],
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}