Numerical Value Type operational Numpy -03

What is NumPy?

  NumPy Python package is a basic scientific computing. It is a Python library that provides a multidimensional array object, various derived object (e.g. a mask array and matrix) and various routines for operating the array for quick, including mathematical, logical, shape processing, sorting, selection, the I / O, discrete Fourier transform, the basic linear algebra, basic statistical calculation, simulation, and so random.

The core NumPy package is ndarray object. This encapsulating the data type of a uniform n -dimensional array, in order to improve performance, many of the operations are performed in the compiled code. There are several important differences between NumPy arrays and standard Python sequence:

  • NumPy array of fixed size at the time of creation, which is a Python list (can grow dynamically) different. Change ndarray size will create a new array and then delete the original array.
  • NumPy all elements in the array must have the same data type, the size will be the same in the memory. Exception: An object may have (the Python, including NumPy) array object, thereby allowing the array elements having different sizes.
  • NumPy array of large amounts of data will help Advanced Math and other types of operations. Typically, compared to the use of the built-in Python sequence, such operations can be performed more efficiently and less code.
  • More and more scientific and mathematical software packages based on the use Python NumPy arrays. While these input sequences are usually supported Python, but they will be converted to such input NumPy array prior to processing, and typically outputs NumPy array. In other words, in order to effectively use many (perhaps even most) based on today's science / math Python-based software, just know how to use the built-in sequence types Python is not enough - people need to know how to use NumPy arrays. (A summary is to do basic scientific computing library in Python, focusing on numerical calculation, also mostly basic library PYTHON scientific computing library, used to perform numerical operations on large, multi-dimensional arrays)
            •  

              Why NumPy fast?

              It describes the quantization code no explicit loop indexes operation - of course, these things happen in the optimization of pre-compiled C code "behind the scenes." Vectorized code has many advantages, including:

              • Vectorized code more concise and easier to read
              • Fewer lines of code usually means fewer errors
              • The code is more similar to the standard mathematical notation (usually easier to correctly coded mathematical structure)
              • Produce more "Pythonic" code to quantify. Not vectorized, our code inefficient and difficult to readfor cycle.

              Broadcast is a term used to describe the behavior of implicit element-wise operation. Generally, in NumPy in all operations, not only the arithmetic operations, and a logic, which are operated in an implicit way by-bit elements, functions, etc., i.e., broadcasting. Further, in the above example,a and bit may be the same shape multidimensional array, or a scalar and array, or even two different shapes of arrays, that can be an array of smaller "extended" to a larger shape. The final broadcast is clear. Details about the broadcast of "rules", see .numpy.doc.broadcasting

              Who else is using NumPy?

              NumPy fully supports object-oriented approach, again from the beginning ndarray . For example, ndarray is a class with a number of methods and properties. It's a number of ways by the outermost NumPy named function reflects space, allowing the programmer to any paradigm they like to be encoded. This flexibility allows NumPy array of dialects and NumPy  ndarray class became multidimensional data exchange using Python in the de facto language.

              Features : fast, convenient, scientific computing library

              numpy create an array (matrix)

              Import numpy NP AS
               # create an array 
              A = np.array ([1,2,3,4,5 ]) 
              B = np.array (Range (1,6 )) 
              C = np.arange (1,6) # B and c compared to facilitate 
              Print (a, B, a)
               # Create above a, b, c are the same, it is to create a one-dimensional array

              The class name of the array

              a = np.array([2,3,4,5,6])
              print(type(a))

              Type array

              print(a.dtype)

               

               

              Data type of operation

              Specifies the data type of the array is created

              # A = np.array ([1,0,2,0], dtype = np.bool) = # or use dtype "?" 
              A = np.array ([1,0,2,0], dtype = " ? " ) # or use = dtype"? " 
              Print (A)

              Modifying the data type array

              # A.astype ( "I1") # or use a.astype (np.int8) 
              a.astype (np.int8)

              Decimal floating-point modification

              Import Random
               # generate a number of 10-dimensional array 
              A = np.array ([random.random () for I in Range (10 )])
               Print (A)
               Print (type (A))
               Print (a.dtype)

              # Reserved decimal places 
              B = np.round (A, 2 )
               Print (B)

               

               

              Shape array

              a = np.array([[1,2,3,4,5],[6,7,8,9,10]])
              a

               

              Check the shape of the array

              a.reshape = B (5,2 )
               Print (B)
               Print (a) # can be seen that the modified shape is not modified on the basis of a 
              # backup modified

               

               

               

               

              The 1-dimensional data into an array

              b.reshape = C (1,10) # # Note that this is not a one-dimensional array, which is a two-dimensional 
              Print ( " C " , C) 
              D = b.reshape ((10 ,))
               Print ( " D " , D) # this is a one-dimensional array of note fancy brackets 
              b.flatten () # this is a one-dimensional 
              b.reshape (1, 10) # this is a two-dimensional

               

                

              Array sums calculated

              Interestingly, this is the result of a numpy broadcast mechanism, and in the course of operations, addition, subtraction value is broadcast to all of the above elements (a bit like a computer with a LAN network broadcast mechanism)

              Computing arrays and arrays (I will jupyter direct shot out in practice notebook)

               

               

               

               

               As can be seen in the above results have inf this logo, which is infinite meaning of the word is an abbreviation of infinity.

              An array of different dimensions of computing:

               

               

               

               

               

               

               

               

              Summary: The above (ValueError: operands could not be broadcast together with shapes (3,4) (1,3)) This error is due to t1 and t3 in the array is not equal to either dimension,

              It can not be operated. Whether multi-dimensional, as long as there is a considerable dimension, you can participate in operations.

              Broadcasting principles

              • If the length of the trailing edge dimension matches two arrays (trailing dimension, i.e. the dimension from the end date) or one of the axial length is 1, they are considered compatible broadcast. Deletions will be performed on the broadcast and (or) dimension of length 1.

              ? How to understand it can dimension refers to the number of digits corresponding to the shape so the question is: shape is (3,3,3) and an array of arrays can (3,2) is calculated it?

                The answer is not in any dimension is not the same, it is not carried out operations.

              shape as (3,3,2) array can and (3,2) of the array to calculate it? What good is it?

                Operation can be carried out, the presence of the same dimensions, in other words a bit difficult to understand, we can come up with one-dimensional and two-dimensional operation conditions also mean that.

              For example: Results of data in each column of the column minus the average

              Axis (axis)

                在numpy中可以理解为方向,使用0, 1,2...数字表示,对于一个- -维数组,只有一个0轴,对于2维数组(shape (2, 2)),有0轴和1轴,对于三维数组(shape(2,2,3)), 有0, 1, 2轴
              
                有了轴的概念之后,我们计算会更加方便,比如计算一个2维数组的平均值,必须指定是计算哪个方向上面的数字的平均值
              
                那么问题来了:
              
                在前面的知识,轴在哪里?
              
                回顾np. arange (0, 10). reshape (2, 5)), reshpe中2表示0轴长度(包含数据的条数)为2, 1轴长度为5, 2*5-共10个数据

               

               

              Two-dimensional array axis:

              Axis three-dimensional array:

               

               

                

               

Guess you like

Origin www.cnblogs.com/lishuntao/p/11607983.html