numpy库简介
简介
开源的Python库,它提供了高性能的多维数值(numpy.ndarray)计算能力; 由“Numerical Python”缩写而来,并且它是Pandas库的基础; NumPy提供了许多有用的功能,例如数组操作、矩阵运算、傅里叶变换等; 支持向量化编程,这使得它比传统的循环和条件语句更加高效; NumPy完全基于C语言实现,并且已经经过了充分的优化,因此它的运行速度非常快。
特点
numpy中,用轴(axis)
来描述数组的维度。 axis=0表列,意味着你将沿着列方向(跨行)对数据进行操作,即进行列数据操作; 同理,axis=1表行,即进行行数据操作。
numpy操作
import numpy as np
数组创建
list = [ 1 , 2 , 3 , 4 , 5 ]
array = np. array( list )
print ( array)
print ( type ( array) ) > [ 1 2 3 4 5 ]
> < class 'numpy.ndarray' >
list = [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ]
array = np. array( list )
print ( array) > [ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]
- np. arange( 开始值, 结束值, 步长) array = np. arange( 1 , 10 , 2 )
print ( array)
> [ 1 3 5 7 9 ]
- np. zeros( shape, dtype= float , order= 'C' ) :创建一个元素全为0 的数组
- np. ones( shape, dtype= None , order= 'C' ) :创建一个元素全为1 的数组
- np. empty( shape, dtype= float , order= 'C' ) :创建一个未初始化的数组, 元素随机
- np. full( shape, fill_value, dtype= None , order= 'C' ) :创建一个具有给定形状和填充值的数组- shape: 数组的性转- dtype: 数组元素数据类型- order: 内存布局顺序,C- 按行,F- 按列array = np. empty( ( 2 , 4 ) , dtype= np. int8)
print ( array)
> [ [ 96 - 39 - 22 70 ] [ 24 86 0 0 ] ]
import numpy as np
rand_array = np. random. rand( 2 , 3 )
print ( "均匀分布的随机数组:\n" , rand_array)
randn_array = np. random. randn( 2 , 3 )
print ( "标准正态分布的随机数组:\n" , randn_array)
randint_array = np. random. randint( 1 , 10 , size= ( 2 , 3 ) )
print ( "指定范围的随机整数数组:\n" , randint_array)
random_array = np. random. random( ( 2 , 3 ) )
print ( "均匀分布的随机数组:\n" , random_array) > 均匀分布的随机数组: [ [ 0.49018606 0.05014734 0.38739906 ] [ 0.09357898 0.98583039 0.6992634 ] ]
> 标准正态分布的随机数组: [ [ 1.44017508 0.55562128 - 0.11157242 ] [ 0.80112095 1.58158805 0.81131876 ] ]
> 指定范围的随机整数数组: [ [ 7 6 9 ] [ 5 2 6 ] ]
> 均匀分布的随机数组: [ [ 0.35562269 0.29418661 0.49925419 ] [ 0.76548519 0.70753405 0.02305559 ] ]
数组属性
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( "第一个元素:" , array[ 0 , 0 ] )
print ( "最后一个元素:" , array[ - 1 , - 1 ] )
print ( "第一行:" , array[ 0 , : ] )
print ( "第二列:" , array[ : , 1 ] ) > 第一个元素: 1
> 最后一个元素: 6
> 第一行: [ 1 2 3 ]
> 第二列: [ 2 5 ]
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( array. shape) > ( 2 , 3 )
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( array. ndim)
print ( array. size) > 2
> 6
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( array. dtype) > int64
数组变更
array = np. empty( ( 2 , 3 ) , dtype= np. int8)
array. fill( 8 )
print ( "使用np.full()方法填充的数组:" , array)
> 使用np. full( ) 方法填充的数组: [ [ 8 8 8 ] [ 8 8 8 ] ]
array = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
inserted_array = np. insert( array, 2 , [ 6 , 7 ] )
print ( "插入元素后的数组:" , inserted_array)
> 插入元素后的数组: [ 1 2 6 7 3 4 5 ]
appended_array = np. append( array, [ 6 , 7 ] )
print ( "追加元素后的数组:" , appended_array)
> 追加元素后的数组: [ 1 2 3 4 5 6 7 ]
deleted_array = np. delete( array, 2 )
print ( "删除元素后的数组:" , deleted_array)
> 删除元素后的数组: [ 1 2 4 5 ]
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
sub_array = array[ 0 : 2 , 1 : 3 ]
print ( sub_array) > [ [ 2 3 ] [ 5 6 ] ]
my_array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
transposed_array = np. transpose( my_array)
print ( transposed_array) > [ [ 1 4 ] [ 2 5 ] [ 3 6 ] ]
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
array = np. reshape( array, ( 3 , 2 ) )
print ( array) > [ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
array = array. astype( float )
print ( array. dtype) > float64
array1 = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
array2 = np. array( [ [ 5 , 6 ] , [ 7 , 8 ] ] )
hstacked_array = np. hstack( ( array1, array2) )
print ( "水平连接的数组:\n" , hstacked_array)
vstacked_array = np. vstack( ( array1, array2) )
print ( "垂直连接的数组:\n" , vstacked_array) > 水平连接的数组: [ [ 1 2 5 6 ] [ 3 4 7 8 ] ]
> 垂直连接的数组: [ [ 1 2 ] [ 3 4 ] [ 5 6 ] [ 7 8 ] ]
- numpy. split( array, indices_or_sections, axis= 0 ) : 沿指定轴拆分数组
- numpy. hsplit( array, indices_or_sections) : 水平拆分数组(列)
- numpy. vsplit( array, indices_or_sections) : 垂直拆分数组(行)array = np. array( [ [ 1 , 2 , 3 , 11 , 12 , 13 ] , [ 4 , 5 , 6 , 14 , 15 , 16 ] , [ 7 , 8 , 9 , 17 , 18 , 19 ] ] )
split_array1 = np. split( array, 3 , axis= 0 )
for arr in split_array1: print ( arr) > [ [ 1 2 3 11 12 13 ] ] [ [ 4 5 6 14 15 16 ] ] [ [ 7 8 9 17 18 19 ] ]
split_array2 = np. hsplit( array, 3 )
for arr in split_array2: print ( arr) > [ [ 1 2 ] [ 4 5 ] [ 7 8 ] ] [ [ 3 11 ] [ 6 14 ] [ 9 17 ] ] [ [ 12 13 ] [ 15 16 ] [ 18 19 ] ]
split_array3 = np. vsplit( array, 3 )
for arr in split_array3: print ( arr) > [ [ 1 2 3 11 12 13 ] ] [ [ 4 5 6 14 15 16 ] ] [ [ 7 8 9 17 18 19 ] ]
array = np. array( [ 3 , 1 , 2 , 5 , 4 ] )
sorted_array = np. sort( array)
print ( "排序后的数组:" , sorted_array) sorted_indices = np. argsort( array)
print ( "排序后的原索引:" , sorted_indices) > 排序后的数组: [ 1 2 3 4 5 ]
> 排序后的索引: [ 1 2 0 4 3 ]
array = np.array( [ 1 , 2 , 3 ] )
copied_array = array.copy( )
print( "复制的数组:" , copied_array)
> 复制的数组: [ 1 2 3 ]
array = np. array( [ 1 , 2 , 3 ] )
repeated_array = np. repeat( array, 2 )
print ( "重复后的数组:" , repeated_array)
tiled_array = np. tile( array, 2 )
print ( "重复后的数组:" , tiled_array) > 重复后的数组: [ 1 1 2 2 3 3 ]
> 重复后的数组: [ 1 2 3 1 2 3 ]
array = np. array( [ 1 , 2 , 2 , 3 , 3 , 4 , 5 , 5 ] )
unique_array = np. unique( array)
print ( "去重后的数组:" , unique_array)
> 去重后的数组: [ 1 2 3 4 5 ]
数据计算
array = np. array( [ 3 , 1 , 2 , 5 , 4 ] )
max_value = np. max ( array)
min_value = np. min ( array)
max_index = np. argmax( array)
min_index = np. argmin( array)
print ( "数组的最大值:" , max_value, "索引:" , max_index)
print ( "数组的最小值:" , min_value, "索引:" , min_index)
> 数组的最大值: 5 索引: 3
> 数组的最小值: 1 索引: 1
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
row_sum = np. sum ( array, axis= 1 )
print ( "数组的行和:" , row_sum)
col_sum = np. sum ( array, axis= 0 )
print ( "数组的列和:" , col_sum)
cumsum_array = np. cumsum( array)
print ( "数组元素的累积和:" , cumsum_array)
cumsum_array = np. cumsum( array, axis= 1 )
print ( "数组行元素的累积和:" , cumsum_array) > 数组的行和: [ 6 15 ]
> 数组的列和: [ 5 7 9 ]
> 数组元素的累积和: [ 1 3 6 10 15 21 ]
> 数组行元素的累积和: [ [ 1 3 6 ] [ 4 9 15 ] ]
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
prod_result = np. prod( array)
print ( "数组元素求积:" , prod_result)
> 数组元素求积: 720
my_array = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
mod_result = np. mod( my_array, 2 )
print ( "数组元素取余操作:" , mod_result)
> 数组元素取余操作: [ 1 0 1 0 1 ]
array = np. array( [ 3 , 1 , 2 , 5 , 4 ] ) mean_value = np. mean( array)
print ( "数组元素的平均值:" , mean_value)
> 数组元素的平均值: 3.0
np. std( )
np. var( )
np. abs ( )
np. round ( )
np. floor( )
np. ceil( )
np. trunc( )
np. power( )
np. log( )
array = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
def custom_function ( x) : return x * 2
new_array = np. apply_along_axis( custom_function, axis= 1 , arr= array)
print ( "应用自定义函数后的数组:\n" , new_array)
> 应用自定义函数后的数组: [ [ 2 4 6 ] [ 8 10 12 ] ]
array1 = np. array( [ 1 , 2 , 3 ] )
array2 = np. array( [ 4 , 5 , 6 ] )
add_result = np. add( array1, array2)
print ( "数组元素加法:" , add_result)
subtract_result = np. subtract( array1, array2)
print ( "数组元素减法:" , subtract_result)
multiply_result = np. multiply( array1, array2)
print ( "数组元素乘法:" , multiply_result)
divide_result = np. divide( array1, array2)
print ( "数组元素除法:" , divide_result)
> 数组元素加法: [ 3 4 6 ]
> 数组元素减法: [ - 1 0 0 ]
> 数组元素乘法: [ 2 4 9 ]
> 数组元素除法: [ 0.5 1. 1. ]
array1 = np. array( [ 1 , 2 , 3 ] )
array2 = np. array( [ 2 , 2 , 3 ] )
equal_result = np. equal( array1, array2)
print ( "数组元素相等比较:" , equal_result)
> 数组元素相等比较: [ False True True ] np. not_equal( array1, array2)
np. greater( array1, array2)
np. less( array1, array2)
np. greater_equal( array1, array2)
np. less_equal( array1, array2)