numpy使用
-
numpy是什么:
- 功能强大的N维数组对象。
- 精密广播功能函数。
- 集成 C/C+和Fortran 代码的工具。
- 强大的线性代数、傅立叶变换和随机数功能
-
numpy利器之一:Ndarray
-
numpy利器之二:索引和切片
-
numpy和matlab进行对比
- matlab非常强大的数据分析软件和工具,语言自成一体
- matlab进行数值和科学计算
- matlab收费,收费很高,如果购买matlab全套服务,一年40万。
- numpy + Python免费
-
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": [
{