import numpy as np
a = np. zeros( ( 2 , 10 ) )
a[ np. arange( 2 ) , 4 ] = 1
print ( "逐个赋值后的值:" , a)
b = np. array( [ 0.34 , 1.3 , 0.75 , - 0.23 , - 1.1 ] )
b = np. ceil( b)
print ( "向上取整的值:" , b)
变化后的值: [[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]
向上取整的值: [ 1. 2. 1. -0. -1.]
print ( 'np.repeat()的用法' )
t = np. array( [ [ 1 , 2 , 4 ] , [ - 0.23 , 0.45 , - 1.2 ] , [ 3.2 , 0.44 , - 0.65 ] ] )
t0 = np. repeat( t, 3 , axis= 0 )
t0_reshape = t0. reshape( t. shape[ 0 ] , - 1 )
t1 = np. repeat( t, 2 , axis= 1 )
t1_reshape = t1. reshape( - 1 , t. shape[ 1 ] )
print ( '输入值:' , t, t. shape)
print ( "按第1维度复制矩阵结果:" , t0, t0. shape)
print ( "按第1维度复制并reshape矩阵结果:" , t0_reshape, t0_reshape. shape)
print ( "按第2维度复制矩阵结果:" , t1, t1. shape)
print ( "按第2维度复制并reshape矩阵结果:" , t1_reshape, t1_reshape. shape)
np.repeat()的用法
输入值: [[ 1. 2. 4. ]
[-0.23 0.45 -1.2 ]
[ 3.2 0.44 -0.65]] (3, 3)
按第1维度复制矩阵结果: [[ 1. 2. 4. ]
[ 1. 2. 4. ]
[ 1. 2. 4. ]
[-0.23 0.45 -1.2 ]
[-0.23 0.45 -1.2 ]
[-0.23 0.45 -1.2 ]
[ 3.2 0.44 -0.65]
[ 3.2 0.44 -0.65]
[ 3.2 0.44 -0.65]] (9, 3)
按第1维度复制并reshape矩阵结果: [[ 1. 2. 4. 1. 2. 4. 1. 2. 4. ]
[-0.23 0.45 -1.2 -0.23 0.45 -1.2 -0.23 0.45 -1.2 ]
[ 3.2 0.44 -0.65 3.2 0.44 -0.65 3.2 0.44 -0.65]] (3, 9)
按第2维度复制矩阵结果: [[ 1. 1. 2. 2. 4. 4. ]
[-0.23 -0.23 0.45 0.45 -1.2 -1.2 ]
[ 3.2 3.2 0.44 0.44 -0.65 -0.65]] (3, 6)
按第2维度复制并reshape矩阵结果: [[ 1. 1. 2. ]
[ 2. 4. 4. ]
[-0.23 -0.23 0.45]
[ 0.45 -1.2 -1.2 ]
[ 3.2 3.2 0.44]
[ 0.44 -0.65 -0.65]] (6, 3)