【数据分析】NumPy

### 文章目录

  • [@[toc]](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray的创建](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.array()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.arange()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.zeros()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.ones()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.full()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.eye()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.random模块](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.random.random()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.random.randint()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.random.choice()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [np.random.shuffle()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray的属性](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.dtype](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.ndim](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.shape](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.size](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.itemsize](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [修改ndarray的维度](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.reshape()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.resize()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.flatten()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray与数的运算](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray与ndarray的运算](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray索引与切片](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray索引](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray切片](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray的转置](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.T](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [ndarray.transpose()方法](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)
  • [布尔索引](#文章目录 @[toc] ndarray的创建 np.array()方法 np.arange()方法 np.zeros()方法 np.ones()方法 np.full()方法 np.eye()方法 np.random模块 np.random.random()方法 np.random.randint()方法 np.random.choice()方法 np.random.shuffle()方法 ndarray的属性 ndarray.dtype ndarray.ndim ndarray.shape ndarray.size ndarray.itemsize 修改ndarray的维度 ndarray.reshape()方法 ndarray.resize()方法 ndarray.flatten()方法 ndarray与数的运算 ndarray与ndarray的运算 ndarray索引与切片 ndarray索引 ndarray切片 ndarray的转置 ndarray.T ndarray.transpose()方法 布尔索引)

个人主页:丷从心·

系列专栏:数据分析

学习指南:Python学习指南


ndarray的创建

np.array()方法
python 复制代码
import numpy as np

arr = np.array([1, 2, 3])

print(arr)
print(type(arr))
shell 复制代码
[1 2 3]
<class 'numpy.ndarray'>
np.arange()方法
python 复制代码
import numpy as np

arr = np.arange(6)

print(arr)
shell 复制代码
[0 1 2 3 4 5]
np.zeros()方法
python 复制代码
import numpy as np

arr = np.zeros((2, 2))

print(arr)
shell 复制代码
[[0. 0.]
 [0. 0.]]
np.ones()方法
python 复制代码
import numpy as np

arr = np.ones((2, 2))

print(arr)
shell 复制代码
[[1. 1.]
 [1. 1.]]
np.full()方法
python 复制代码
import numpy as np

arr = np.full((2, 2), 2)

print(arr)
shell 复制代码
[[2 2]
 [2 2]]
np.eye()方法
python 复制代码
import numpy as np

arr = np.eye(3)

print(arr)
shell 复制代码
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
np.random模块
np.random.random()方法
python 复制代码
import numpy as np

arr = np.random.random((2, 2))

print(arr)
shell 复制代码
[[0.32149432 0.01896196]
 [0.62318947 0.24221268]]
np.random.randint()方法
python 复制代码
import numpy as np

arr = np.random.randint(0, 10, size=(2, 2))

print(arr)
shell 复制代码
[[6 7]
 [2 3]]
np.random.choice()方法
  • choice()方法用于从数组或列表中随机采样
python 复制代码
import numpy as np

arr = np.arange(6)

print(np.random.choice(arr, size=(2, 3)))
shell 复制代码
[[2 1 0]
 [0 2 5]]
np.random.shuffle()方法
  • shuffle()方法用于将数组随机打乱
python 复制代码
import numpy as np

arr = np.arange(6)
np.random.shuffle(arr)

print(arr)
shell 复制代码
[1 0 5 4 3 2]

ndarray的属性

ndarray.dtype
  • ndarray.dtype表示数组元素的类型
python 复制代码
import numpy as np

arr = np.random.random((2, 2))

print(arr.dtype)
shell 复制代码
float64
ndarray.ndim
  • ndarray.ndim表示数组的维度
python 复制代码
import numpy as np

arr = np.random.random((2, 2))

print(arr.ndim)
shell 复制代码
2
ndarray.shape
  • ndarray.shape表示数组的各轴长度
python 复制代码
import numpy as np

arr = np.random.random((2, 2))

print(arr.shape)
shell 复制代码
(2, 2)
ndarray.size
  • ndarray.size表示数组的元素总数量
python 复制代码
import numpy as np

arr = np.random.random((2, 2))

print(arr.size)
shell 复制代码
4
ndarray.itemsize
  • ndarray.itemsize表示数组每个元素占的字节数
python 复制代码
import numpy as np

arr = np.random.random((2, 2))

print(arr.itemsize)
shell 复制代码
8

修改ndarray的维度

ndarray.reshape()方法
  • reshape()方法不会修改数组本身,只返回修改后的结果
python 复制代码
import numpy as np

arr_1 = np.arange(6)

arr_1.reshape((2, 3))
print(arr_1)

arr_2 = arr_1.reshape((2, 3))
print(arr_2)
shell 复制代码
[0 1 2 3 4 5]
[[0 1 2]
 [3 4 5]]
ndarray.resize()方法
  • resize()方法修改数组本身,没有返回值
python 复制代码
import numpy as np

arr = np.arange(6)
arr.resize((2, 3))

print(arr)
shell 复制代码
[[0 1 2]
 [3 4 5]]
ndarray.flatten()方法
  • flatten()方法用于将多维数组转变为一维数组
python 复制代码
import numpy as np

arr = np.arange(6)
arr.resize((2, 3))

print(arr.flatten())
shell 复制代码
[0 1 2 3 4 5]

ndarray与数的运算

python 复制代码
import numpy as np

arr = np.random.random((3, 4))
arr *= 10

print(arr.round(2))
shell 复制代码
[[6.61 5.87 3.59 9.65]
 [9.91 5.98 2.7  5.72]
 [2.76 3.68 2.4  2.79]]
  • round()方法用于修改保留小数的位数

ndarray与ndarray的运算

python 复制代码
import numpy as np

arr_1 = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])

arr_2 = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])

