文章目录
数据分析第四讲 numpy学习+numpy读取本地数据
一、numpy数组
1.numpy介绍
- numpy(numerical python)
一个在Python中做科学计算的基础库,重在数值计算,也是大部分python科学计算库的基础库,多用于在大型、多维数组上执行数值运算 - scipy
1.在numpy库的基础上增加了众多的数学、科学及工程常用的库函数
2.线性代数、图像处理
2.numpy基础
- 1.创建数组
np.array([1,2,3,4,5,6])
np.array(range(1,7))
np.arange(1,7) - 2.数组的类名
- 3.数据的类型
- 示例
import numpy as np # pip install numpy
# 数组 array list
a = np.array([1, 2, 3, 4, 5, 6])
print(a) # [1 2 3 4 5 6]
b = np.array(range(1, 7))
print(b) # [1 2 3 4 5 6]
c = np.arange(1, 7)
print(c) # [1 2 3 4 5 6]
# 类名
print(type(a)) # <class 'numpy.ndarray'>
# 数据类型
print(a.dtype) # int32
a1 = np.array([1.8, 2.0, 3.4, 4.6, 5.2, 6.9])
print(a1.dtype) # float64
a2 = np.array([True, False])
print(a2.dtype) # bool
a3 = np.array(['YANGYU', 'yangyu', 1, 3])
print(a3.dtype) # <U6
a4 = np.array([1.4, 1, 3])
print(a4.dtype) # float64
3.numpy常见的数据类型
-
numpy常见的数据类型
类型 说明
bool_ 布尔型数据类型(True 或者 False)
int_ 默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
intc 与 C 的 int 类型一样,一般是 int32 或 int 64
intp 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
int8 字节(-128 to 127)
int16 整数(-32768 to 32767)
int32 整数(-2147483648 to 2147483647)
int64 整数(-9223372036854775808 to 9223372036854775807)
uint8 无符号整数(0 to 255)
uint16 无符号整数(0 to 65535)
uint32 无符号整数(0 to 4294967295)
uint64 无符号整数(0 to 18446744073709551615)
float_ float64 类型的简写
float16 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
float32 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
float64 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
complex_ complex128 类型的简写,即 128 位复数
complex64 复数,表示双 32 位浮点数(实数部分和虚数部分)
complex128 复数,表示双 64 位浮点数(实数部分和虚数部分)
numpy 的数值类型实际上是 dtype 对象的实例,并对应唯一的字符,包括 np.bool_,np.int32,np.float32,等等。 -
数据类型的操作
1.指定创建的数组的数据类型
2.修改数组的数据类型
3.修改浮点型的小数位数 -
示例
import numpy as np # pip install numpy
import random
# 数组类型的操作
a = np.array([1,0,1,0], dtype=bool)
print(a,a.dtype) # [ True False True False] bool
a1 = np.array([1,0,1,0],dtype="float32")
print(a1,a1.dtype) # [1. 0. 1. 0.] float32
a2 = np.array([1,0,1,0],dtype="int64")
print(a2,a2.dtype) # [1 0 1 0] int64
# 修改数组的数据类型 int64->float32
a3 = a2.astype("float32")
print(a3.dtype)
print("===========================================")
print(random.random()) # 0.1545033085242674 随机生成0到1的小数
b = np.array([random.random() for i in range(4)])
print(b) # [0.03633417 0.33103144 0.83817931 0.04367941]
# python 四舍五入 round
print(np.round(b,2)) # [0.04 0.33 0.84 0.04]
print(np.around(b,3)) # [0.036 0.331 0.838 0.044]
4.数组的形状
- 数组的形状
1.查看数组维度
2.查看数组形状
3.修改数组形状 - 示例
import numpy as np
# 数组的形状
t1 = np.array(range(6))
# ndim
print(t1, t1.ndim) # [0 1 2 3 4 5] 1表示1维
# 调用类中的属性
print(t1.shape) # (6,) 形状
t2 = np.array([[1, 2, 3], [4, 5, 6]])
# ndim
print(t2, t2.ndim)
'''
[[1 2 3]
[4 5 6]] 2 ''' # 2表示2维
print(t2.shape) # (2, 3) 形状 两行三列
# t21 = np.array([[1,2,3],[4,5]]) # 非对称写法没有意义 [list([1, 2, 3]) list([4, 5])] <class 'numpy.ndarray'> 1 表示1维
# print(t21,type(t21),t21.ndim)
# print(t21.shape) # (2,) # 形状 认为它是个1维数组
t3 = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
print(t3)
'''
[[[1 2 3]
[4 5 6]
[7 8 9]]]'''
print(t3.ndim) # 3 表示3维
print(t3.shape) # 形状(1, 3, 3)
t3_2 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(t3_2)
'''
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]'''
print(t3_2.ndim) # 3 表示3维
print(t3_2.shape) # 形状(2, 2, 3)
# 修改数组的形状
# 1维变2维
t1 = t1.reshape(2, 3) # reshape 函数不会修改本身,需要一个接收变量
print(t1)
'''
[[0 1 2]
[3 4 5]]'''
print(t1.ndim) # 2
# 3维变1维
t11 = t3_2.reshape(12)
print(t11) # [ 1 2 3 4 5 6 7 8 9 10 11 12]
print(t11.ndim) # 1
# t2的行*列 2维变1维
t2 = t2.reshape(t2.shape[0]*t2.shape[1])
print(t2) # [1 2 3 4 5 6]
# t3 3维变1维
t1_1 = t3.reshape(t3.shape[0]*t3.shape[1]*t3.shape[2])
print(t1_1) # [1 2 3 4 5 6 7 8 9]
# 转成1维
res = t3_2.flatten() # 不管是几维,都转成1维
print(res) # [ 1 2 3 4 5 6 7 8 9 10 11 12]
5.数组的计算
- 数组的计算
1.数组和数的计算
2.数组和数组的计算 - 示例
import numpy as np
print(np.zeros(3)) # [0. 0. 0.]
print(np.zeros((3,4))) # 3行4列全零数组
'''
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]'''
print(np.ones((3,4))) # 3行4列全1数组
'''
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]'''
print("="*30)
# 数组和数的计算 + - * /
t = np.array([[1,2,3],[4,5,6]])
print(t+1)
'''
[[2 3 4]
[5 6 7]]'''
print("="*30)
print(t*3)
'''
[[ 3 6 9]
[12 15 18]]'''
print("="*30)
t1 = np.arange(6)
print(t1) # [0 1 2 3 4 5]
print(t1/0) # [nan inf inf inf inf inf] nan:not a number inf无穷大
# 数组和数组的计算 + - * /
t2 = np.array([list(range(3,9)),list(range(4,10))]) # 也可以写t2 = np.array([range(3,9),range(4,10)])
t3 = np.array([range(3,9),range(4,10)])
print(t2)
'''
[[3 4 5 6 7 8]
[4 5 6 7 8 9]]'''
print(t2 + t3) # 对应位相加而不是数组的拼接
'''
[[ 6 8 10 12 14 16]
[ 8 10 12 14 16 18]]'''
print(t2 * t3) # 对应位相乘
'''
[[ 9 16 25 36 49 64]
[16 25 36 49 64 81]]'''
print(t2 / t3) # 对应位相除,除完是浮点型
'''
[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]'''
print("="*30)
t4 = np.array([[1],[2]]) # 2维 2行1列
print(t4)
'''
[[1]
[2]]'''
print(t2)
'''
[[3 4 5 6 7 8]
[4 5 6 7 8 9]]'''
print(t2 + t4) # 2行1列 + 2行6列
'''
[[ 4 5 6 7 8 9]
[ 6 7 8 9 10 11]]'''
print("="*30)
t5 = np.array([1,2,3,4,5,6])
print(t5)
'''
[1 2 3 4 5 6]'''
print(t2)
'''
[[3 4 5 6 7 8]
[4 5 6 7 8 9]]'''
print(t2 + t5)
'''
[[ 4 6 8 10 12 14]
[ 5 7 9 11 13 15]]'''
# 行或者列至少要有一样是相同的才能进行运算
6.数组的广播
二、numpy读取本地数据和索引
1.numpy读取本地数据
- numpy读取数据
以纯文本形式存储的表格数据(以逗号作为分隔符),通常第一行为列名
由于csv便于展示,读取和写入,所以很多地方也是用csv的格式存储和传输中小型的数据。 - 示例
# numpy本地数据的读取
import numpy as np
# 默认dtype=float, delimiter=None, csv文件必须指定分隔符为’,‘
t = np.loadtxt('demo.csv', delimiter=',')
print(t)
'''
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
'''
print("="*30) # 指定类型为dtype=int,int可以加引号
t = np.loadtxt('demo.csv', delimiter=',', dtype=int)
print(t)
'''
[[1 2 3]
[4 5 6]
[7 8 9]]
'''
print("="*30) # 跳过第1行skiprows=1
t = np.loadtxt('demo.csv', delimiter=',', dtype=int, skiprows=1)
print(t)
'''
[[4 5 6]
[7 8 9]]
'''
print("="*30) # 指定读取的列usecols=2,从0开始算,usecols=2是第3列
t = np.loadtxt('demo.csv', delimiter=',', dtype=int, usecols=2)
print(t)
'''
[3 6 9]
'''
print("="*30) # 行、列交换unpack=True 转置
t = np.loadtxt('demo.csv', delimiter=',', dtype=int, unpack=True)
print(t)
'''
[[1 4 7]
[2 5 8]
[3 6 9]]
'''
2.读取数据的方法
np.loadtxt(fname,dtype=np.float,delimiter=None,skiprows=0,usecols=None,unpack=False)
现在这里有一个英国和美国各自youtube1000多个视频的点击,喜欢,不喜欢,评论数量
([“views”,“likes”,“dislikes”,“comment_total”])的csv
3.numpy中的转置
转置是一种变换,对于numpy中的数组来说,就是在对角线方向交换数据,目的也是为了更方
便的去处理数据
np.arange(20).reshape(4,5).transpose()
np.arange(20).reshape(4,5).T
np.arange(20).reshape(4,5).swapaxes(1,0)
4.轴(axis)
在numpy中可以理解为方向,使用0,1,2…数字表示,对于一个一维数组,只有一个0轴,对于2维
数组(shape(2,2)),有0轴和1轴,对于三维数组(shape(3,2, 3)),有0,1,2轴
有了轴的概念之后,我们计算会更加方便,比如计算一个2维数组的平均值,必须指定是计算
哪个方向上面的数字的平均值
轴(axis)
import numpy as np
a = np.arange(20).reshape(4, 5)
print(a)
'''
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]'''
a1 = a.transpose() # 转置,把原先的行列交换 a.transpose()可以简写为a.T
print(a1)
'''
[[ 0 5 10 15]
[ 1 6 11 16]
[ 2 7 12 17]
[ 3 8 13 18]
[ 4 9 14 19]]'''
print(a.T)
'''
[[ 0 5 10 15]
[ 1 6 11 16]
[ 2 7 12 17]
[ 3 8 13 18]
[ 4 9 14 19]]'''
print(a.swapaxes(1, 0)) # 交换轴
'''
[[ 0 5 10 15]
[ 1 6 11 16]
[ 2 7 12 17]
[ 3 8 13 18]
[ 4 9 14 19]]'''
4.numpy索引和切片
取一行
取连续的多行
取不连续的多行
取列
取连续的多列
取不连续的多列
取多行多列的值
取多个不相邻的点
# numpy索引和切片
import numpy as np
t1 = np.loadtxt('GB_video_data_numbers.csv', delimiter=',', dtype=int)
print(t1)
'''
[[7426393 78240 13548 705]
[ 494203 2651 1309 0]
[ 142819 13119 151 1141]
...
[ 109222 4840 35 212]
[ 626223 22962 532 1559]
[ 99228 1699 23 135]]
'''
# 取1行 第3行t1[2] 是从0开始的
print(t1[2])
'''[142819 13119 151 1141]'''
# 取连续的行 取前3行t1[:3]
print(t1[:3])
'''
[[7426393 78240 13548 705]
[ 494203 2651 1309 0]
[ 142819 13119 151 1141]]'''
# 取不连续的行 取第1行和第3行t1[:3:2] t1[开始位置:结束位置:步长]
print(t1[:3:2])
'''
[[7426393 78240 13548 705]
[ 142819 13119 151 1141]]'''
# 取不连续的行 取第1行和第3行t1[[0, 2]]
print(t1[[0, 2]])
'''
[[7426393 78240 13548 705]
[ 142819 13119 151 1141]]'''
# 取列 行,列 t1[:, 0] 取所有行的第1列
print(t1[:, 0])
'''[7426393 494203 142819 ... 109222 626223 99228]'''
# 取连续的列 取前2列 t1[:, 0:2] 或者t1[:, :2]
print(t1[:, :2])
'''
[[7426393 78240]
[ 494203 2651]
[ 142819 13119]
...
[ 109222 4840]
[ 626223 22962]
[ 99228 1699]]
'''
# 取不连续的列 取第1列和第4列 t1[:, [0, 3]]
print(t1[:, [0, 3]])
'''
[[7426393 705]
[ 494203 0]
[ 142819 1141]
...
[ 109222 212]
[ 626223 1559]
[ 99228 135]]'''
# 取多行多列 取第2、3行的前两列 t1[1:3, :2]
print(t1[1:3, :2])
'''
[[494203 2651]
[142819 13119]]'''
# 取多个不相邻的点 取第2行的第2个和第4个
print(t1[[1, 1], [1, 3]])
'''[2651 0]'''
print("=" * 30)
# 取多个不相邻的点 取第3行的第2个和第2行的第4个
print(t1[[2, 1], [1, 3]])
'''[13119 0]'''
# 取多个不相邻的点 取第2行的第1个,第4行的第2个,第2行的第2个
print(t1[[1, 3, 1], [0, 1, 1]])
'''[494203 65729 2651]'''