1. numpy简介
NumPy(Numerical Python)是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix)),支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
2. numpy的基本操作
2.1 创建数组、矩阵
(1)方法一
a=np.array([1,2,3,4,5,6])
举例:
import numpy as np
a=np.array([1,2,3,4,5,6])
print(a)
print(type(a))
执行结果:
[1 2 3 4 5 6]
<class 'numpy.ndarray'>
(2)方法二
a = np.array(range(1,7))
举例
import numpy as np
a = np.array(range(1,7))
print(a)
print(type(a))
执行结果:
[1 2 3 4 5 6]
<class 'numpy.ndarray'>
(3)方法三
a = np.arange(1,7)
举例
import numpy as np
a = np.arange(1,7)
print(a)
print(type(a))
执行结果:
[1 2 3 4 5 6]
<class 'numpy.ndarray'>
2.2 查看数组、矩阵中数据的类型
2.2.1 dtype 方法
a.dtype
举例
import numpy as np
a = np.arange(1,7)
print(a.dtype)
执行结果:
int32
2.2.2 numpy中常见的数据类型
类型 | 类型代码 | 说明 |
---|---|---|
int8, uint8 | i1, u1 | 有符号和无符号的8位(1字节)整型 |
int16, uint16 | i2, u2 | 有符号和无符号的16位(2字节)整型 |
int32, uint32 | i4, u4 | 有符号和无符号的32位(4字节)整型 |
int64, uint64 | i8, u8 | 有符号和无符号的64位(8字节)整型 |
float16 | f2 | 半精度浮点数 |
float32 | f4或f | 标准的单精度浮点数,与C的float兼容 |
float64 | f8或d | 标准的双精度浮点数,与C的double和python的float对象兼容 |
float128 | f16或g | 扩展精度浮点数 |
complex64、complex128、complex256 | c8、c16、c32 | 分别用两个32位、64位、128位浮点数表示的复数 |
bool | - | 存储True或False的布尔类型 |
2.3 数据类型的操作
2.3.1 制定创建的数组的数据类型
import numpy as np
b = np.array([1, 2, 3, 4], dtype=bool)
b1 = np.array(range(1,9), dtype=int)
b2=np.arange(1,5,dtype=int)
print(b)
print(b1)
print(b2)
执行结果:
[ True True True True]
[1 2 3 4 5 6 7 8]
[1 2 3 4]
2.3.2 修改数组的数据类型
import numpy as np
b = np.array([1, 2, 3, 4], dtype=bool)
b3 = b.astype(np.int)
print(b)
print(b3)
执行结果:
[ True True True True]
[1 1 1 1]
2.3.3 修改浮点数的小数位数
import numpy as np
c = np.array([0.123451, 1.232832, 4.232323, 8.233242])
print(c)
c1 = np.round(c, 3)
print(c1)
执行结果:
[0.123451 1.232832 4.232323 8.233242]
[0.123 1.233 4.232 8.233]
2.4 数组的形状
2.4.1 查看数组的形状
import numpy as np
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(a)
print(a.shape)
执行结果:
[[1 2 3 4]
[5 6 7 8]]
(2, 4)
2.4.2 修改数组的形状
import numpy as np
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
a1 = a.reshape(4,2)
print(a1)
执行结果:
[[1 2]
[3 4]
[5 6]
[7 8]]
或者:
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
print(a)
执行结果:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
2.4.3 将数组转化为一维数据
注意:reshape() 方法转化成的数据并不是一维的
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
print(a)
b=a.reshape(1,12)
print(b)
print(b.shape)
执行结果:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 2 3 4 5 6 7 8 9 10 11 12]]
(1, 12) # 根据b的维度可以看出,b是二维数据,只不过是具有1行或者1列,
# 其存在仍然以 “行*列” 的形式存在
方法:
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
print(a)
b1 = a.flatten()
print(b1)
print(b1.shape)
执行结果:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[ 1 2 3 4 5 6 7 8 9 10 11 12]
(12,) # 根据此处b1的维度可以看出,b1是一维数据
2.5 数组和数的计算
2.5.1 加减法运算
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
print(a)
a1 = a + 1
print(a1)
a2 = a - 1
print(a2)
执行结果:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
2.5.2 乘除法运算
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
print(a)
a1 = a * 10
print(a1)
a2 = a / 5
print(a2)
执行结果:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 10 20 30 40]
[ 50 60 70 80]
[ 90 100 110 120]]
[[0.2 0.4 0.6 0.8]
[1. 1.2 1.4 1.6]
[1.8 2. 2.2 2.4]]
2.6 数组和数组的计算
2.6.1 加减法运算
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
b = np.array(range(10, 130, 10), dtype=int).reshape(3, 4)
sumab = a + b
subab = a - b
print(sumab)
print(subab)
执行结果:
[[ 11 22 33 44]
[ 55 66 77 88]
[ 99 110 121 132]]
[[ -9 -18 -27 -36]
[ -45 -54 -63 -72]
[ -81 -90 -99 -108]]
2.6.2 乘除法运算
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
b = np.array(range(10, 130, 10), dtype=int).reshape(3, 4)
mulab = a * b
divab = a / b
print(mulab)
print(divab)
执行结果:
[[ 10 40 90 160]
[ 250 360 490 640]
[ 810 1000 1210 1440]]
[[0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1]]
2.6.3 广播原则
如果两个数组的后缘维度(trailing dimension,即从末尾算起的维度)的轴长度相符或者其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失和/或长度为1的维度上进行。
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(3, 4)
b = np.array(range(10, 50, 10), dtype=int).reshape(1, 4)
print(a)
print(b)
sumab = a + b
subab = a - b
mulab = a * b
divab = a / b
print("sumab:\n", sumab)
print("subab:\n", subab)
print("mulab:\n", mulab)
print("divab:\n", divab)
执行结果:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[10 20 30 40]]
sumab:
[[11 22 33 44]
[15 26 37 48]
[19 30 41 52]]
subab:
[[ -9 -18 -27 -36]
[ -5 -14 -23 -32]
[ -1 -10 -19 -28]]
mulab:
[[ 10 40 90 160]
[ 50 120 210 320]
[ 90 200 330 480]]
divab:
[[0.1 0.1 0.1 0.1 ]
[0.5 0.3 0.23333333 0.2 ]
[0.9 0.5 0.36666667 0.3 ]]
3. 轴axis的概念与理解
3.1 一维数组
一维数组只有axis=0轴,求和既是求所有数的和
import numpy as np
c = np.array(range(3), dtype=int)
print(c)
# 一维数组只有axis=0方向,求和就是求所有数的和
sum_caxis0 = np.sum(c, axis=0)
print(sum_caxis0)
执行结果:
[0 1 2]
3
3.2 二维数组
import numpy as np
a = np.array(range(1, 13), dtype=int).reshape(2, 6)
print(a)
# 二维数组的axis=0轴指其行方向
sum_axis0 = np.sum(a, axis=0)
print(sum_axis0)
# 二维数组的axis=1轴指其列方向
sum_axis1 = np.sum(a, axis=1)
print(sum_axis1)
执行结果:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
[ 8 10 12 14 16 18]
[21 57]
3.3 三维数组
import numpy as np
b = np.array(range(1, 25), dtype=int).reshape(2, 3, 4)
print(b)
# 三维数组的axis=0轴指其块方向
sum_baxis0 = np.sum(b, axis=0)
print("axis=0:\n", sum_baxis0)
# 三维数组的axis=1轴指其行方向
sum_baxis1 = np.sum(b, axis=1)
print("axis=1:\n", sum_baxis1)
# 三维数组的axis=2轴指其列方向
sum_baxis2 = np.sum(b, axis=2)
print("axis=2:\n", sum_baxis2)
执行结果:
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
axis=0:
[[14 16 18 20]
[22 24 26 28]
[30 32 34 36]]
axis=1:
[[15 18 21 24]
[51 54 57 60]]
axis=2:
[[10 26 42]
[58 74 90]]
4. numpy读取数据
CSV:Comma-Separated Value,逗号分隔值文件。
显示:表格状态。
源文件:换行和逗号分割行列的格式化文本,每一行的数据表示一条记录。
由于csv便于展示、读取和写入,很多场景使用csv格式存储和传输中小型数据。
语法:
np.loadtext(fname,
dtype=np.float,
delimeiter=None,
skiprows=0,
usecols=None,
unpack=False)
参数:
参数 | 解释 |
---|---|
fname | 文件、字符串或产生器,可以是.gz或bz2压缩文件 |
dtype | 数据类型,可选,CSV的字符串一什么数据类型读入数组中,默认np.float |
delimiter | 分割字符串,默认是任何空格,改为 逗号 |
skiprows | 跳过前x行,一般跳过第一行表头 |
usecols | 读取指定的列,索引,元组类型 |
unpack | 如果为True,读如属性将分别写入不同的数组变量;False读入数据将只写入一个数组变量,默认False。相当于转置的作用。 |
import numpy as np
us_file_path = "D:/人工智能课程/【4】14100_HM数据科学库课件/数据分析资料/day03/code/youtube_video_data/US_video_data_numbers.csv"
uk_file_path = "D:/人工智能课程/【4】14100_HM数据科学库课件/数据分析资料/day03/code/youtube_video_data/GB_video_data_numbers.csv"
t1 = np.loadtxt(us_file_path,delimiter=",",dtype="int",unpack=True)
t2 = np.loadtxt(us_file_path,delimiter=",",dtype="int")
print(t1)
print("*"*100)
print(t2)
执行结果:
[[4394029 7860119 5845909 ... 142463 2162240 515000]
[ 320053 185853 576597 ... 4231 41032 34727]
[ 5931 26679 39774 ... 148 1384 195]
[ 46245 0 170708 ... 279 4737 4722]]
********************************************************************
[[4394029 320053 5931 46245]
[7860119 185853 26679 0]
[5845909 576597 39774 170708]
...
[ 142463 4231 148 279]
[2162240 41032 1384 4737]
[ 515000 34727 195 4722]]