当前位置:网站首页>数据分析(二)——numpy
数据分析(二)——numpy
2022-08-03 13:33:00 【flag:卷王!】
numpy:一个在Python中做科学计算的基础库,重在数值计算,也是大部分Python科学计算库的基础库,多用于在大型、多维数组上执行运算。
一、numpy的数组创建
1)数组的创建
import random
import numpy as np
#使用numpy生成数组,得到ndarray类型
t1=np.array([1,2,3])
print(t1)
print(type(t1))
t2=np.array(range(10))
print(t2)
print(type(t2))
t3=np.arange(10)
print(t3)
print(type(t3))
t4=np.arange(4,10,2)
print(t4)
print(t4.dtype)
#numpy中的数据类型
t5=np.array(range(1,4),dtype=float)
print(t5)
print(t5.dtype)
t6=np.array([1,1,0,1,0,0],dtype=bool)
print(t6)
print(t6.dtype)
#调整数据类型
t7=t6.astype("int32")
print(t7)
#numpy中的小数
t8=np.array([random.random() for i in range(10)])
print(t8)
print(t8.dtype)
t9=np.round(t8,2) #round保留小数
print(t9)结果:
numpy中常见的更多数据类型:
2)数组的形状
import random
import numpy as np
t1=np.arange(12)
print(t1)
print(t1.shape)
#二维数组
t2=np.array([[1,2,3],[4,5,6]])
print(t2)
print(t2.shape) #结果第一个数表示行数,第二个表示列数
#三维数组
t3=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(t3)
print(t3.shape) #结果第一个数为块数,第二个表示为每一块中的行数,第三个数表示每一块中的列数
t4=np.arange(12)
print(t4)
#修改数组
t5=t4.reshape((3,4))
print(t5)
t6=t5.flatten() #将t5展开成一维数组
print(t6)结果:
3)numpy读取本地数据和索引
1,轴(axis)
在numpy中可以理解为方向,使用0,1,2......数字表示,对于一个一维数组,只有一个0轴,对于二维数组(shape(2,2)),有0轴和1轴,对于三维数组(shape(2,2,3)),有0,1,2轴

2,读取数据
np.loadtxt(frame,dtype=np.float,delimliter=None,skiprows=0,usecols=None,unpack=False)
3)索引和切片
import random
import numpy as np
#创建数组
t1=np.array([random.randint(1,10) for i in range(12)])
print(t1)
#数组修改
t2=t1.reshape((4,3))
print(t2)
print("*"*30)
#取行
print(t2[2]) #从0开始
print("*"*30)
#取连续多行
print(t2[2:])
print("*"*30)
#取不连续多行
print(t2[[0,1,3]])
print("*"*30)
#取列
print(t2[:,0])
#取多个不相邻的点
a=t2[[0,1,2],[2,1,0]] #注意,此时取得点为(0,2),(1,1),(2,0)
print(a)结果:

4)数值的修改
import random
import numpy as np
#创建数组
t1=np.array([random.randint(1,10) for i in range(12)])
print(t1)
#数组修改
t2=t1.reshape((4,3))
print(t2)
print("*"*30)
#数值的修改
t2[t2<5]=0 #布尔索引
print(t2)
print("*"*30)
print(np.where(t2<=3,0,10)) #三元运算符
print("*"*30)
print(t2.clip(3,6)) #clip裁剪,比3小的为3,比6大的为6结果:

5)数组拼接与行列交换
import random
import numpy as np
#创建二维数组
t1=np.array([[0,1,2,3,4,5],
[6,7,8,9,10,11]])
t2=np.arange(12,24)
t2=t2.reshape(2,6)
#数组的拼接
t3=np.vstack((t1,t2)) #竖直拼接
print(t3)
print("*"*50)
t4=np.hstack((t1,t2)) #水平拼接
print(t4)
print("*"*50)
#数组的行列交换
t1[[0,1],:]=t1[[1,0],:] #行交换
print(t1)
t2[:,[0,2]]=t2[:,[2,0]] #列交换
print(t2) 结果:
6)numpy更多好用的方法
生成随机数:

numpy的注意点copy和view:
- a=b完全不复制,a和b相互影响
- a=b[:],视图的操作,一种切片,会创建新的对象a,但是a的数据完全由b保管,他们两个的数据变化是一致的
- a=b.copy(),复制,a和b互不影响
7)numpy中的nan的注意点
- 两个nan是不相等的
- np.nan!=np.nan
- 利用以上的特性,判断数组中nan的个数
- 由于第二点,那么在判断一个数字是否为nan时可以通过np.isnan()来判断,返回bool类型
- nan和任何值计算都为nan
8)numpy中常用的统计函数
边栏推荐
- Notepad++ install jsonview plugin
- [OpenCV] Book view correction + advertising screen switching Perspective transformation image processing
- An animation based button animation combined with basic code
- Golang 字符串
- PyTorch构建神经网络预测气温(数据集对比,CPU与GPU对比)
- Golang dictionary map
- petri网-1、概论
- The Chinese Embassy in Nigeria issued an emergency safety warning for the area near Zuma Rock in Abuja
- Forrester:行业云帮助中国企业更快适应未来的发展
- [Microservice] Multi-level cache
猜你喜欢

365天挑战LeetCode1000题——Day 048 有序队列 脑筋急转弯

升级农企业务运营建设,智慧供应链管理平台打造“共赢生态链”

飞桨开源社区季度报告来啦,你想知道的都在这里

An introduction to the width tool, deformation tool and lasso tool

TiFlash 计算层概览

工具模板 | 用APOEM方法消除对用户行为的偏见

An introduction to the pen tool, pencil tool and brush tool

Graphic animation and button animation of an animation basic component

【深度学习中的激活函数的整理与使用总结】

Hanyuan Hi-Tech G8032 standard ERPS ring network switch Gigabit 4 optical 10 electrical industrial Ethernet switch ring network + WEB management + SNMP VLAN planning
随机推荐
国产替代风潮下,电子元器件B2B商城系统如何助力企业突围市场竞争
短视频的头号玩家:抖音产品体验报告
有趣的opencv-记录图片二值化和相似度实现
苹果终于认清现实,销量成为优先考虑,iPhone14将不涨价
TensorFlow离线安装包
驻冰岛使馆提醒旅冰中国公民务必加强安全防护
Golang interface interface
【框架】idea找不到xxx依赖项怎么办
中英文说明书丨Abbkine AbFluor 488-鬼笔环肽
PyTorch builds a classification network model (Mnist dataset, fully connected neural network)
Relia Tech活性VEGFR重组蛋白丨小鼠 VEGF120实例展示
【OpenCV】 书本视图矫正 + 广告屏幕切换 透视变换图像处理
Nanoprobes 金纳米颗粒标记试剂丨1.4 nm Nanogold 标记试剂
d写二进制
ideaIU-2020.1下载
sessionStorage of BOM series
农产品企业如何进行全网营销?
15 years of software architect experience summary: In the ML field, 5 pits that beginners have stepped on
IDEA的模板(Templates)
Basic principle of the bulk of the animation and shape the An animation tip point