当前位置:网站首页>NumPy学习挑战第四关-NumPy数组属性
NumPy学习挑战第四关-NumPy数组属性
2022-06-26 06:44:00 【云曦我女神】
NumPy 数组属性
NumPy 数组的维数称为秩(rank),秩就是轴的数量,即数组的维度,一维数组的秩为 1,二维数组的秩为 2,以此类推。
在 NumPy中,每一个线性的数组称为是一个轴(axis),也就是维度(dimensions)。比如说,二维数组相当于是两个一维数组,其中第一个一维数组中每个元素又是一个一维数组。所以一维数组就是 NumPy 中的轴(axis),第一个轴相当于是底层数组,第二个轴是底层数组里的数组。而轴的数量——秩,就是数组的维数。
很多时候可以声明 axis。axis=0,表示沿着第 0 轴进行操作,即对每一列进行操作;axis=1,表示沿着第1轴进行操作,即对每一行进行操作。
NumPy 的数组中比较重要 ndarray 对象属性有:
1、ndarray.ndim
ndarray.ndim 用于返回数组的维数,等于秩。
mport numpy as np
a = np.arange(24)
print (a.ndim) # a 现只有一个维度
# 现在调整其大小
b = a.reshape(2,4,3) # b 现在拥有三个维度
print (b.ndim)
1
3
2、ndarray.shape
ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。比如,一个二维数组,其维度表示"行数"和"列数"。
arr3=np.array([[1,2,3],[4,5,6]])
print(arr3)
print(arr3.shape)
[[1 2 3]
[4 5 6]]
(2, 3)
ndarray.shape 也可以用于调整数组大小。
arr3=np.array([[1,2,3],[4,5,6]])
print(arr3)
print(arr3.shape)
arr3.shape=(3,2)
print(arr3)
[[1 2 3]
[4 5 6]]
(2, 3)
[[1 2]
[3 4]
[5 6]]
也能使用reshape函数来调整数组大小
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print (b)
[[1, 2]
[3, 4]
[5, 6]]
3、ndarray.itemsize
ndarray.itemsize 以字节的形式返回数组中每一个元素的大小。
例如,一个元素类型为 float64 的数组 itemsize 属性值为 8(float64 占用 64 个 bits,每个字节长度为 8,所以 64/8,占用 8 个字节),又如,一个元素类型为 complex32 的数组 item 属性为 4(32/8)。
import numpy as np
# 数组的 dtype 为 int8(一个字节)
x = np.array([1,2,3,4,5], dtype = np.int8)
print (x.itemsize)
# 数组的 dtype 现在为 float64(八个字节)
y = np.array([1,2,3,4,5], dtype = np.float64)
print (y.itemsize)
1
8
4、ndarray.flags
ndarray.flags 返回 ndarray 对象的内存信息,包含以下属性:
arr3=np.array([[1,2,3],[4,5,6]])
print(arr3)
print(arr3.flags)
[[1 2 3]
[4 5 6]]
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
5、np.genfromtxt()
可以通过genfromtxt()函数读取外部文本文件的数据,文本文件类型主要为TXT和CSV文件。
语法格式:
np.genfromtxt(fname,dtype=<class 'float'>,comments='#',delimiter=None,skip_header=0,skip_footer=0,converters=None,missing_values=None,filling_values=None,usecols=None,names=None,)
fname:文件路径
dtype:指定读入数据的数据类型,默认为浮点型,字符型数据需要指定为str
comments:指定注释符,默认为‘#’,即如果源数据的行首有‘#’,将忽略这些行的读入
delimiter:指定数据集的列分割符
skip_header:是否掉过数据集的首行,默认不跳过
converters:将指定列的数据转换为其他数据
miss_values:指定缺失值的标记,如果源数据集含有指定标记,读入后这样的数据就为缺失值
filling_values:指定缺失值的填充值
usecols:指定需要读入哪些列
names:为读入数据的列设置列名称
读入的数据会返回一个numpy对象,接着就可以使用上面的属性来获取数据集的维数ndim等。
6、数组形状处理
import numpy as np
arr1=np.array([1,2,3,4,5,6])#列表创建一维数组
arr2=np.array(((1,2,3),(4,5,6),(7,8,9),(10,11,12)))#嵌套元组创建二维数组,两个元组要用小括号括起
print('二维数组:\n',arr2)
print(arr2.shape)
#改变数组的形状
#reshape返回改变形状后的预览,并不会改变原数组的形状
print(arr2.reshape(3,4))
#resize会直接改变数组的形状
print(arr2.resize(3,4))
print(arr2.shape)
二维数组:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
(4, 3)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
None
(3, 4)
import numpy as np
arr1=np.array([1,2,3,4,5,6])#列表创建一维数组
arr2=np.array(((1,2,3),(4,5,6),(7,8,9),(10,11,12)))#嵌套元组创建二维数组,两个元组要用小括号括起
print('二维数组:\n',arr2)
print(arr2.shape)
#数组堆叠
arr3=np.array([13,14,15])
arr4=np.array([[13],[14],[15],[16]])#列分开
#实现垂直方向的堆叠,即添加行
print(np.vstack([arr2,arr3]))
print('\n')
print(np.row_stack([arr2,arr3]))
print('\n')
#实现水平方向的堆叠,添加列
print(np.hstack([arr2,arr4]))
二维数组:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
(4, 3)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]
[13 14 15]]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]
[13 14 15]]
[[ 1 2 3 13]
[ 4 5 6 14]
[ 7 8 9 15]
[10 11 12 16]]
边栏推荐
- STM 32 uses cube to generate Tim to trigger ADC and transmit through DMA
- Closure problem C Lua
- Kotlin Compose 状态恢复 rememberSaveable 与 remember
- China polyimide film market demand and future investment risk outlook report 2022-2027
- Hudi compilation of data Lake architecture
- MySQL基础用法01
- My SQL(二)
- I caught a 10-year-old Alibaba test developer in the company. After chatting with him, I realized everything
- [golang] time related
- What is data mining?
猜你喜欢
Go语言学习笔记 1.1
Solution of garbled code in sparkshell deletion key of SecureCRT
Past events of Xinhua III
MySQL (III)
When vs code uses prettier to format JS, there is a space between the name of the function definition and the parentheses, and ESLIt does not allow this space
淺析一道經典題
Go language learning notes 1.1
海量日志采集工具——Flume
【golang】time相关
Everything is a vector. The service practice of iqiyi online vector recall project
随机推荐
Jasminum plug-in of Zotero document management tool
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
I use flask to write the website "II"
个人博客系统需求分析
ts中枚举类型(enum)简单使用
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
LabVIEW Arduino tcp/ip remote smart home system (project part-5)
浅析一道经典题
Play with a variety of application scenarios and share secrets with Kwai MMU
Research Report on China's surfactant market demand and future investment opportunities 2022
海量日志采集工具——Flume
MYSQL触发器要如何设置,简单教程新手一看就会
寶塔服務器搭建及數據庫遠程連接
LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)
~94 zoom
【微服务系列】Protocol buffer动态解析
【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真
When vs code uses prettier to format JS, there is a space between the name of the function definition and the parentheses, and ESLIt does not allow this space
MySQL delete in without index
Top down transformation method