Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

python - What is the difference between resize and reshape when using arrays in NumPy?

I have just started using NumPy. What is the difference between resize and reshape for arrays?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Reshape doesn't change the data as mentioned here. Resize changes the data as can be seen here.

Here are some examples:

>>> numpy.random.rand(2,3)
array([[ 0.6832785 ,  0.23452056,  0.25131171],
       [ 0.81549186,  0.64789272,  0.48778127]])
>>> ar = numpy.random.rand(2,3)
>>> ar.reshape(1,6)
array([[ 0.43968751,  0.95057451,  0.54744355,  0.33887095,  0.95809916,
         0.88722904]])
>>> ar
array([[ 0.43968751,  0.95057451,  0.54744355],
       [ 0.33887095,  0.95809916,  0.88722904]])

After reshape the array didn't change, but only output a temporary array reshape.

>>> ar.resize(1,6)
>>> ar
array([[ 0.43968751,  0.95057451,  0.54744355,  0.33887095,  0.95809916,
         0.88722904]])

After resize the array changed its shape.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...