数据分析学习笔记(一)--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、uint8 i1、u1 有符号和无符号的8位(1个字节)整型
int16、uint16 i2、u2 有符号和无符号的8位(2个字节)整型
int32、uint32 i4、u4 有符号和无符号的8位(4个字节)整型
int64、uint8 i8、u8 有符号和无符号的8位(8个字节)整型
float16 f2 半精度浮点数
float32 f4或f 标准的单精度浮点数.与C的float兼容
float64 f8或d 标准的单精度浮点数.与C的double和Python的float对象兼容
float128 f16或g 扩展精度浮点数
complex64、complex128、complex258 c8、c16、c32 分别用两个32位、64位、128位浮点数表示的复数
bool ? 存储Ture和False值的布尔类型
object O Python对象类型
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'

猜你喜欢

转载自blog.csdn.net/lolita0164/article/details/79952867
今日推荐