1.Numpy读取txt/csv文件
读取数据
import numpy as np# numpy打开本地txt文件world_alcohol = np.genfromtxt("D:\\数据分析\\01_Numpy\\numpy\\world_alcohol.txt", delimiter=",", dtype=str, skip_header=1)print(world_alcohol)# 第一个参数为文件存放路径# delimiter 分隔符# dtype 以哪种数据类型打开# skip_header=1 跳过头信息,不打印第一行
结果输出:
[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0'] ['1986' 'Americas' 'Uruguay' 'Other' '0.5'] ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62'] ... ['1987' 'Africa' 'Malawi' 'Other' '0.75'] ['1989' 'Americas' 'Bahamas' 'Wine' '1.5'] ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
# numpy打开数据后,以矩阵的形式展示,索引值从0开始vector = world_alcohol[2, 4] # 取第二行第四列的值print(vector)
结果输出:
1.62
帮助文档
# 打印帮助文档print(help(np.genfromtxt))
使用help命令查看帮助文档
结果输出:
Help on function genfromtxt in module numpy.lib.npyio:genfromtxt(fname, dtype=, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding='bytes') Load data from a text file, with missing values handled as specified. Each line past the first `skip_header` lines is split at the `delimiter` character, and characters following the `comments` character are discarded. Parameters ----------...
2.Numpy的矩阵操作
构造矩阵
# 导入import numpy as npvector = np.array([5, 10, 15, 20]) # 构造一个numpy的向量matrix = np.array([[5, 10, 15], [20, 25, 30]]) # 构造一个numpy的矩阵print(vector)print(matrix)
结果输出:
[ 5 10 15 20][[ 5 10 15] [20 25 30]]
vector = np.array([5, 10, 15, 20])print("矩阵维度:", vector.shape) # shape属性打印矩阵(向量)的维度print("矩阵数据类型:", vector.dtype) # 打印矩阵中的数据类型
结果输出:
矩阵维度: (4,)矩阵数据类型: int32
类型转换
# numpy 要求array中是数据为同一类型,且自动实现类型转换vector = np.array([5.0, 10, 15])print(vector)print(vector.dtype)
结果输出:
[ 5. 10. 15.]float64
切片操作
vector = np.array([5, 10, 15, 20])# 切片操作,取前三个元素print(vector[0:3])
结果输出:
[ 5 10 15]
matrix = np.array([ [1,2,3], [4,5,6], [7,8,9]])# 矩阵取元素# 取矩阵第二列,所有值vector = matrix[:, 1] # 冒号表示所有样本,1表述列数(前面表示行,后面表示列)print(vector)print("---------")print(matrix[:, 0:2]) # 取前两列所有元素print("---------")print(matrix[:2, :2]) # 取前两列的前两行元素
结果输出:
[2 5 8]---------[[1 2] [4 5] [7 8]]---------[[1 2] [4 5]]
逻辑运算
vector = np.array([5, 10, 15, 20])equal = (vector == 10) # 判断array中的元素是否为10, 对array的每一个元素进行判断print(equal)print(vector[equal]) # 从向量中取返回True的值and_ = (vector == 10) & (vector == 5) # 逻辑运算 与 其他同理print(and_)
结果输出:
[False True False False][10][False False False False]
matrix = np.array([ [1,2,3], [4,5,6], [7,8,9]])matrix == 5 # 矩阵操作同理,判断array中所有元素是否为5
结果输出:
array([[False, False, False], [False, True, False], [False, False, False]])
# array数据类型转换换vector = np.array(['1', '2', '3'])print(vector.dtype) # 打印数据类型 str类型显示未U1vector = vector.astype(float) # 使用astype()实现类型转换,转换为float类型print(vector.dtype)
结果输出:
一般函数
vector = np.array([5, 10, 15, 20])# 求最小值print(vector.min())# 求最大值print(vector.max())
结果输出:
520
matrix = np.array([ [1,2,3], [4,5,6], [7,8,9]])# 按行求和matrix.sum(axis=1) # axis=0表示按列求和
结果输出:
array([ 6, 15, 24])
3.Numpy矩阵属性
常用属性
import numpy as npprint(np.arange(15)) # 生成一个列表(索引)print('--'*10)a = np.arange(15).reshape(3, 5) # reshape函数修改矩阵为3行5列print(a)
结果输出:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]--------------------[[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]]
print(a.shape) # shape属性打印矩阵的行列数print(a.ndim) # ndim查看当前矩阵的维度print(a.dtype) # 查看矩阵数据类型print(a.size) # 查看矩阵中的元素
结果输出:
(3, 5)2int3215
4.Numpy矩阵的初始化
zeros与ones
np.zeros((3, 4)) # 初始化一个3行4列的0矩阵,参数以元组的形式传入,默认为float类型
结果输出:
array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])
np.ones((2, 3, 4),dtype=np.int32) # 初始化一个三维1矩阵, 指定数据类型为int,元组长度决定了矩阵维度
结果输出:
array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]])
np.arange(5, 10, 0.3) # 创建一个等差数列,公差为0.3
结果输出:
array([5. , 5.3, 5.6, 5.9, 6.2, 6.5, 6.8, 7.1, 7.4, 7.7, 8. , 8.3, 8.6, 8.9, 9.2, 9.5, 9.8])
随机矩阵
np.random.random((2, 3)) # 随机矩阵,创建一个2行3列的随机矩阵,默认为0到1之间
结果输出:
array([[0.10536991, 0.49359947, 0.56369024], [0.39357893, 0.78362851, 0.75576749]])
from numpy import pi # 数学中的πnp.linspace(0, 2*pi, 100) # 从0到2π中平均生成100个数
结果输出:
array([0. , 0.06346652, 0.12693304, 0.19039955, 0.25386607, 0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866, 0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126, 0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385, 1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644, 1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903, 1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162, 2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421, 2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 , 2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939, 3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,...
基础运算
a = np.array([20, 30, 40])b = np.array([2, 3, 4])print("a:",a)print("b:",b)c = a - bprint("c:",c) # numpy是对应位置相减c = c - 1print("c-1:",c)print("b^2:", b ** 2) # 平方print("a<30:",a < 30) # 判断每个元素是否小于30
结果输出:
a: [20 30 40]b: [2 3 4]c: [18 27 36]c-1: [17 26 35]b^2: [ 4 9 16]a<30: [ True False False]
a = np.array([ [1, 1], [0, 1]])b = np.array([ [2, 0], [3, 4]])print( a * b) # 点积相乘,对应位置相乘print("------")print(a.dot(b)) # 矩阵相乘print("------")print(np.dot(a, b)) # 同样的矩阵相乘
结果输出:
[[2 0] [0 4]]------[[5 4] [3 4]]------[[5 4] [3 4]]
5.Numpy中的常用函数
平方与幂
B = np.arange(3)print(np.exp(B)) # e的幂运算print(np.sqrt(B)) # 开平方
结果输出:
[1. 2.71828183 7.3890561 ][0. 1. 1.41421356]
取整函数
a = np.floor(10 * np.random.random((3, 4))) # floor 向下取整print(a)
结果输出:
[[8. 0. 7. 6.] [3. 1. 3. 1.] [3. 0. 1. 4.]]
拆分矩阵
aa = a.ravel() # ravel 拆分矩阵,将矩阵拆分为向量print(aa)
结果输出:
[8. 0. 7. 6. 3. 1. 3. 1. 3. 0. 1. 4.]
reshape函数
aa.shape = (6, 2) # 改变矩阵样式, 效果和reshape((6, 2))相同aa
结果输出:
array([[8., 0.], [7., 6.], [3., 1.], [3., 1.], [3., 0.], [1., 4.]])
矩阵转置
aa.T # 转置
结果输出:
array([[8., 7., 3., 3., 3., 1.], [0., 6., 1., 1., 0., 4.]])
矩阵拼接
a = np.floor(10 * np.random.random((2, 3)))b = np.floor(10 * np.random.random((2, 3)))print(a)print(b)print('============')print(np.vstack((a, b))) # 按行拼接print('============')print(np.hstack((a, b))) # 按列拼接
结果输出:
[[5. 6. 7.] [0. 1. 7.]][[6. 2. 3.] [5. 4. 7.]]============[[5. 6. 7.] [0. 1. 7.] [6. 2. 3.] [5. 4. 7.]]============[[5. 6. 7. 6. 2. 3.] [0. 1. 7. 5. 4. 7.]]
a = np.floor(10 * np.random.random((2, 12)))print(a)print("===="*5)print(np.hsplit(a, 3)) # 将a(2行12列)平均切分为3份,按行切分print("===="*5)print(np.hsplit(a, (3, 4))) # 在角标为3的地方切分开一次,角标为4的地方再切分开一次,参数为元组print("===="*5)a = a.Tprint(np.vsplit(a, 3)) # 同理操作,按列切分
结果输出:
[[6. 0. 3. 8. 4. 2. 6. 7. 2. 9. 7. 9.] [2. 3. 5. 5. 3. 2. 5. 6. 8. 2. 6. 4.]]====================[array([[6., 0., 3., 8.], [2., 3., 5., 5.]]), array([[4., 2., 6., 7.], [3., 2., 5., 6.]]), array([[2., 9., 7., 9.], [8., 2., 6., 4.]])]====================[array([[6., 0., 3.], [2., 3., 5.]]), array([[8.], [5.]]), array([[4., 2., 6., 7., 2., 9., 7., 9.], [3., 2., 5., 6., 8., 2., 6., 4.]])]====================[array([[6., 2.], [0., 3.], [3., 5.], [8., 5.]]), array([[4., 3.], [2., 2.], [6., 5.], [7., 6.]]), array([[2., 8.], [9., 2.], [7., 6.], [9., 4.]])]
import numpy as npdata = np.sin(np.arange(20)).reshape(5, 4) # 生成一组数据print(data)ind = data.argmax(axis=0) # 按列查找,返回每列最大值的索引print(ind)data_max = data[ind, range(data.shape[1])] # 取出每一列的最大值print(data_max)
结果输出:
[[ 0. 0.84147098 0.90929743 0.14112001] [-0.7568025 -0.95892427 -0.2794155 0.6569866 ] [ 0.98935825 0.41211849 -0.54402111 -0.99999021] [-0.53657292 0.42016704 0.99060736 0.65028784] [-0.28790332 -0.96139749 -0.75098725 0.14987721]][2 0 3 1][0.98935825 0.84147098 0.99060736 0.6569866 ]
a = np.arange(0, 20, 3)print(a)b = np.tile(a, (4, 2)) # 平铺,行变为原来的多少倍(4),列变为原来的多少倍(2)print(b)
结果输出:
[ 0 3 6 9 12 15 18][[ 0 3 6 9 12 15 18 0 3 6 9 12 15 18] [ 0 3 6 9 12 15 18 0 3 6 9 12 15 18] [ 0 3 6 9 12 15 18 0 3 6 9 12 15 18] [ 0 3 6 9 12 15 18 0 3 6 9 12 15 18]]
矩阵排序
a = np.array([ [4, 3, 5], [1, 2, 1]])print(a)print("---"*5)b = np.sort(a, axis=0) # 按行从小到大排序print(b)print("---"*5)print(np.sort(a, axis=1)) # 按列从小到大排序print("---"*5)index = np.argsort(a) # 获取从小到大排序的索引值print(index)
结果输出:
[[4 3 5] [1 2 1]]---------------[[1 2 1] [4 3 5]]---------------[[3 4 5] [1 1 2]]---------------[[1 0 2] [0 2 1]]