文章目录
什么是Numpy
应用于大型数值计算
基本语法
import random
import numpy as np
# 使用NUMPY生成数组
t1=np.array([1,2,3])
print(t1)
print(type(t1))
t2 =np.array(range(5,10,2))
print(t2)
t3 = np.arange(4,10,3)
print(t3)
print(type(t2),type(t3))
print(t3.dtype,t2.dtype,t1.dtype)
print('*'*100)
# numpy 中的数据类型
t4 = np.arange(4,10,2,dtype='float32')
print(t4)
print(t4.dtype)
# numpy中的布尔类型
t5 = np.array([1,1,0,0,0,0,1,],dtype=bool)
print(t5)
print(t5.dtype)
print('+'*100)
# 改换数据类型
t6 = t5.astype('int8')
print(t6.dtype)
t7 = t4.astype('bool')
print(t7,t7.dtype)
# numpy生成小数
t8 = np.array([random.random() for i in range(10)])
print(t8)
print(t8.dtype)
print('/'*100)
# 保留小数
t9 = np.round(t8,4)
print(t9)
运行结果
数组的形状语法
import numpy as np
t1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(t1,t1.shape,t1.dtype)
t2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(t2,t2.shape,t2.dtype)
t3 = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,22,11]]])
print(t3,t3.shape,t3.dtype)
print('*'*100)
t4 = np.array([[[11,22,33],[44,55,66]],[[77,88,99],[44,55,66]]])
print(t4.shape,t4,t4.dtype)
# 更改数字的形状
t5 = np.arange(14)
print(t5)
print(t5.reshape((2,7)))
t6 = np.arange(24)
print(t6.reshape((2,3,4)))
print(t6.reshape(24,)) # 一维数组
print(t6.reshape(1,24)) # 二维数组
print("8"*100)
print(t6.flatten()) # 展开为一维数组
运行结果展示
数组的运算
# 数组运算
import numpy as np
t1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
t2 = t1+2
print(t2)
t3 = t1*2
print(t3)
t4 = t1/2
t5 = t1/0
print(t4,t5) # inf 代表无穷大
print('*'*100)
# 数组与数组之间的运算
t6 = np.arange(100,109).reshape(3,3)
print(t1*t6,t1+t6)
t7 = np.arange(0,3)
print(t7-t1)
t8 = np.arange(3).reshape(3,1)
print(t8-t1)
程序运行结果
numpy读取数据与存储数据
import numpy as np
file_path_1 = r'G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\GB_video_data_numbers.csv'
file_path_2 = r"G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\US_video_data_numbers.csv"
t1 = np.loadtxt(file_path_1,delimiter=',',dtype=int)
t2 = np.loadtxt(file_path_2,delimiter=',',dtype=int)
print(t1)
print("*"*100)
print(t2)
运行结果展示
Numpy实现转置
# 数组转置实现
t3 = np.arange(24).reshape((4,6))
print(t3)
print(t3.transpose())
print('*'*100)
print(t3.T)
print('/'*100)
print(t3.swapaxes(1,0))
程序运行结果
Numpy索引与切片
import numpy as np
file_path_1 = r'G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\GB_video_data_numbers.csv'
file_path_2 = r"G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\US_video_data_numbers.csv"
t1 = np.loadtxt(file_path_1,delimiter=',',dtype='int32')
t2 = np.loadtxt(file_path_2,delimiter=',',dtype=int,unpack=True) # unpack实现转置
# 取行
print(t2)
print(t2[2],t2[0])
# 取连续的行
print(t2[2:])
print('/'*100)
# 取不连续的多行
print(t1[1],t1[10],t1[20])
print('9'*100)
# 取列
print(t1[:,0])
# 取连续的多列
print(t1[:,2:])
# 取不连续多列
print(t1[:,[0,2]])
# 取行列,三行四列
a = t1[2,3]
print(a,type(a))
print(t1)
print(np.shape(t1))
# 取多行多列 第三行到第五行,第三列到第四列
# 取行列交叉点的位置
print(t1[1:5,2:4])
# 取多个不相邻的点,取(0,0)(1,1)(2,3)
print(t1[[0,1,2],[1,2,3]])
运行结果展示
Numpy中数值修改
a = t1[5,:]
print(a)
print(a<1000)
a[a<1000]=3
print(a)
print(np.where(a<1000,0,1))
t3 = np.arange(12).reshape(3,4)
print(t3)
print(t3.clip(10,11))
t3 = t3.astype(float)
t3[1,1]=np.nan
print(t3)
运行结果展示
Numpy读取数据Matplotlib绘图
import numpy
from matplotlib import pyplot as plt
import numpy as np
file_path_1 = r'G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\GB_video_data_numbers.csv'
file_path_2 = r"G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\US_video_data_numbers.csv"
t_us = numpy.loadtxt(file_path_2,delimiter=',',dtype='int')
t_us_comments = t_us[:,-1]
# 选择部分数据
t_us_comments = t_us_comments[t_us_comments<=5000]
print(t_us_comments)
print(t_us_comments.min(),t_us_comments.max())
# 取组距
d = 100
num_data = (t_us_comments.max()-t_us_comments.min())//d
# 绘图
plt.figure(figsize=(10,10),dpi=90)
plt.hist(t_us_comments,num_data)
plt.show()
···
## 运行结果展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/dce454454b6147ffae67aaf80fc2c424.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5L2g55qE56yR5b6I5aSV6Ziz,size_20,color_FFFFFF,t_70,g_se,x_16)
## 绘制Numpy数据中两个数据之间的关系
```
iimport numpy as np
from matplotlib import font_manager
from matplotlib import pyplot as plt
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')
file_path_1 = r'G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\GB_video_data_numbers.csv'
file_path_2 = r"G:\钉钉课件\数据分析资料(2)\day03\code\youtube_video_data\US_video_data_numbers.csv"
t_uk = np.loadtxt(file_path_1,delimiter=',',dtype='int')
# 选择喜欢的数据比50万小的
t_uk = t_uk[t_uk[:,1]<=500000]
t_uk = t_uk[t_uk[:,-1]<=500000]
t_uk_comments = t_uk[:,-1]
t_uk_love = t_uk[:,1]
# 绘图
plt.figure(figsize=(10,10),dpi=90)
# 添加描述信息
plt.ylabel('评论数',fontproperties=my_font)
plt.xlabel('喜欢数',fontproperties=my_font)
plt.title('二者之间的关系',fontproperties=my_font)
plt.scatter(t_uk_love,t_uk_comments)
plt.show()
```
## 运行结果展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/31c504bfb6444453a5a33467f3d4407d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5L2g55qE56yR5b6I5aSV6Ziz,size_20,color_FFFFFF,t_70,g_se,x_16)
## Numpy中数组的拼接
```
import numpy as np
t1 = np.arange(12).reshape(3,4).astype('int')
t2 = np.arange(12,24).reshape(3,4).astype('int')
# 竖直拼接
t3 = np.vstack((t1,t2))
# 水平拼接
t4 = np.hstack((t1,t2))
print(t3)
print(t4)
```
## 运行结果展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/21ad9103000b49169a769e609b28e5bf.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5L2g55qE56yR5b6I5aSV6Ziz,size_9,color_FFFFFF,t_70,g_se,x_16)
## Numpy数组中的行列交换
```
# 换行
t3[[1,2],:] = t3[[2,1],:]
print(t3)
# 换列
t3[:,[2,1]] = t3[:,[1,2]]
print(t3)
```![在这里插入图片描述](https://img-blog.csdnimg.cn/57844aaf77484a3d9a46ee2f269824d2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5L2g55qE56yR5b6I5aSV6Ziz,size_11,color_FFFFFF,t_70,g_se,x_16)