Numpy概述
numpy(Numerical Python)提供了python对多维数组对象的支持:ndarray,具有矢量运算能力,快速、节省空间。numpy支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
创建数组
| 类型 |
描述 |
| ndarray |
N维数组对象(矩阵),所有元素必须是相同类型 |
| ndarray属性 |
ndim属性,表示维度个数;shape属性,表示各维度大小;dtype属性,表示数据类型 |
#encoding=utf-8
import numpy as np
def main():
lst = [[1,2,3],[4,5,6]]
print(type(lst)) #列表数据类型
np_lst = np.array(lst)
print(np_lst) #打印数组
print(type(np_lst)) #经过numpy处理后的数组类型(矩阵)
print(np_lst.shape) #打印数组各个维度的长度
print(np_lst.ndim) #打印数组的维度
print(np_lst.dtype) #打印数组元素的类型
print(np_lst.itemsize) #打印每个字节长度
print(np_lst.size) #打印数组长度
if __name__ == "__main__":
main()
ndarray对象的主要属性有:
| 方法 |
描述 |
| ndarray.ndim |
纬度或者轴的数量 |
| ndarray.shape |
数组的每个纬度的尺寸 |
| ndarray.size |
数组元素的总个数 |
| ndarray.dtype |
数组元素的类型 |
| ndarray.itemsize |
数组元素二进制的大小 |
| ndarray.data |
数组元素容器(不常用 |
类型转换
可以设置类型
| 类型 |
描述 |
| bool |
Boolean (True or False) stored as a byte |
| int |
Default integer type (same as C long; normally either int64 or int32) |
| intc |
Identical to C int (normally int32 or int64) |
| intp |
Integer used for indexing (same as C ssize_t; normally either int32 or int64) |
| int8 |
Byte (-128 to 127) |
| int16 |
Integer (-32768 to 32767) |
| int32 |
Integer (-2147483648 to 2147483647) |
| int64 |
Integer (-9223372036854775808 to 9223372036854775807) |
| uint8 |
Unsigned integer (0 to 255) |
| uint16 |
Unsigned integer (0 to 65535) |
| uint32 |
Unsigned integer (0 to 4294967295) |
| uint64 |
Unsigned integer (0 to 18446744073709551615) |
| float |
Shorthand for float64 |
| float16 |
Half precision float: sign bit, 5 bits exponent, 10 bits mantissa |
| float32 |
Single precision float: sign bit, 8 bits exponent, 23 bits mantissa |
| float64 |
Double precision float: sign bit, 11 bits exponent, 52 bits mantissa |
| complex |
Shorthand for complex128 |
| complex64 |
Complex number, represented by two 32-bit floats (real and imaginary components) |
| complex128 |
Complex number, represented by two 64-bit floats (real and imaginary components) |
np.array(lst, dtype=complex)
常用函数
reshape
生成从0-14的15个数字,使用reshape(3,5)将其构造成一个三行五列的array
arr = np.arange(15).reshape(3, 5) #一定要对等 15 = 3*5 3行5列
print(arr)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
arr = np.arange(15)
print(arr)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
zeros
生成指定结构的默认为0的array
arr = np.zeros([3,4,6])
#arr = np.zeros((3,4,6))
print(arr)
[[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]]
range
指定范围和数值间的间隔生成array,注意范围包左不包右,创建序列数组
arr = np.arange(0,10,2) #范围0~10,每隔一位自增2
print(arr)
arr = np.arange(0,10,5)
print(arr)
ones
生成一个三维的array,通过dtype指定类型
arr = np.ones((2,3,4), dtype=np.int32)
print(arr)
[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]
random
生成指定结构的随机数,可以用于生成随机权重
arr = np.random.random((2, 2, 3))
#arr = np.random.rand(2, 2, 3)
print(arr)
[[[0.90288171 0.04294187 0.79566515]
[0.10836542 0.51022785 0.99601061]]
[[0.08225114 0.73245064 0.57750816]
[0.45772924 0.15048225 0.22736567]]]
随机生成0~2,长度为10的一维数组
arr = np.random.randint(0, 2, 10)
参考文档
-NumPy v1.11中文官方手册
Numpy概述
numpy(Numerical Python)提供了python对多维数组对象的支持:ndarray,具有矢量运算能力,快速、节省空间。numpy支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。创建数组
ndarray对象的主要属性有:类型转换
可以设置类型
常用函数
reshape
生成从0-14的15个数字,使用
reshape(3,5)将其构造成一个三行五列的arrayzeros
生成指定结构的默认为0的array
range
指定范围和数值间的间隔生成array,注意范围包左不包右,创建序列数组
ones
生成一个三维的array,通过dtype指定类型
random
生成指定结构的随机数,可以用于生成随机权重
随机生成0~2,长度为10的一维数组
参考文档
-NumPy v1.11中文官方手册