如果两个数组是可广播的,nditer 组合对象能够同时迭代它们,假设数组 a 具有维度 3X4,并且存在维度为 1X4 的另一个数组 b,则使用以下类型的迭代器(数组 b 被广播到 a 的大小):
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)print(a)
b = np.array([1,2,3,4], dtype=int)print(b)for x, y in np.nditer([a, b]):print(x, y)
import numpy as np
a = np.arange(4).reshape(1,4)print(a)print(np.broadcast_to(a,(4,4)))
运行结果:
[[0123]][[0123][0123][0123][0123]]
② numpy.expand_dims
函数通过在指定位置插入新的轴来扩展数组形状,该函数需要两个参数:
numpy.expand_dims(arr, axis)
其中:
arr:输入数组;
axis:新轴插入的位置。
使用如下:
import numpy as np
x = np.array(([1,2],[3,4]))print(x)
y = np.expand_dims(x, axis=0)print(y)print(x.shape, y.shape)
y = np.expand_dims(x, axis=1)print(y)print(x.ndim, y.ndim)print(x.shape, y.shape)
import numpy as np
a = np.array([[1,2],[3,4]])print(a)
b = np.array([[5,6],[7,8]])print(b)print('竖直堆叠:')
c = np.vstack((a, b))print(c)
运行结果:
[[12][34]][[56][78]]
竖直堆叠:
[[12][34][56][78]]
④ numpy.concatenate
函数用于沿指定轴连接相同形状的两个或多个数组,该函数接受以下参数:
numpy.concatenate((a1, a2, …), axis)
其中:
a1, a2, …:相同类型的数组序列;
axis:沿着它连接数组的轴,默认为 0。
import numpy as np
a = np.array([[1,2],[3,4]])print(a)
b = np.array([[5,6],[7,8]])print(b)print(np.concatenate((a,b)))print(np.concatenate((a,b),axis =1))
import numpy as np
a = np.array([[1,2,3],[4,5,6]])print(a)print(a.shape)
b = np.resize(a,(3,2))print(b)print(b.shape)print('修改第二个数组的大小:')
b = np.resize(a,(3,3))print(b)print('修改第三个数组的大小:')
b = np.resize(a,(2,2))print(b)
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])print(a)print(np.insert(a,3,[11,12]))print(np.insert(a,1,[11],axis =0))print(np.insert(a,1,[11],axis =1))
import numpy as np
print(np.char.add(['hello'],[' xyz']))print(np.char.add(['hello','hi'],[' abc',' xyz']))print(np.char.multiply('Hello ',3))print(np.char.center('hello',20,fillchar ='*'))print(np.char.capitalize('hello world'))print(np.char.title('hello how are you?'))print(np.char.lower(['HELLO','WORLD']))print(np.char.lower('HELLO'))print(np.char.upper('hello'))print(np.char.upper(['hello','world']))print(np.char.split ('hello how are you?'))print(np.char.split ('YiibaiPoint,Hyderabad,Telangana', sep =','))print(np.char.splitlines('hello\nhow are you?'))print(np.char.splitlines('hello\rhow are you?'))print(np.char.strip('ashok arora','a'))print(np.char.strip(['arora','admin','java'],'a'))print(np.char.join(':','dmy'))print(np.char.join([':','-'],['dmy','ymd']))print(np.char.replace ('He is a good boy','is','was'))
a = np.char.encode('hello','cp500')print(a)print(np.char.decode(a,'cp500'))
运行结果:
['hello xyz']['hello abc''hi xyz']
Hello Hello Hello
*******hello********
Hello world
Hello How Are You?
['hello''world']
hello
HELLO
['HELLO''WORLD']['hello','how','are','you?']['YiibaiPoint','Hyderabad','Telangana']['hello','how are you?']['hello','how are you?']
shok aror
['ror''dmin''jav']
d:m:y
['d:m:y''y-m-d']
He was a good boy
b'\x88\x85\x93\x93\x96'
hello
import numpy as np
a = np.array([0.25,2,1,0.2,100])print(np.reciprocal(a))print(np.power(a,2))
a = np.array([10,20,30])
b = np.array([3,5,7])print(np.mod(a,b))
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])print(np.amin(a,1))print(np.amax(a,1))print(np.ptp(a))print(np.percentile(a,50))print(np.median(a))print(np.mean(a))print(np.average(a))print(np.std([1,2,3,4]))#返回数组标准差print(np.var([1,2,3,4]))#返回数组方差
import numpy as np
a = np.array([[3,7,3,1],[9,7,8,7]])print(np.sort(a))print(np.argsort(a))print(np.argmax(a))print(np.argmin(a))print(np.nonzero(a))print(np.where(a >3))
nm =('raju','anil','ravi','amar')
dv =('f.y.','s.y.','s.y.','f.y.')print(np.lexsort((dv, nm)))
import numpy as np
a = np.array([1,2,3,4,5])
np.save('outfile',a)
b = np.load('outfile.npy')print(b)
a = np.array([1,2,3,4,5])
np.savetxt('out.txt',a)
b = np.loadtxt('out.txt')print(b)