print(arr_1 * arr_2)
shell 复制代码
[[ 1  4  9]
 [16 25 36]
 [49 64 81]]
python 复制代码
import numpy as np

arr_1 = np.random.randint(0, 10, size=(3, 4))  # 3 行 4 列
arr_2 = np.random.randint(0, 10, size=(3, 1))  # 3 行 1 列

arr_3 = arr_1 - arr_2

print(arr_1)
print(arr_2)
print(arr_3)
shell 复制代码
[[9 5 1 6]
 [4 4 9 5]
 [9 0 7 3]]
[[3]
 [7]
 [8]]
[[ 6  2 -2  3]
 [-3 -3  2 -2]
 [ 1 -8 -1 -5]]
python 复制代码
import numpy as np

arr_1 = np.random.randint(0, 10, size=(3, 4))  # 3 行 4 列
arr_2 = np.random.randint(0, 10, size=(1, 4))  # 1 行 4 列

arr_3 = arr_1 - arr_2

print(arr_1)
print(arr_2)
print(arr_3)
shell 复制代码
[[0 1 1 8]
 [3 3 6 3]
 [4 9 8 6]]
[[3 5 9 0]]
[[-3 -4 -8  8]
 [ 0 -2 -3  3]
 [ 1  4 -1  6]]

ndarray索引与切片

ndarray索引
python 复制代码
import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

print(arr[0])  # 打印第 0 行的元素
print(arr[0, 0])  # 打印第 0 行第 0 列的元素
shell 复制代码
[1 2 3]
1
ndarray切片
python 复制代码
import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

print(arr[:2])  # 打印第 0 ~ 1 行的元素
print(arr[:2, :2])  # 打印第 0 ~ 1 行的第 0 ~ 1 列的元素
shell 复制代码
[[1 2 3]
 [4 5 6]]
[[1 2]
 [4 5]]

ndarray的转置

ndarray.T
  • ndarray.T不会修改数组本身,只返回转置后的结果
python 复制代码
import numpy as np

arr = np.arange(6).reshape((2, 3))

print(arr)
print(arr.T)
shell 复制代码
[[0 1 2]
 [3 4 5]]
[[0 3]
 [1 4]
 [2 5]]
ndarray.transpose()方法
  • transpose()方法修改数组本身,并返回转置后的结果
python 复制代码
import numpy as np

arr = np.arange(6).reshape((2, 3))

print(arr)
print(arr.transpose())
shell 复制代码
[[0 1 2]
 [3 4 5]]
[[0 3]
 [1 4]
 [2 5]]

布尔索引

  • 布尔索引是指将一个布尔数组作为数组索引,返回的数据是布尔数组中Ture对应位置的值
python 复制代码
import numpy as np

arr_1 = np.arange(0, 12).reshape((3, 4))
arr_2 = (arr_1 < 3) | (arr_1 > 8)

print(arr_1)
print(arr_2)

print(arr_1[arr_2])
shell 复制代码
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ True  True  True False]
 [False False False False]
 [False  True  True  True]]
[ 0  1  2  9 10 11]

相关推荐
城数派3 小时前
2000-2024年1km精度人口分布栅格数据(全球/全国/分省/分市)
arcgis·信息可视化·数据分析·excel
城数派3 小时前
1984-2024年中国10米分辨率城市土地利用栅格数据(商业、公服、居住等9类)
arcgis·信息可视化·数据分析·excel
城数派3 小时前
2015-2024年我国1km分辨率逐日地表温度(LST)栅格数据
数据库·arcgis·信息可视化·数据分析·excel
城数派4 小时前
中国全国土壤有机碳密度数据集(2010-2024年)
数据库·arcgis·信息可视化·数据分析·excel
码界筑梦坊7 小时前
354-基于Python的全国水稻数据可视化分析系统
开发语言·python·信息可视化·数据分析·flask·bootstrap·毕业设计
码界筑梦坊7 小时前
336-基于Python的肺癌数据可视化分析预测系统
开发语言·python·信息可视化·数据分析·django·vue·毕业设计
zandy10117 小时前
从拖拽到对话:衡石Agentic BI如何重构企业数据分析的交互范式
重构·数据分析·交互
keke.shengfengpolang9 小时前
大专电子商务转数据分析:把“会卖货”变成“会分析”的四个阶段
数据挖掘·数据分析
源码之家9 小时前
计算机毕业设计:基于Python的美食菜谱数据分析可视化系统 Django框架 爬虫 机器学习 数据分析 可视化 食物 食品 菜谱(建议收藏)✅
爬虫·python·数据分析·django·flask·课程设计·美食
数据科学小丫9 小时前
Power BI 使用
数据分析·数据可视化·powerbi