import numpy as np
#全部行都能输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
#为了确保大家都能生成一样的数组, 我们先设置随机数种子
np.random.seed(123)
x1 = np.random.randint(1,10, size=6) # 一维数组
x2 = np.random.randint(1,10, size=(3, 4)) # 二维数组
x3 = np.random.randint(1,10, size=(3, 4, 5)) # 三维数组
x1
x2
x3
array([3, 3, 7, 2, 4, 7])
array([[2, 1, 2, 1], [1, 4, 5, 1], [1, 5, 2, 8]])
array([[[4, 3, 5, 8, 3], [5, 9, 1, 8, 4], [5, 7, 2, 6, 7], [3, 2, 9, 4, 6]], [[1, 3, 7, 3, 5], [5, 7, 4, 1, 7], [5, 8, 7, 8, 2], [6, 8, 3, 5, 9]], [[2, 3, 2, 2, 4], [6, 1, 9, 2, 7], [4, 4, 6, 8, 3], [4, 4, 4, 9, 7]]])
#查看数据的形状
x1.shape
x2.shape
x3.shape
(6,)
(3, 4)
(3, 4, 5)
x1.ndim
x2.ndim
x3.ndim
1
2
3
x1.size
x2.size
x3.size
6
12
60
x1.dtype
x2.dtype
x3.dtype
dtype('int32')
dtype('int32')
dtype('int32')
x1.itemsize
x2.itemsize
x3.itemsize
4
4
4
# 数组的 dtype 为 int8(一个字节)
x = np.array([1,2,3,4,5], dtype = np.int8)
x.itemsize
1
x = np.array([1,2,3,4,5], dtype = np.float64)
x.itemsize
8
m = np.random.randint(1, 100,(2, 3, 4))
m
array([[[10, 88, 15, 84], [71, 13, 55, 28], [39, 18, 62, 75]], [[66, 48, 17, 6], [87, 47, 16, 60], [41, 26, 46, 50]]])
m.reshape(3,8) #不改变原数组,会返回新的数组
array([[ 1, 36, 30, 2, 84, 69, 31, 8], [94, 61, 66, 77, 68, 45, 52, 8], [89, 71, 14, 29, 64, 85, 37, 97]])
m.shape
(2, 3, 4)
m.reshape(3,7) #注意这点,要保证reshape前后数组中的元素是一样的
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-19-e3effb8f525e> in <module> ----> 1 m.reshape(3,7) #注意这点,要保证reshape前后数组中的元素是一样的 ValueError: cannot reshape array of size 24 into shape (3,7)
m = np.random.randint(1, 100,(2, 3, 4))
m
array([[[41, 89, 64, 59], [78, 9, 79, 7], [66, 95, 71, 41]], [[75, 77, 77, 26], [ 8, 14, 45, 2], [42, 79, 57, 88]]])
m.resize(4,6) #改变原数组
m
array([[41, 89, 64, 59, 78, 9], [79, 7, 66, 95, 71, 41], [75, 77, 77, 26, 8, 14], [45, 2, 42, 79, 57, 88]])
m.shape
(4, 6)
m.shape
(2, 3, 4)
m = np.random.randint(1, 100,(2, 3, 4))
m
array([[[ 6, 82, 36, 78], [26, 98, 94, 71], [59, 30, 82, 22]], [[17, 31, 56, 81], [31, 13, 91, 84], [17, 93, 39, 99]]])
n = m.flatten() #不改变原数组
n
array([ 6, 82, 36, 78, 26, 98, 94, 71, 59, 30, 82, 22, 17, 31, 56, 81, 31, 13, 91, 84, 17, 93, 39, 99])
m
array([[[ 6, 82, 36, 78], [26, 98, 94, 71], [59, 30, 82, 22]], [[17, 31, 56, 81], [31, 13, 91, 84], [17, 93, 39, 99]]])
m.ravel()
array([ 6, 82, 36, 78, 26, 98, 94, 71, 59, 30, 82, 22, 17, 31, 56, 81, 31, 13, 91, 84, 17, 93, 39, 99])
m
array([[[36, 59, 39, 2], [53, 18, 46, 23], [54, 90, 75, 1]], [[36, 84, 95, 72], [40, 88, 84, 79], [30, 20, 38, 2]]])
m.reshape(1,-1)
array([[ 6, 82, 36, 78, 26, 98, 94, 71, 59, 30, 82, 22, 17, 31, 56, 81, 31, 13, 91, 84, 17, 93, 39, 99]])
m.reshape(-1,1)
array([[ 6], [82], [36], [78], [26], [98], [94], [71], [59], [30], [82], [22], [17], [31], [56], [81], [31], [13], [91], [84], [17], [93], [39], [99]])
这种改变的就是原数组的形状而已
有时候我们需要把把数组转化为列表来处理数据,因为列表有很多方法
之前我们有学过ndarray.astype()用来修改数组元素的数据类型
也知道怎么把列表 元组变成数组,反过来我再学一个怎么把ndarray变成列表
a = np.array([1, 2, 3, 4, 5, 6, 7, 8]) #列表变数组
a
array([1, 2, 3, 4, 5, 6, 7, 8])
b = a.tolist() #不会改变原数组,重新返回一个新生成的列表
b
[1, 2, 3, 4, 5, 6, 7, 8]
a
array([1, 2, 3, 4, 5, 6, 7, 8])
和字符串 列表 元祖基本上是一样的
[开始位置, 终止位置, 步长和方向]
a = np.arange(5,16)
a
array([ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
索引单个元素
a[1]
6
a[-1]
15
a[-2]
14
多个元素的连续索引
a[0:3]
array([5, 6, 7])
a[3:8]
array([ 8, 9, 10, 11, 12])
a[5:]
array([10, 11, 12, 13, 14, 15])
a[5:-1]
array([10, 11, 12, 13, 14])
间隔索引
a
array([ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
a[0:6:2]
array([5, 7, 9])
a[0::4]
array([ 5, 9, 13])
a[::-2]
array([15, 13, 11, 9, 7, 5])
a = np.array([[1, 2, 3], [4, 5, 6,], [7, 8, 9]])
a
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a[0]
array([1, 2, 3])
a[0:2]
array([[1, 2, 3], [4, 5, 6]])
a[0][1]
2
a[2][:]
array([7, 8, 9])
a
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a[0:2,0] #[行,列]
array([1, 4])
a[:,:]
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a[:,1]
array([2, 5, 8])
a[1]==a[1,:]
array([ True, True, True])
当结果对象是布尔运算(例如比较运算符)的结果时,将使用此类型的高级索引。
x = np.arange(12).reshape(4,3)
x
array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]])
x>5
array([[False, False, False], [False, False, False], [ True, True, True], [ True, True, True]])
x[x>5]
array([ 6, 7, 8, 9, 10, 11])
a = np.array([np.nan,1,2,np.nan,3,4,5])
a
array([nan, 1., 2., nan, 3., 4., 5.])
np.isnan(a)
array([ True, False, False, True, False, False, False])
a[np.isnan(a)]
array([nan, nan])