numpy模块 02
<title>numpy学习</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS_HTML"></script>
<!-- MathJax configuration -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true,
processEnvironments: true
},
// Center justify equations in code and markdown cells. Elsewhere
// we use CSS to left justify single line equations in code cells.
displayAlign: 'center',
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}},
linebreaks: { automatic: true }
}
});
</script>
<!-- End of mathjax configuration --></head>
<body>
<div tabindex="-1" id="notebook" class="border-box-sizing">
<div class="container" id="notebook-container">
In [121]:
import numpy as np
</div>
为啥使用numpy¶
计算购物车的总共价格¶
In [2]:
shop_car = [2,3,10,5]
shop_price = [10,200,150,50]
</div>
In [3]:
shop_car_np = np.array(shop_car)
</div>
In [4]:
shop_price_np = np.array(shop_price)
</div>
In [5]:
shop_car_np
</div>
<div class="prompt output_prompt">Out[5]:</div>
In [6]:
shop_price_np
</div>
<div class="prompt output_prompt">Out[6]:</div>
In [8]:
np.sum(shop_car_np * shop_price_np) #### 向量运算
</div>
<div class="prompt output_prompt">Out[8]:</div>
In [123]:
shop_car_np * 7 #### 矢量运算
</div>
<div class="prompt output_prompt">Out[123]:</div>
In [125]:
shop_car * 7
</div>
<div class="prompt output_prompt">Out[125]:</div>
常见属性¶
一维数组¶
In [10]:
arr = np.array([1,2,3,4,5,6])
</div>
In [12]:
arr #### 简单的ndarray一维数组
</div>
<div class="prompt output_prompt">Out[12]:</div>
In [14]:
arr.dtype #### 数组的数据类型
</div>
<div class="prompt output_prompt">Out[14]:</div>
In [16]:
arr.size ##### 数组元素的个数
</div>
<div class="prompt output_prompt">Out[16]:</div>
In [17]:
arr.ndim #### 数组的维度
</div>
<div class="prompt output_prompt">Out[17]:</div>
In [18]:
arr.shape
</div>
<div class="prompt output_prompt">Out[18]:</div>
二维数组¶
In [19]:
arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10]])
</div>
In [20]:
arr2
</div>
<div class="prompt output_prompt">Out[20]:</div>
In [21]:
arr2.ndim
</div>
<div class="prompt output_prompt">Out[21]:</div>
In [22]:
arr2.shape
</div>
<div class="prompt output_prompt">Out[22]:</div>
In [23]:
arr2.T #### 高维数组转置
</div>
<div class="prompt output_prompt">Out[23]:</div>
数据类型转换¶
In [24]:
arr
</div>
<div class="prompt output_prompt">Out[24]:</div>
In [25]:
arr.dtype
</div>
<div class="prompt output_prompt">Out[25]:</div>
In [26]:
arr.astype('float') #### 强制转换指定的数据类型
</div>
<div class="prompt output_prompt">Out[26]:</div>
ndarray数组的创建方式¶
In [27]:
arr3 = np.array([1,2,3,4,5,6], dtype='float')
</div>
In [28]:
arr3
</div>
<div class="prompt output_prompt">Out[28]:</div>
In [29]:
np.arange(1,10) ### numpy版的arange
</div>
<div class="prompt output_prompt">Out[29]:</div>
In [30]:
np.arange(10)
</div>
<div class="prompt output_prompt">Out[30]:</div>
In [34]:
np.linspace(1,10, num=5, endpoint=False) ##### 均匀分配多少份
</div>
<div class="prompt output_prompt">Out[34]:</div>
In [36]:
np.linspace? #### 查看函数的使用方法
</div>
<div class="prompt"></div>
In [37]:
np.zeros?
</div>
In [38]:
np.zeros(5)
</div>
<div class="prompt output_prompt">Out[38]:</div>
In [40]:
np.zeros((2,3), dtype='int')
</div>
<div class="prompt output_prompt">Out[40]:</div>
In [41]:
np.ones(5)
</div>
<div class="prompt output_prompt">Out[41]:</div>
In [42]:
np.ones((3,4))
</div>
<div class="prompt output_prompt">Out[42]:</div>
In [43]:
np.empty?
</div>
In [46]:
np.empty((2,3))
</div>
<div class="prompt output_prompt">Out[46]:</div>
In [47]:
np.eye?
</div>
In [51]:
np.eye(5, dtype='int') ### 默认生成5行5列的数组, 并且数组的对角线默认是1
</div>
<div class="prompt output_prompt">Out[51]:</div>
ndarray的索引¶
一维数组¶
In [56]:
arr
</div>
<div class="prompt output_prompt">Out[56]:</div>
In [58]:
#### 在列表中怎么通过索引获取值,在这里也是一样的
arr[2]
</div>
<div class="prompt output_prompt">Out[58]:</div>
二维数组¶
In [59]:
arr2
</div>
<div class="prompt output_prompt">Out[59]:</div>
In [61]:
arr2[0,2] ##### 逗号前面是行索引, 逗号后面是列索引
</div>
<div class="prompt output_prompt">Out[61]:</div>
切片¶
一维数组¶
In [62]:
arr
</div>
<div class="prompt output_prompt">Out[62]:</div>
In [76]:
arr[0:-1]
</div>
<div class="prompt output_prompt">Out[76]:</div>
二维数组¶
In [65]:
arr2
</div>
<div class="prompt output_prompt">Out[65]:</div>
In [74]:
arr2[:,1:3] ##### 和索引的使用方法一致, 逗号前面是行索引 , 后面是列索引
</div>
<div class="prompt output_prompt">Out[74]:</div>
In [75]:
arr2[:, :]
</div>
<div class="prompt output_prompt">Out[75]:</div>
布尔型索引¶
In [77]:
###查询arr中大于4的所有的数据
arr
</div>
<div class="prompt output_prompt">Out[77]:</div>
In [78]:
###查询arr中大于4的所有的数据
arr > 4
</div>
<div class="prompt output_prompt">Out[78]:</div>
In [79]:
arr[arr>4] ##### 布尔型索引
</div>
<div class="prompt output_prompt">Out[79]:</div>
In [80]:
arr2
</div>
<div class="prompt output_prompt">Out[80]:</div>
In [82]:
arr2[arr2 > 8]
</div>
<div class="prompt output_prompt">Out[82]:</div>
通用函数¶
In [84]:
a = -4
np.abs(a)
</div>
<div class="prompt output_prompt">Out[84]:</div>
In [85]:
np.ceil(4.3) #### 向上取整
</div>
<div class="prompt output_prompt">Out[85]:</div>
In [86]:
np.floor(4.6) #### 向下取整
</div>
<div class="prompt output_prompt">Out[86]:</div>
In [87]:
np.rint(4.6)
</div>
<div class="prompt output_prompt">Out[87]:</div>
In [88]:
np.rint(4.4)
</div>
<div class="prompt output_prompt">Out[88]:</div>
In [89]:
np.modf(5.6)
</div>
<div class="prompt output_prompt">Out[89]:</div>
In [90]:
## isnan ### 判断某一个数是否是 nan === not a number (不是一个数)
np.nan
</div>
<div class="prompt output_prompt">Out[90]:</div>
In [91]:
arr
</div>
<div class="prompt output_prompt">Out[91]:</div>
In [94]:
np.sum(arr)
</div>
<div class="prompt output_prompt">Out[94]:</div>
In [95]:
np.cumsum(arr) #### 累加
</div>
<div class="prompt output_prompt">Out[95]:</div>
In [96]:
arr
</div>
<div class="prompt output_prompt">Out[96]:</div>
In [97]:
np.min(arr)
</div>
<div class="prompt output_prompt">Out[97]:</div>
In [98]:
np.max(arr)
</div>
<div class="prompt output_prompt">Out[98]:</div>
In [99]:
np.argmin(arr)
</div>
<div class="prompt output_prompt">Out[99]:</div>
随机数¶
In [100]:
np.random.rand(10) ### 随机生成10个0-1之间的数字
</div>
<div class="prompt output_prompt">Out[100]:</div>
In [106]:
np.random.randint(5)
</div>
<div class="prompt output_prompt">Out[106]:</div>
In [109]:
np.random.choice(5,3) ### 随机生成3个 0-5 之间的数
</div>
<div class="prompt output_prompt">Out[109]:</div>
In [110]:
np.random.shuffle?
</div>
In [111]:
arr
</div>
<div class="prompt output_prompt">Out[111]:</div>
In [112]:
np.random.shuffle(arr) #### 打乱给定的一个数组
</div>
In [113]:
arr
</div>
<div class="prompt output_prompt">Out[113]:</div>
In [114]:
arr
</div>
<div class="prompt output_prompt">Out[114]:</div>
In [119]:
arr.reshape(3,2) #### 改变一维数组的形状成二维数组
</div>
<div class="prompt output_prompt">Out[119]:</div>