numpy的概念
一个重在计算且是大部分Python科学计算库的基础库,多用于在大型、多维数组上执行数值运算
数组形状
按数组的值分类
- 一维数组
t1 = np.arange(12)
print(t1)
#array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) #一维数组
print(t1.shape)
#(12,) #只有一个值时表示该元组内元素的个数
- 二维数组
t2 = np.array([[1,2,3],[4,5,6]])
print(t2)
#array([[1, 2, 3], #二维数组
# [4, 5, 6]])
print(t2.shape)
#(2, 3) #2表示行数,3表示列数
- 三维数组
t3 = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(t3)
'''
array([[[ 1, 2, 3], #三维数组
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
'''
print(t3.shape)
#(2, 2, 3) #2块数,2块数里有2行,3块数里有3列
按轴分类(axis)
在numpy中可以理解为方向,使用0,1,2…数字表示,对于一个一维数组,只有一个0轴,对于2维数组(shape(2,2)),有0轴和1轴,对于三维数组(shape(2,2, 3)),有0,1,2轴
- 一维数组 0轴
- 二维数组,0和1轴
- 三维数组的轴 0,1,2轴
numpy中常见的数据类型(拓展)
数组形状转换
- 一维换多维
t4 = np.arange(12)
print(t4.reshape(3,4))
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
tips:改变的形状要与原值匹配,t4 3行4列可分,3行5列不可分
ValueError: cannot reshape array of size 12 into shape (3,5)
'''
t5 = np.arange(24).reshape((2,3,4))
print(t5)
'''
分成2块,每块3行4列数据
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
'''
print(t5.reshape((4,6)))
'''
tips:reshape()是有返回值的,同时调用的t5这个变量本身没有发生改变
1,如果要调用reshape()的返回值内容,使用新变量接收 t6 = t5.reshape((4,6))
2,如果使用原调用的变量t5去接收,则是对数据进行原地修改,没有返回值,t5发生改变 t5=t5.reshape((4,6))
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
'''
- 二维换一维
t5 = t5.reshape((4, 6))
t6 = t5.reshape((24,)) #reshape里只有1个值才会获得一维数组
print(t6)
t7 = t5.reshape((t5.shape[0]*t5.shape[1],)) #t5的行数*t5的列数
print(t7)
print(t5.flatten()) #在二维情况下将数组展开为一维
'''
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
'''
t8 = t5.reshape(1,24)
print(t8)
'''
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]]
tips:t8的数组形状不是一维数组,是1行24列二维数组
'''
数组计算
数组计算的广播原则,如果两个数组的后缘维度(即从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为他们是广播兼容的,广播会在缺失 和/或 长度为1的维度上进行
- 数组和数的计算,数组内的每个数都与数进行运算
import numpy as np
#数组和数的计算时,数组内的每个数都与数进行运算
t1 = np.arange(24).reshape((4,6))
#print(t1.shape)
'''
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
'''
print(t1 + 2)
print(t1 - 2)
print(t1 * 2)
print(t1 / 2)
print(t1/0)
'''
加
[[ 2 3 4 5 6 7]
[ 8 9 10 11 12 13]
[14 15 16 17 18 19]
[20 21 22 23 24 25]]
减
[[-2 -1 0 1 2 3]
[ 4 5 6 7 8 9]
[10 11 12 13 14 15]
[16 17 18 19 20 21]]
乘
[[ 0 2 4 6 8 10]
[12 14 16 18 20 22]
[24 26 28 30 32 34]
[36 38 40 42 44 46]]
除
[[ 0. 0.5 1. 1.5 2. 2.5]
[ 3. 3.5 4. 4.5 5. 5.5]
[ 6. 6.5 7. 7.5 8. 8.5]
[ 9. 9.5 10. 10.5 11. 11.5]]
除0:nan不是个数字,Inf(inf表示正无穷,-inf表示负无穷)
[[nan inf inf inf inf inf]
[inf inf inf inf inf inf]
[inf inf inf inf inf inf]
[inf inf inf inf inf inf]]
'''
- 数组和数组的计算(同维度),数组与数组与之对应的元素进行计算
import numpy as np
t1 = np.array([[3,4,5,6,7,8],[9,10,11,12,13,14]])
print(t1.shape)
t2 = np.array([[21,22,23,24,25,26],[27,28,29,30,31,32]])
print(t2.shape)
print(t1+t2)
'''
[[24 26 28 30 32 34]
[36 38 40 42 44 46]]
'''
print(t2-t1)
'''
[[18 18 18 18 18 18]
[18 18 18 18 18 18]]
'''
print(t1*t2)
'''
[[ 63 88 115 144 175 208]
[243 280 319 360 403 448]]
'''
print(t2/t1)
'''
[[7. 5.5 4.6 4. 3.57142857 3.25 ]
[3. 2.8 2.63636364 2.5 2.38461538 2.28571429]]
'''
- 数组和数组的计算(不同维度),只要他们在某个维度上相符(行或列),则可以进行对应计算
import numpy as np
#数组和数组在不同维度时,只要他们在某个维度上相符,则可以进行对应计算
###########################可取值############################
t1 = np.array([[3,4,5,6,7,8],[9,10,11,12,13,14]])
#print(t1.shape)
t2 = np.array(range(2)).reshape((2,1))
'''[[0]
[1]]'''
#print(t1-t2)
'''
t1对应t2的是行数,行数内的每个元素都进行计算
[[ 3 4 5 6 7 8] t1:[3,4,5,6,7,8] t2:[0]
[ 8 9 10 11 12 13]] t1:[9,10,11,12,13,14] t2:[1]
'''
t3 = np.array(range(1,7))
'''[1 2 3 4 5 6]'''
#print(t1*t3)
'''
t1对应t3的列数,则可以进行计算,t1的2行数据都与t2的1行数据进行对应元素(列数)计算
[[ 3 8 15 24 35 48] t1:[3,4,5,6,7,8] t2:[1 2 3 4 5 6]
[ 9 20 33 48 65 84]] t1:[9,10,11,12,13,14] t2:[1 2 3 4 5 6]
'''
###########################不可取值############################
t4 = np.arange(24).reshape((3,2,4))
'''
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
[[16 17 18 19]
[20 21 22 23]]]
'''
t5 = np.arange(12).reshape((3,4))
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
'''
'''各维度都取不到对应值时,无法计算
print(t4-t3)
ValueError: operands could not be broadcast together with shapes (3,2,4) (6,)
print(t1*t5)
ValueError: operands could not be broadcast together with shapes (2,6) (3,4)
'''
numpy操作数据
使用方法
读取数据的参数补充
数值修改(给nan赋均值)
import numpy as np
def fill_ndarray(a):
for i in range(a.shape[1]): #遍历每一列
temp_col = a[:,i] #当前这一列
nan_num = np.count_nonzero(temp_col != temp_col)
if nan_num != 0: #不为0,有nan值
temp_not_nan_col = temp_col[temp_col == temp_col] #nan当列不为nan的值
#选中当前为nan的位置,把nan当列的均值赋值给nan
temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()
return a
if __name__ == '__main__':
a = np.arange(12).reshape((3, 4)).astype('float')
a[1, 2:] = np.nan
print(a)
'''
[[ 0. 1. 2. 3.]
[ 4. 5. nan nan]
[ 8. 9. 10. 11.]]
'''
a = fill_ndarray(a)
print(a)
'''
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]
'''
多数据取值并绘图
- 获取极值后判断组距获得组数,根据绘制结果调整,离散数据考虑放弃,选择密集的点位继续绘图找到可供观察规律的数据
#绘制直方图(YouTube评论数据)
import numpy as np
from matplotlib import pyplot as plt
us_file_path = 'US_video_data_numbers.csv'
gb_file_path = 'GB_video_data_numbers.csv'
#t1 = np.loadtxt(us_file_path,delimiter=',',dtype='int',unpack=True)
t_us = np.loadtxt(us_file_path,delimiter=',',dtype='int')
#获取评论(最后一列)
t_us_comment = t_us[:,-1]
t_us_comment = t_us_comment[t_us_comment<=5000]
print(t_us_comment.max(),t_us_comment.min())
d = 250 #组距
bin_nums = (t_us_comment.max()-t_us_comment.min())//d
plt.figure(figsize=(20,8),dpi=80)
plt.hist(t_us_comment,bin_nums)
plt.show()
#2个信息对比,作散点图,找分布规律
import numpy as np
from matplotlib import pyplot as plt
us_file_path = 'US_video_data_numbers.csv'
gb_file_path = 'GB_video_data_numbers.csv'
#t1 = np.loadtxt(us_file_path,delimiter=',',dtype='int',unpack=True)
t_us = np.loadtxt(us_file_path,delimiter=',',dtype='int')
#限定范围,使数据图更清晰
#在此处做限定,获得低于50Wlike数的整条数据(包括评论)
t_us = t_us[t_us[:,1]<=500000]
#获取comment like数据
t_us_comment = t_us[:,-1]
t_us_like = t_us[:,1]
plt.scatter(t_us_like,t_us_comment)
plt.show()
数据拼接
- 给数据分别添加识别码,再进行拼接
import numpy as np
#加载原始数据
us_data = 'US_video_data_numbers.csv'
uk_data = 'GB_video_data_numbers.csv'
us_data = np.loadtxt(us_data,delimiter=',',dtype=int)
uk_data = np.loadtxt(uk_data,delimiter=',',dtype=int)
#添加国家信息(使用1和0编码)
#构造全为0的数据
zeros_data = np.zeros((us_data.shape[0], 1)).astype(int)
ones_data = np.ones((uk_data.shape[0], 1)).astype(int)
#分别在数据前列添加国家码
#使用拼接
us_data = np.hstack((us_data,zeros_data))
uk_data = np.hstack((uk_data,ones_data))
#tips:拼接方法里只能传1个值(元组),注意括号,否则报错
#拼接2组数据
final_data = np.vstack((us_data,uk_data))
print(final_data)