数据分析:是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出所研究对象的内在规律
数据分析三剑客:Numpy,Pandas,Matplotlib
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
一、创建ndarray
1. 使用np.array()创建
- 一维数据创建
import numpy as np
np.array([1,2,3])
- 二维数组创建
np.array([[1,2,3],[4,5,6]]) #就是列表中放列表
np.array([[1,'two',3],[4,5,6]])
注意:
- numpy默认ndarray的所有元素的类型是相同的
- 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
- 使用matplotlib.pyplot获取一个numpy数组,数据来源于一张图片
import matplotlib.pylab as plt
img_arr = plt.imread('./cat.jpg')
img_arrOut[17]:
array([[[231, 186, 131], [232, 187, 132], [233, 188, 133], ..., [100, 55, 52], [ 92, 48, 49], [ 85, 43, 44]], [[232, 187, 132], [232, 187, 132], [233, 188, 133], ..., [100, 55, 52], [ 92, 48, 49], [ 84, 42, 43]], [[232, 187, 132], [233, 188, 133], [233, 188, 133], ..., [ 99, 54, 51], [ 91, 47, 48], [ 83, 41, 42]], ..., [[199, 119, 82], [199, 119, 82], [200, 120, 83], ..., [189, 99, 64], [187, 97, 62], [187, 97, 62]], [[199, 119, 82], [199, 119, 82], [199, 119, 82], ..., [188, 98, 64], [188, 95, 62], [188, 95, 62]], [[199, 119, 82], [199, 119, 82], [199, 119, 82], ..., [188, 98, 64], [188, 95, 62], [188, 95, 62]]], dtype=uint8)In [7]:
plt.imshow(img_arr)Out[7]:
<matplotlib.image.AxesImage at 0x1ee32cd34e0>In [8]:
plt.imshow(img_arr-100)Out[8]:
<matplotlib.image.AxesImage at 0x1ee331c3518>
- 操作该numpy数据,该操作会同步到图片中
2. 使用np的routines函数创建
5) np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 等差数列
In [13]:np.linspace(0,100,num=6)Out[13]:
array([ 0., 20., 40., 60., 80., 100.])
6) np.arange([start, ]stop, [step, ]dtype=None)
In [10]:np.arange(0,100,2)Out[10]:
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98])
7) np.random.randint(low, high=None, size=None, dtype='l')
In [15]:#固定随机性
#随机因子:系统的时间
np.random.seed(100)
arr = np.random.randint(0,100,size=(4,5))
arrOut[15]:
array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98], [53, 66, 98, 14, 34], [24, 15, 60, 58, 16]])
9) np.random.random(size=None)
生成0到1的随机数,左闭右开 np.random.seed(3)
In [19]:np.random.random(size=(4,5))Out[19]:
array([[0.56229626, 0.00581719, 0.30742321, 0.95018431, 0.12665424], [0.07898787, 0.31135313, 0.63238359, 0.69935892, 0.64196495], [0.92002378, 0.29887635, 0.56874553, 0.17862432, 0.5325737 ], [0.64669147, 0.14206538, 0.58138896, 0.47918994, 0.38641911]])
二、ndarray的属性
4个必记参数: ndim:维度 shape:形状(各维度的长度) size:总长度
dtype:元素类型
In [24]:arr.shape
arr.size
img_arr.size
arr.dtypeOut[24]:
(4, 5)
三、ndarray的基本操作
1. 索引
一维与列表完全一致 多维时同理
In [26]:arrOut[26]:
array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98], [53, 66, 98, 14, 34], [24, 15, 60, 58, 16]])In [29]:
arr[1]Out[29]:
array([48, 10, 94, 52, 98])
2. 切片
一维与列表完全一致 多维时同理
In [30]:#获取二维数组前两行
arr[0:2]Out[30]:
array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98]])In [ ]:
#获取二维数组前两列In [32]:
arr[:,0:2] #arr[hang,lie]Out[32]:
array([[ 8, 24], [48, 10], [53, 66], [24, 15]])In [10]:
#获取二维数组前两行和前两列数据In [33]:
arr[0:2,0:2]Out[33]:
array([[ 8, 24], [48, 10]])In [34]:
#将数组的行倒序
arrOut[34]:
array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98], [53, 66, 98, 14, 34], [24, 15, 60, 58, 16]])In [35]:
arr[::-1]Out[35]:
array([[24, 15, 60, 58, 16], [53, 66, 98, 14, 34], [48, 10, 94, 52, 98], [ 8, 24, 67, 87, 79]])In [36]:
#列倒序
arr[:,::-1]Out[36]:
array([[79, 87, 67, 24, 8], [98, 52, 94, 10, 48], [34, 14, 98, 66, 53], [16, 58, 60, 15, 24]])In [37]:
#全部倒序
arr[::-1,::-1]Out[37]:
array([[16, 58, 60, 15, 24], [34, 14, 98, 66, 53], [98, 52, 94, 10, 48], [79, 87, 67, 24, 8]])In [38]:
#将图片进行全倒置操作
plt.imshow(img_arr)Out[38]:
<matplotlib.image.AxesImage at 0x1ee32fbb160>In [40]:
plt.imshow(img_arr[:,::-1,:])Out[40]:
<matplotlib.image.AxesImage at 0x1ee3304b588>In [42]:
plt.imshow(img_arr[::-1,::-1,:])Out[42]:
<matplotlib.image.AxesImage at 0x1ee32fa8898>In [43]:
#裁剪
plt.imshow(img_arr)Out[43]:
<matplotlib.image.AxesImage at 0x1ee34adbbe0>In [44]:
plt.imshow(img_arr[100:340,170:565,:])Out[44]:
<matplotlib.image.AxesImage at 0x1ee345a22b0>
3. 变形
使用arr.reshape()函数,注意参数是一个tuple!
-
基本使用
1.将一维数组变形成多维数组
arr_1.reshape((2,10))
arr_1.reshape((5,-1))Out[35]:
array([[ 8, 24, 67, 87], [79, 48, 10, 94], [52, 98, 53, 66], [98, 14, 34, 24], [15, 60, 58, 16]])
2.将多维数组变形成一维数组
In [34]:arr_1 = arr.reshape((20,))
arr_1Out[34]:
array([ 8, 24, 67, 87, 79, 48, 10, 94, 52, 98, 53, 66, 98, 14, 34, 24, 15, 60, 58, 16])
4. 级联
- np.concatenate()
1.一维,二维,多维数组的级联,实际操作中级联多为二维数组
In [58]:arrOut[58]:
array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98], [53, 66, 98, 14, 34], [24, 15, 60, 58, 16]])In [61]:
np.concatenate((arr,arr,arr),axis=1)Out[61]:
array([[ 8, 24, 67, 87, 79, 8, 24, 67, 87, 79, 8, 24, 67, 87, 79], [48, 10, 94, 52, 98, 48, 10, 94, 52, 98, 48, 10, 94, 52, 98], [53, 66, 98, 14, 34, 53, 66, 98, 14, 34, 53, 66, 98, 14, 34], [24, 15, 60, 58, 16, 24, 15, 60, 58, 16, 24, 15, 60, 58, 16]])
2.合并两张照片
In [63]:img_3 = np.concatenate((img_arr,img_arr,img_arr),axis=1)
img_9 = np.concatenate((img_3,img_3,img_3),axis=0)
plt.imshow(img_9)Out[63]:
<matplotlib.image.AxesImage at 0x1ee3456aa90>
级联需要注意的点:
- 级联的参数是列表:一定要加中括号或小括号
- 维度必须相同
- 形状相符:在维度保持一致的前提下,如果进行横向(axis=1)级联,必须保证进行级联的数组行数保持一致。如果进行纵向(axis=0)级联,必须保证进行级联的数组列数保持一致。
- 可通过axis参数改变级联的方向
arr_1 = arr.reshape((5,4))
arr_1Out[64]:
array([[ 8, 24, 67, 87], [79, 48, 10, 94], [52, 98, 53, 66], [98, 14, 34, 24], [15, 60, 58, 16]])In [65]:
arrOut[65]:
array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98], [53, 66, 98, 14, 34], [24, 15, 60, 58, 16]])
np.concatenate((arr,arr_1),axis=1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-38-78b5b92d6f99> in <module> ----> 1 np.concatenate((arr,arr_1),axis=1) <__array_function__ internals> in concatenate(*args, **kwargs) ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
四、ndarray的聚合操作
1. 求和np.sum
arr.sum(axis=1)
array([265, 302, 265, 173])
### 2. 最大最小值:np.max/ np.min
同理
3.平均值:np.mean()
In [ ]:3. 其他聚合操作
Function Name NaN-safe Version Description
np.sum np.nansum Compute sum of elements
np.prod np.nanprod Compute product of elements
np.mean np.nanmean Compute mean of elements
np.std np.nanstd Compute standard deviation
np.var np.nanvar Compute variance
np.min np.nanmin Find minimum value
np.max np.nanmax Find maximum value
np.argmin np.nanargmin Find index of minimum value
np.argmax np.nanargmax Find index of maximum value
np.median np.nanmedian Compute median of elements
np.percentile np.nanpercentile Compute rank-based statistics of elements
np.any N/A Evaluate whether any elements are true
np.all N/A Evaluate whether all elements are true
np.power 幂运算
六、ndarray的排序
1. 快速排序
np.sort()与ndarray.sort()都可以,但有区别:
- np.sort()不改变输入
- ndarray.sort()本地处理,不占用空间,但改变输入
np.sort(arr,axis=0)Out[77]:
array([[ 8, 10, 60, 14, 16], [24, 15, 67, 52, 34], [48, 24, 94, 58, 79], [53, 66, 98, 87, 98]])In [74]:
arrOut[74]:
array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98], [53, 66, 98, 14, 34], [24, 15, 60, 58, 16]])In [75]:
arr.sort(axis=0)In [76]:
arrOut[76]:
array([[ 8, 10, 60, 14, 16], [24, 15, 67, 52, 34], [48, 24, 94, 58, 79], [53, 66, 98, 87, 98]])In [ ]: 标签:数据分析,24,arr,98,np,array,Out From: https://www.cnblogs.com/machangwei-8/p/18383768