查看版本
print(np.__version__)
np.show_config()
1.23.4
openblas64__info:
library_dirs = ['D:\\a\\numpy\\numpy\\build\\openblas64__info']
libraries = ['openblas64__info']
language = f77
define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
blas_ilp64_opt_info:
library_dirs = ['D:\\a\\numpy\\numpy\\build\\openblas64__info']
libraries = ['openblas64__info']
language = f77
define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
openblas64__lapack_info:
library_dirs = ['D:\\a\\numpy\\numpy\\build\\openblas64__lapack_info']
libraries = ['openblas64__lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
lapack_ilp64_opt_info:
library_dirs = ['D:\\a\\numpy\\numpy\\build\\openblas64__lapack_info']
libraries = ['openblas64__lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
Supported SIMD extensions in this NumPy install:
baseline = SSE,SSE2,SSE3
found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
not found = AVX512F,AVX512CD,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL
创建长度为10的空向量
data1 = np.zeros(10)
# x = np.ones(10)
print(data1)
s1 = pd.Series([1,2,3])
print("s1为:\n",s1)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
s1为:
0 1
1 2
2 3dtype: int64
找到任何数组的内存大小
data2 = np.ones((10,10))
print(data2)
print("内存大小是:","%d bytes"%(data2.size*data2.itemsize))
[[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. 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. 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. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
内存大小是: 800 bytes
从命令行中得到numpy中add函数的说明文档
print(np.info(np.add))
add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Add arguments element-wise.
Parameters
----------
x1, x2 : array_like
The arrays to be added.
If ``x1.shape != x2.shape``, they must be broadcastable to a common
shape (which becomes the shape of the output).
out : ndarray, None, or tuple of ndarray and None, optional
A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where : array_like, optional
This condition is broadcast over the input. At locations where the
condition is True, the `out` array will be set to the ufunc result.
Elsewhere, the `out` array will retain its original value.
Note that if an uninitialized `out` array is created via the default
``out=None``, locations within it where the condition is False will
remain uninitialized.
**kwargs
For other keyword-only arguments, see the
:ref:`ufunc docs <ufuncs.kwargs>`.Returns
-------
add : ndarray or scalar
The sum of `x1` and `x2`, element-wise.
This is a scalar if both `x1` and `x2` are scalars.Notes
-----
Equivalent to `x1` + `x2` in terms of array broadcasting.Examples
--------
>>> np.add(1.0, 4.0)
5.0
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.add(x1, x2)
array([[ 0., 2., 4.],
[ 3., 5., 7.],
[ 6., 8., 10.]])The ``+`` operator can be used as a shorthand for ``np.add`` on ndarrays.
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> x1 + x2
array([[ 0., 2., 4.],
[ 3., 5., 7.],
[ 6., 8., 10.]])
None
鼠标放在该函数上持续几秒也可提取文档
创建一个长度为10,并且除了第五个值为1的空向量
data3 = np.zeros(10)
data3[4]=1
print(data3)
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
创建一个值域范围从10-49的向量
data4=np.arange(10,50)
print(data4)
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
反转一个向量
data5=data4[::-1]
print(data5)
[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10]
创建一个3*3并且值从0-8的矩阵
data6=np.arange(9).reshape(3,3)
print(data6)
#生成一个5-14的3*3的矩阵。注意数值范围和矩阵m,n要对应关系,不然会报错
data7=np.arange(5,14).reshape(3,3)
print(data7)
[[0 1 2]
[3 4 5]
[6 7 8]]
[[ 5 6 7]
[ 8 9 10]
[11 12 13]]
data7=np.arange(5,50).reshape(3,3)
print(data7)
Traceback (most recent call last):
File "D:\NumPyPractice\numpy1.py", line 34, in <module>
data7=np.arange(5,50).reshape(3,3)
ValueError: cannot reshape array of size 45 into shape (3,3)
找到数组中[1,2,0,0,4,0]中非0元素的位置索引
data8=np.nonzero([1,2,0,0,4,0])
print('数组中[1,2,0,0,4,0]中非0元素的位置索引为',data8)
数组中[1,2,0,0,4,0]中非0元素的位置索引为 (array([0, 1, 4], dtype=int64),)