numpy的学习

1、基本属性

NumPy的数组类被称为ndarray它也被别名数组所知 

请注意,numpy.array与标准Python库类array.array不同,后者仅处理一维数组并提供较少的功能。ndarray对象的更重要的属性是:

.ndim:阵列的轴数(维度)。在Python世界中,维度的数量被称为等级

.shape:数组的尺寸。这是一个整数的元组,表示每个维度中数组的大小。对于具有nm的矩阵形状将是(n,m)因此形状元组的长度 是等级或维数 ndim

.size:数组元素的总数。这等于形状元素的乘积

.dtype:一个描述数组中元素类型的对象。可以使用标准的Python类型创建或指定dtype。另外NumPy提供它自己的类型。numpy.int32,numpy.int16和numpy.float64就是一些例子。

ndarray.itemsize:数组中每个元素的字节大小。例如,类型的元件的阵列float64具有itemsize 8(= 64/8),而类型的一个complex32具有itemsize 4(= 32/8)。它相当于ndarray.dtype.itemsize

ndarray.data:该缓冲区包含数组的实际元素。通常,我们不需要使用此属性,因为我们将使用索引设施访问数组中的元素。

2、文件的读取

import numpy as np
world_alcohol = np.genfromtxt('world_alcohol.txt', delimiter=',', dtype=str)
print(type(world_alcohol))
# <class 'numpy.ndarray'>  nd 是N-dimensional的缩写 表示N 维数组
print(world_alcohol)

3、数组创建

import numpy as np
a = np.array([2,3,4])
print(a)
print(a.dtype)
b = np.array([1.2, 3.5, 5.1])
print(b.dtype)
[2 3 4]
int32

float64

4、暂时写到这里,看https://www.yiibai.com/numpy/numpy_array_from_existing_data.html

扫描二维码关注公众号,回复: 144612 查看本文章

网站继续学习更新

猜你喜欢

转载自blog.csdn.net/qq_40276310/article/details/79600729