数据分析学习笔记(一)--numpy:数组对象的创建和类型转换

创建数组对象

函数说明
arange类似python内置的range,但返回的是一个ndarray而不是列表
array将输入的数据(列表、元祖、数组或其他序列类型)转换为ndarray。要么推断出dtype,要么显示指定dtype。默认直接复制输入的数据
asarray将输入的数据转换为ndarray,如果输入本身就是一个ndarray就不进行复制
ones、ones_like根据指定的形状和dtype创建一个全1的数组。ones_like以另一个数组为参数,并根据其形状和dtype创建一个全1的数组
zeros、zeros_like类似于ones和ones_like ,只不过产生全0的数组
empty、empty_like创建新数组,只分配内存空间但是不填充任何值
eye、identity创建一个正方的NxN单位矩阵(对角巷为1,其余为0)

数组对象的属性、方法

属性/方法说明
ndim维度
size元素个数
shape数组的形状
itemsize每个元素所占的字节
nbytes总占字节
real针对复数类型的实数部分
imag针对复数类型的虚数部分
dtype用于指定或输出数组对象元素的类型
reshape分割数组成指定形状:data.reshape(2,3,4)
astype类型转换:arr.astype(np.float64)

一些栗子

  • 创建一维数组
a = np.arange(24) # 0-23的一维数组
  • 输出一些信息
print(a)        # 输出数组
print(a.dtype)  # 输出类型
print(a.shape)  # 输出类型
输出:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
int64
(24,)
  • 创建多维数组
# 将之前创建的一维数组 a 转换为 2维度,34列数组
b = a.reshape(2,3,4)
print(b)        # 输出数组
print(b.dtype)  # 输出类型
print(b.shape)  # 输出类型
输出:
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
int64
(2, 3, 4)
  • array 创建一维数组
np.array([0,1,2,3]) # 等同于 np.arange(4)
  • array 创建多维维数组
np.array([np.arange(4),[4,5,6,7]])
  • 创建全0/1的数组
np.ones(10) # 创建全1的一维数组 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
np.ones((2,3,4))  # 创建多维数组
输出:
[[[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]]

np.zeros(10)    # 创建全0的一维数组:[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
np.zeros((4,4)) # 创建全0的多维数组
输出:
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
  • 数组的属性
a = np.arange(24).reshape(2,3,4)
a.ndim   # 维数    3
a.size   # 数量    24
a.itemsize   # 每个元素所占的字节     8
a.nbytes     # 总占字节              192 (24*8)
a.dtype      # 元素的类型             int64
a.flat       # <numpy.flatiter object at 0x10214b600>
[item for item in a.flat]    # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
a.flat[2]    # 2
# 创建复数数组
b = np.array(np.arange(4),dtype=np.complex) 
b.real       # [0. 1. 2. 3.]
b.imag       # [0. 0. 0. 0.]

数据类型

类型类型代码说明
int8、uint8i1、u1有符号和无符号的8位(1个字节)整型
int16、uint16i2、u2有符号和无符号的8位(2个字节)整型
int32、uint32i4、u4有符号和无符号的8位(4个字节)整型
int64、uint8i8、u8有符号和无符号的8位(8个字节)整型
float16f2半精度浮点数
float32f4或f标准的单精度浮点数.与C的float兼容
float64f8或d标准的单精度浮点数.与C的double和Python的float对象兼容
float128f16或g扩展精度浮点数
complex64、complex128、complex258c8、c16、c32分别用两个32位、64位、128位浮点数表示的复数
bool?存储Ture和False值的布尔类型
objectOPython对象类型
string_S固定长度的字符串类型(每个字符1个字节),例如,要创建一个长度为10的字符串,应使用S10
unicode_U固定长度的unicode类型(字节数由平台决定)。和字符串的定义方式一样,如U10

一些栗子

  • 转换类型1
np.float64(10)  # 转换为浮点型    10.
np.int(13.2)    # 转换为整型     13
np.bool(12.3)   # 转换为布尔型    True
np.complex(3)   # 转换为复数类型   (3+0j)
np.int((2+3j))  # 不能将复数转换为整型或者浮点型
  • 转换类型2
# 创建一个例子数组
a = np.arange(1,5)
print(a.dtype)          # 该数组类型为:int64
# 将其转换为浮点型
float_a = a.astype(np.float32)  
print(float_a.dtype)    # float32
# 将其转换为整型
int_a = float_a.astype(np.int8)
print(int_a.dtype)      # int8
# 创建字符数组
numeric_strings = np.array(['1.25', '-9.6', '42'], dtype='S')
# 将其转换为浮点型
numeric_strings = numeric_strings.astype(np.float)
print(numeric_strings.dtype)    # float64
  • 指定类型
a = np.arange(4,dtype=np.float) # 指定其类型为float
print(a.dtype)  # 输出:float64
  • dtype类的属性
# dtype类的属性
t = np.dtype('d')
print(t.char)   # d,即通过指定'd'即可获取类型float64
print(t.type)   # <class 'numpy.float64'>
print(t.str)    # <f8
  • 数据类型占字节数
a = np.array([[1,2],[3,4]])
print(a.dtype.itemsize) # 8 
  • 创建自定义数据类型
# 自定义一个新类型
myType = np.dtype([('name', np.string_, 40), ('age', np.int32), ('height', np.float)])
print(myType)   # [('name', 'S40'), ('age', '<i4'), ('height', '<f8')]
# 使用新类型创建一个数组
array = np.array([('lolita0164', 25, 170.0), ('luo', 19, 165.3)], dtype=myType)
# 取出第0个
print(array[0])     # (b'lolita0164', 25, 170.)
# 取出第0个,'name'属性的值
print(array[0]['name']) # b'lolita0164'
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值