1、 什么是Numpy以及numpy数组?
- 面向数组
- 列表、字典
- 更加贴近硬件(用c语言编写的)
import numpy as np
a = np.array([1,3,5])
a # 这里返回一个numpy.array对象
array([1, 3, 5])
# 对比以下python列表和array对象的性能比较,%timeit查看运行时间
L = range(1000)
%timeit [i**2 for i in L]
363 µs ± 14.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
a = np.arange(1000)
%timeit a**2
2.34 µs ± 24.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
如何查找(功能索引)
np.lookfor('create array')# 使用lookfor查找功能索引,索引内容可以是一些相关字符
Search results for 'create array'
---------------------------------
numpy.array
Create an array.
numpy.memmap
Create a memory-map to an array stored in a *binary* file on disk.
numpy.diagflat
Create a two-dimensional array with the flattened input as a diagonal.
numpy.fromiter
Create a new 1-dimensional array from an iterable object.
numpy.partition
Return a partitioned copy of an array.
numpy.ctypeslib.as_array
Create a numpy array from a ctypes array or a ctypes POINTER.
numpy.ma.diagflat
Create a two-dimensional array with the flattened input as a diagonal.
numpy.ma.make_mask
Create a boolean mask from an array.
numpy.ctypeslib.as_ctypes
Create and return a ctypes object from a numpy array. Actually
numpy.ma.mrecords.fromarrays
Creates a mrecarray from a (flat) list of masked arrays.
numpy.ma.mvoid.__new__
Create a new masked array from scratch.
numpy.lib.format.open_memmap
Open a .npy file as a memory-mapped array.
numpy.ma.MaskedArray.__new__
Create a new masked array from scratch.
numpy.lib.arrayterator.Arrayterator
Buffered iterator for big arrays.
numpy.ma.mrecords.fromtextfile
Creates a mrecarray from data stored in the file `filename`.
numpy.asarray
Convert the input to an array.
numpy.ndarray
ndarray(shape, dtype=float, buffer=None, offset=0,
numpy.recarray
Construct an ndarray that allows field access using attributes.
numpy.chararray
chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0,
numpy.pad
Pads an array.
numpy.asanyarray
Convert the input to an ndarray, but pass ndarray subclasses through.
numpy.copy
Return an array copy of the given object.
numpy.diag
Extract a diagonal or construct a diagonal array.
numpy.load
Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files.
numpy.sort
Return a sorted copy of an array.
numpy.array_equiv
Returns True if input arrays are shape consistent and all elements equal.
numpy.dtype
Create a data type object.
numpy.choose
Construct an array from an index array and a set of arrays to choose from.
numpy.nditer
Efficient multi-dimensional iterator object to iterate over arrays.
numpy.swapaxes
Interchange two axes of an array.
numpy.full_like
Return a full array with the same shape and type as a given array.
numpy.ones_like
Return an array of ones with the same shape and type as a given array.
numpy.empty_like
Return a new array with the same shape and type as a given array.
numpy.ma.mrecords.MaskedRecords.__new__
Create a new masked array from scratch.
numpy.nan_to_num
Replace nan with zero and inf with finite numbers.
numpy.zeros_like
Return an array of zeros with the same shape and type as a given array.
numpy.asarray_chkfinite
Convert the input to an array, checking for NaNs or Infs.
numpy.diag_indices
Return the indices to access the main diagonal of an array.
numpy.chararray.tolist
a.tolist()
numpy.ma.choose
Use an index array to construct a new array from a set of choices.
numpy.savez_compressed
Save several arrays into a single file in compressed ``.npz`` format.
numpy.matlib.rand
Return a matrix of random values with given shape.
numpy.ma.empty_like
Return a new array with the same shape and type as a given array.
numpy.ma.make_mask_none
Return a boolean mask of the given shape, filled with False.
numpy.ma.mrecords.fromrecords
Creates a MaskedRecords from a list of records.
numpy.around
Evenly round to the given number of decimals.
numpy.source
Print or write to a file the source code for a NumPy object.
numpy.diagonal
Return specified diagonals.
numpy.einsum_path
Evaluates the lowest cost contraction order for an einsum expression by
numpy.histogram2d
Compute the bi-dimensional histogram of two data samples.
numpy.fft.ifft
Compute the one-dimensional inverse discrete Fourier Transform.
numpy.fft.ifftn
Compute the N-dimensional inverse discrete Fourier Transform.
numpy.busdaycalendar
A business day calendar object that efficiently stores information
导入惯例
一般使用下面的方式调用numpy库
import numpy as np
2、创建数组
手动创建数组的方法
a = np.array([0,1,2,3])
a # 创建一个array对象
array([0, 1, 2, 3])
a.ndim # 使用array.ndim查看数组的维度
1
b = np.array([[0,1,2,3],
[2,4,6,8]])
print(b.shape) # array.shape查看数组的形状,返回一个数组,第一个元素代表多少行,第二个元素代表多少列
print(a.shape) # 上文中a的定义为列向量
(2, 4)
(4,)
print(len(a)) #这是python自带的len函数
print(len(b))
4
2
- 二维向量,可以说矩阵的定义
b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 数组
b
array([[0, 1, 2],
[3, 4, 5]])
- 三维数组
c = np.array([[[1], [2]],
[[3], [4]]])
c # 三维数组
array([[[1],
[2]],
[[3],
[4]]])
c.shape
(2, 2, 1)
用函数创建数组
d = np.arange(10) # 用法类似python的range()函数
d
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a1= np.linspace(0, 1, 20) # 起点、终点、数据点,是否包括终点
a1
array([ 0. , 0.05263158, 0.10526316, 0.15789474, 0.21052632,
0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,
0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,
0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ])
a2= np.linspace(0, 1, 20,endpoint= False) # 起点、终点、数据点,是否包括终点
a2
array([ 0. , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 ,
0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85,
0.9 , 0.95])
a3 = np.ones((4,4)) # 参数是一个元组,表示几行几列
a3
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
a4 = np.ones((3,3,3))
a4 # 也可以创建一个高维的
array([[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]],
[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]],
[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]]])
a5 = np.zeros((3,5,7))
a5 #用法与ones类似
array([[[ 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., 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.]]])
a6 = np.eye(4,2) # 创建一个最简阶梯矩阵
a7 = np.eye(5) # 创建一个单位矩阵
print(a6)
print(a7)
[[ 1. 0.]
[ 0. 1.]
[ 0. 0.]
[ 0. 0.]]
[[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1.]]
a8 = np.diag(np.array([1, 2, 3, 4])) #创建一个对角矩阵,传入的参数是一个一维array对象
a8
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
numpy库中的random
b1 = np.random.rand(4) # 服从(0,1)的均匀分布
b1
array([ 0.45353327, 0.4396774 , 0.89103721, 0.05761257])
b2 = np.random.randn(4) #服从高斯分布的随机数
b2
array([ 0.31269526, 2.25998574, -2.25694432, 0.50817828])
np.random.seed(1234) # 指定种子,与标准库random的seed方法特别像,设置相同的种子,后续产生的随机数相同
b3 = np.random.randint(1,10)
print(b3)
b4 = np.random.randint(1,10)
print(b4)
np.random.seed(1234) #再次使用的种子,产生相同的随机数
b5 = np.random.randint(1,10)
print(b3)