程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Reshape in Python_ Reshape of numpy ()

編輯:Python


usage :

Numpy in reshape The usage of is :numpy.reshape(a, newshape, order='C')

Parameters, :

  • a: type:array_like( Pseudo array , It can be seen as an extension of an array , But does not affect the original array .)( Facts have proved that it is possible to omit , But this time reshape Method is preceded by the array to be processed )
    need reshape Of array
  • newshape: New format —— Integers or arrays of integers , Such as (2,3) Express 2 That's ok 3 Column . The new shape should be compatible with the original shape , That is, the number of rows multiplied by the number of columns equals a Number of elements in . If it's an integer , The result will be a one-dimensional array of lengths , So this integer must be equal to a Number of elements in . If this is an array of integers , Then one of the data can be -1, under these circumstances , This value python It will be automatically inferred from the second value and the remaining dimension .
  • order: The optional range is {‘C’, ‘F’, ‘A’}. Use index order to read a The elements of , And put the elements into the transformed array according to the index order . If not order Parameter settings , The default parameter is C.

(1)“C” It refers to using class C Written reading / Elements in index order , The last dimension changes the fastest , The first dimension changes the slowest . Take two-dimensional arrays as an example , The short answer is Read across , Write horizontally , Read first / Write one That's ok .

(2)“F” Refers to using FORTRAN Class index sequential read / Write elements , The last dimension changes the slowest , The first dimension changes the fastest . Read vertically , Write vertically , Read first / Write one Column . Be careful ,“C” and “F” Option does not consider the memory layout of the underlying array , Refer only to the order of the index .

(3)“A” The effect of the array generated by option is the same as that of the original array a It's about how you store your data , If the data is based on FORTRAN For storage , Its generating effect is similar to ”F“ identical ( Column priority ), Otherwise and “C” identical ( Line first ). This may sound a little vague , An example is given below .

notes :FORTRAN and C It's two languages , They store arrays differently ,FORTRSAN For the column is limited , and C For line first . stay python The default array in is generated according to C The way to store . but , Many times we need to call Fortran Some libraries for mathematical calculations , So we need to let numpy The generated array becomes as follows FORTRAN How to store , Use numpy.asfortranarray Method , This blog also gives its The official link .

  • returns: ndarray, That is, return one or more dimensional arrays

give an example :

1. First, randomly generate a 4 That's ok 3 Array of columns

2. Use reshape

There are two ways to use it , have access to np.reshape(r,(-1,1),order='F'), You can also use r1=r.reshape((-1,1),order='F'), Here you choose to use the second method . Through examples, you can observe different order Parameter effect .

It can be seen from the example that ,F Is the priority to Column Information to operate , and C Is a priority That's ok Information operation . If not r Set the format of , Then we rashape At the same time “A” In the order of order Words , Its effect and “C” identical .

3. take r The storage mode of is changed to class Fortan The way to store

Do and 2 Step similar operation


It can be seen from the figure below :np.asfortranarray(r), It's not true r The output results of , But because of the change r Storage type of , Will change “A” The output of the format , Make it relate to “F” identical .

reference :
numpy in reshape Methods,
numpy.reshape Official documents


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved