1.numpy 向量运算 VS python 运算
def pySum ( ) :
a = list ( range ( 10000 ) )
b = list ( range ( 10000 ) )
c= [ ]
for i in range ( len ( a) ) :
c. append( a[ i] ** 2 + b[ i] ** 2 )
return c
% timeit pySum( )
8.43 ms ± 108 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
import numpy as np
def npSum ( ) :
a= np. arange( 10000 )
b= np. arange( 10000 )
c= a** 2 + b** 2
return c
% timeit npSum( )
38.9 µs ± 997 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
2.数组创建
Method1:基于list 或tuple
import numpy as np
arr1 = np. array( [ 1 , 2 , 3 , 4 ] )
print ( arr1)
arr_tuple = np. array( ( 1 , 2 , 3 , 4 ) )
print ( arr_tuple)
arr2 = np. array( [ [ 1 , 2 , 4 ] , [ 2 , 3 , 5 ] ] )
arr2
[1 2 3 4]
[1 2 3 4]
array([[1, 2, 4],
[2, 3, 5]])
Method2:基于np.arange
import numpy as np
arr1 = np. arange( 5 )
print ( arr1)
arr2 = np. array( [ np. arange( 3 ) , np. arange( 3 ) ] )
arr2
[0 1 2 3 4]
array([[0, 1, 2],
[0, 1, 2]])
Method3: 基于arange以及reshape创建多维数组
import numpy as np
arr = np. arange( 24 ) . reshape( 2 , 3 , 4 )
arr
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
3. Numpy的数值类型
import numpy as np
print ( np. int8( 12.334 ) )
print ( np. float64( 12 ) )
print ( np. float ( True ) )
print ( bool ( 1 ) )
a= np. arange( 5 , dtype= float )
print ( a)
12
12.0
1.0
True
[0. 1. 2. 3. 4.]
4.ndarray 数组的属性
dtype 属性, ndarray 数组的数据类型
import numpy as np
print ( np. arange( 4 , dtype= float ) )
print ( np. arange( 4 , dtype= 'D' ) )
print ( np. arange( 4 , dtype= 'int8' ) )
[0. 1. 2. 3.]
[0.+0.j 1.+0.j 2.+0.j 3.+0.j]
[0 1 2 3]
import numpy as np
a = np. array( [ [ 1 , 2 , 3 ] , [ 7 , 8 , 9 ] ] )
a. ndim
2
shape属性,数组对象的尺度,对于矩阵,即n行m列,shape是一个元组(tuple) size属性用来保存元素的数量,相当于shape中nXm的值 itemsize属性返回数组中各个元素所占用的字节数大小 nbytes属性,如果想知道整个数组所需的字节数量,可以使用nbytes属性。其值等于数组的size属性值乘以itemsize属性值 T 属性,数组转置
import numpy as np
a = np. array( [ [ 1 , 2 , 3 ] , [ 7 , 8 , 9 ] ] )
print ( '数组shape' , a. shape)
print ( '数组size' , a. size)
print ( '数组itemSize' , a. itemsize)
print ( '数组nbytes' , a. nbytes)
b= np. arange( 24 ) . reshape( 4 , 6 )
print ( '源数组b为:\n' , b)
print ( '转置后的数组b 为:\n' , b. T)
数组shape (2, 3)
数组size 6
数组itemSize 4
数组nbytes 24
源数组b为:
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
转置后的数组b 为:
[[ 0 6 12 18]
[ 1 7 13 19]
[ 2 8 14 20]
[ 3 9 15 21]
[ 4 10 16 22]
[ 5 11 17 23]]
复数的实部和虚部属性,real和imag属性 flat属性,返回一个numpy.flatiter对象,即可迭代的对象
import numpy as np
d = np. array( [ 1.2 + 2j , 2 + 3j ] )
print ( d)
print ( "d的实部为\n" , d. real)
print ( "d的虚部为\n" , d. imag)
e = np. arange( 6 ) . reshape( 2 , 3 )
print ( e)
f= e. flat
print ( "遍历数组\n" )
for item in f:
print ( item)
[1.2+2.j 2. +3.j]
d的实部为
[1.2 2. ]
d的虚部为
[2. 3.]
[[0 1 2]
[3 4 5]]
遍历数组
0
1
2
3
4
5
5. ndarray 数组的切片和索引
import numpy as np
a = np. arange( 7 )
print ( a)
b= np. arange( 12 ) . reshape( 4 , 3 )
print ( b)
print ( "二维索引的使用\n" )
print ( b[ 2 ] )
print ( b[ 1 ] [ 2 ] )
print ( "二维切片的使用:arr[start:stop:step,start:stop:step]\n" )
print ( b[ : , : ] )
print ( b[ : , 1 ] )
print ( b[ : , 0 : 2 ] )
print ( b[ : : 2 , : ] )
print ( b[ 0 : 2 , : ] )
print ( "坐标获取 [行,列]\n" )
print ( b[ 1 ] [ 2 ] )
print ( b[ 1 , 2 ] , b[ 2 , 0 ] )
print ( "二维索引为负数:\n" )
print ( b[ - 1 ] )
print ( b[ : : - 1 ] )
[0 1 2 3 4 5 6]
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
二维索引的使用
[6 7 8]
5
二维切片的使用:arr[start:stop:step,start:stop:step]
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
[ 1 4 7 10]
[[ 0 1]
[ 3 4]
[ 6 7]
[ 9 10]]
[[0 1 2]
[6 7 8]]
[[0 1 2]
[3 4 5]]
坐标获取 [行,列]
5
5 6
二维索引为负数:
[ 9 10 11]
[[ 9 10 11]
[ 6 7 8]
[ 3 4 5]
[ 0 1 2]]