NumPy notes,

Source: Internet
Author: User

NumPy notes,

I recently read the book "using Python for data analysis", and I will also view some information on the Internet, so I will summarize some of the more important parts, on the one hand, I will deepen my memory of some methods and facilitate my future summary.

Basic concepts:

Numpy (Numerical Python) is a basic package for high-performance scientific computing and data analysis.
NumPy ndarray: multidimensional array object, fast and flexible big dataset container
All elements must be of the same type

Refer to the official documentation Documentation for details.

Download the following source code: https://github.com/NSGUF/PythonLeaning

 Common attributes:

Print (np. version. version) # numpy version data1 = [1, 2, 4] arr1 = np. array (data1) print (arr1) data2 = [[1, 2, 3, 4], [5, 6, 7, 8] arr2 = np. array (data2) print (arr2) print ("------------ attribute ---------------") print (arr2.ndim) # print the dimension of the array (arr2.shape) # print the dimension of the array (arr2.size) # print (arr2.dtype) # print (arr2.itemsize) of the element type in the array # print (arr2.data) of the byte size of each element in the array) # buffer of the actual array element # NumPy comes with many data types, such as int16 and int32. np is available for values. int16 # NumPy is converted to print (np. int32 (12.123 ))

How to create an array:

Print ("------------ method for creating an array ------------") print (np. ones (2, 3, 4), dtype = np. int16) # print (np. empty (2, 3) # if it is an empty multi-digit group, only the memory is allocated and not initialized. Therefore, print (np) runs fast. full (4, np. pi) # initialize to the specified value print (np. random. random (2, 3) # generate a random array print (np. arange (10, 30, 5) # returns an arithmetic difference of 5 starting from 10, excluding 30 print (np. linspace (, 4) # The first number is 10, the last number is 30, and the fifth equidifference is print (np. logspace (, 5) # returns five print (np) numbers from 10 ^ 0 to 10 ^ 2. arange (4) # equivalent to the following formula: print (np. arange (0, 4, 1) print (np. arange (12 ). reshape () # change to shape 4, 3a = np. arange (24 ). reshape (2, 3, 4) # print (. resize (24) # resize is used in the same way as reshape, but calling resize will change itself, and reshape will not be B = np. arange (4) print (a) # shape is 2, 3, 4, ps: the total number must be the product of shape. Otherwise, print (a [1,...]) is returned. # equivalent to a [1] print (a [1]) # 2nd full columns print (a []) # Take the third column of the first full column print (, 1]) # 2nd columns of 2nd rows in 2nd columns, an element

Operations and operations on Arrays:

Print ("------------ operate on arrays --------------") c = a-B # All elements can be included in addition, subtraction, multiplication, division, and shape, note that when the divisor is 0, print (c) print (a ** 2) # processes each element of the array. You can also traverse print (. sum () # sum print (. min () # print (. max () # print (. cumsum () # obtain the sum of the First n of each number. For example, if the first one is 0, the first two are 0 + 1, and the first three are 0 + 1 + 2, then d = np. arange (12 ). reshape (3, 4) print (d. sum (axis =-2) # sum print (d. min (axis = 0) # print (d. max (axis =-1) # print (d. mean () # arithmetic mean print (d. std () # standard deviation print (d. var () # variance print (d. argmin () # print (d. argmax () # maximum index print (d. repeat (5) # repeat print (np. power (B, 3) # print (np. repeat (3, 4) # create a one-dimensional array with 3 elements and print (d. cumsum (axis = 1) # obtain the sum of the First n of each number. For example, the first one is 0, and the first two are 0 + 1, print (d. cumprod () # cumulative product of all elements # conclusion: When the axis parameter is not set, all elements are used by default. If the value is 0 or-2, the first number in each column is displayed, when the value is 1 or-1, the operation is performed in columns. If the value is other values, the error s = np is returned. array ([, 23]) s. sort () # sort print (np. unique (s) # locate and sort unique values

Boolean array method:

Print ("------------ Boolean value array method --------------") bools = np. array ([False, False, True, False]) print (bools. any () # whether one or more trueprints (bools. all () # all are Trueprint (. ravel () # Set the shape value to (total number of elements), which is equivalent to. reshape (. size) print (. ravel (). shape) print (np. sin (a) print (np. floor (a) # rounded up c =. copy () # Deep copy print (c is a) print ()

 How to traverse the array:

Print ("------------ traverse the array --------------") for row in a: # print (row) for element in a. flat: # traverse each element print (element)

 Input and Output of array files:

Print ("------------ array file input and output ------------") np. save ("save_a.yy", a) # convert the array. if the file name is not modified, the file name must be added to the disk np. load ("save_a.yy") np. savez ('array _ a_ B .npz', a = a, B = B) # store arch = np together with a and B. load ('array _ a_ B .npz') print (arch ['B']) # obtain array B np. savetxt ("text_ B .txt", B, delimiter = ',') # Write the txt file, with the separator arr = np. loadtxt ("text_a.txt", delimiter = ',') # Read the txt file, with the separator print (arr)

  Some methods for linear algebra and operations on matrices:

Print ("---------------------- linear algebra -------------------------") print (np. dot (a, B) # print (. transpose () # transpose print (np. rot90 (a, 3) # select 90 degrees counterclockwise, and the second parameter is the number of rotations print (np. fliplr (a) # print (np. flipud (a) # print (np. roll (a, 1, axis = 1) # Rolling Displacement print (np. trace (a) # trace, diagonal and e = np. array ([[1, 2, 3], [1, 2, 3], [1, 2, 3]) print (np. diag (e) # returns the diagonal element print (np. vstack (np. array ([[1, 1], [1, 1]), np. array ([[], []) # merge print (np. hstack (np. array ([[1, 1], [1, 1]), np. array ([[], []) # flip after merging

Random Number Generation:

Print ("---------------------- Random Number Generation -----------------------") print (np. random. normal (size = () # a random number with a normal distribution to generate an array of 4*4. Compared with the python built-in random, np. random is much faster than print (np. random. binomial (10, 0.5, 100) # print (np. random. shuffle (B) # print (np. random. permutation (a) # returns a new arrayprint (np. random. rand (3, 2) # print (np) of evenly distributed sample values. random. randint (5, size = () # The upper limit of the first attribute indicates 0-5, print (np. random. randn () # print (np) sample value of normal distribution (Mean Value: 0, standard deviation: 1. random. beta (10, 10, size = 10) # beta distribution

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.