当前位置:网站首页>数据分析(二)——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中常用的统计函数
边栏推荐
猜你喜欢
Relia Tech活性VEGFR重组蛋白丨小鼠 VEGF120实例展示
An introduction to basic tools for selecting line tools (package church)
飞桨开源社区季度报告来啦,你想知道的都在这里
OpenCV 透视变换
HCIP 第十六天笔记(SVI、生成树协议)
苹果终于认清现实,销量成为优先考虑,iPhone14将不涨价
Nanoprobes EnzMet - 酶金相相关介绍及应用
Basic principle of the bulk of the animation and shape the An animation tip point
工具模板 | 用APOEM方法消除对用户行为的偏见
An animation optimization of traditional guide layer animation
随机推荐
有哪些好用的IT资产管理平台?
js \n\r 换行失败 :【white-space: pre-line;】${} Template Literals
HCIP第十五天笔记(企业网的三层架构、VLAN以及VLAN 的配置)
Golang dictionary map
国产替代风潮下,电子元器件B2B商城系统如何助力企业突围市场竞争
Petri网-2、有向网
如何让history历史记录前带时间戳
Golang 字典 map
typedef关键字的用法
leetcode/字符串中的所有变位词(s1字符串的某个排列是s2的子串)的左索引
TiFlash 计算层概览
使用Typora+EasyBlogImageForTypora写博客,无图床快速上传图片
1, introduction to petri net
The embassy in Iceland reminds Chinese citizens in Iceland to strengthen safety protection
Relia Tech活性VEGFR重组蛋白丨小鼠 VEGF120实例展示
Redis connection pool tool class
为什么手动启动GBase 8c数据库中GTM节点
豪威集团发布新款5000万像素图像传感器OV50E
【框架】idea找不到xxx依赖项怎么办
致一位湖南女孩