Data analysis using python: "NumPy"

Source: Internet
Author: User
Tags abs array length cos mathematical functions shuffle sin square root

One, NumPy: Array calculation
1. NumPy is a basic package for high performance scientific computing and data analysis. It is the basis of various other tools such as pandas.
2, the main functions of NumPy:

# Ndarray, a multidimensional array structure, efficient and space-saving # mathematical functions that do not require a loop to perform fast operations on an entire set of data # * Tools to read and write disk data and tools for manipulating memory-mapped files # * Linear algebra, random number generation and Fourier transform functions # * Tools for integrating code such as C, C + +

3. Installation method: Pip Install NumPy

Second, numpy:ndarray-multidimensional array objects
1. Create Ndarray:np.array ()
2, Ndarray is a multidimensional array structure, and the difference between the list is:
--the type of elements within the array object must be the same
--the array size is not modifiable
3. Common Properties:

Transpose of the--T array (for high-dimensional arrays)
--dtype the data type of an array element
--size number of array elements
Dimensions of the--ndim array
Dimension size of the--shape array (in tuple form)
4. Common methods

#specifications for Array.shape Array#Array.ndim#data Specification for Array.dtype array#Numpy.zeros (dim1,dim2) Create a dim1*dim2 0 matrix#Numpy.arange#Numpy.eye (n)/numpy.identity (n) creating a n*n unit matrix#Numpy.array ([...], Dtype=float64)#Array.astype (numpy.float64) data form of the replacement matrix#Array.astype (float) data form of the replacement matrix#Array * Array matrix point multiplication#array[a:b] Slices#array.copy () Gets a copy of the Ndarray instead of the view#Array [A] [B]=array [A, b] both equivalent#Name=np.array ([' Bob ', ' Joe ', ' 'll ']) res=name== ' Bob ' res= Array ([True, False, false], Dtype=bool)#Data[true,false,.....] Index, only ask for true parts, remove false#by selecting the data in the array with a Boolean index, a copy of the data is always created. #data[[4,3,0,6]] index, extract the line 4,3,0,6 to form a new array#data[-1]=data[data.__len__ ()-1]#Numpy.reshape (A, b) arranges a one-dimensional array of a*b into the form of a*b#Array ([A,b,c,d],[d,e,f,g]) returns a one-dimensional array, respectively [A,d],[b,e],[c,f],[d,g]#array[[a,b,c,d]][:,[e,f,g,h]]=array[numpy.ix_ ([a,b,c,d],[e,f,g,h])]#Array. Transpose of the T array#Numpy.random.randn (A, b) generates a random array of a*b#Numpy.dot (matrix_1,matrix_2) matrix multiplication#Array.transpose ((1,0,2,etc.)) For high-dimensional arrays, transpose requires a tuple of axis numbers

Third, numpy:ndarray-data type

# ndarray Data type: Dtype: # Boolean type: Bool_ # integral type: Int_ int8 int16 int32 int64 # unsigned integral type: uint8 uint16 uint32 UInt64 # Float type: float_ float16 float32 float64 # plural type: complex_ complex64 complex128

Iv. creation of numpy:ndarray-

# Array ()         converts the list to an array, optionally specifying        The numpy version of Dtype # Arange () range to support floating-point number #  Linspace ()      similar to Arange (), the third parameter is array length #  zeros ()         creates a full 0 array based on the specified shape and dtype #  Ones () creates          an          empty Array (random value) from the specified shape and Dtypeby creating a full 1 array # empty () based on the specified shape and dtype# Eye ()           creates a unit matrix based on the specified edge length and Dtype

V. NumPy: Indexes and slices

#1. Operations between arrays and scalars#a+1 a*3 1//a a**0.5#2. Operations between arrays of the same size#a+b A/b a**b#3, the index of the array:#one-dimensional array: a[5]#Multidimensional Arrays:#list style: A[2][3]#new wording: a[2,3] (recommended)#slices of an array:#one-dimensional array: A[5:8] a[4:] a[2:10] = 1#multidimensional Arrays: A[1:2, 3:4] a[:,3:5] a[:,1]#4, emphasize: Unlike the list, array slices are not automatically copied, changes on the slice array will affect the original array. "Workaround: Copy ()"

VI, NumPy: Boolean index

problem: Give an array that selects all the numbers greater than 5 in the array. Answer: A[a>5] Principle: a>5 Each element in a is judged, returning a Boolean array Boolean index: the same size of the Boolean array into the index, will return an array of all true corresponding to the element of the problem 2: to an array, select all the array is greater than 5 even.  Question 3: Given an array, select all the numbers greater than 5 and the even number in the array. Answer: a[(a>5) & (a%2==0)] a[(a>5) | (a%2==0)]#import NumPy as NP#a = Np.array ([1,2,3,4,5,4,7,8,9,10])#a[a>5& (a%2==0)] #注意加括号, not called parenthesis error, as follows#output: Array ([1, 2, 3, 4, 5, 4, 7, 8, 9, ten])#a[(a>5) & (a%2==0)]#output: Array ([8, ten])

Vii. NumPy: Fancy Index *

Question 1: For an array, select its 1,3,4,6, 7 elements to form a new two-dimensional array. Answer: a[[1,3,4,6,7] Question 2: For a two-dimensional array, select its first and third columns to form a new two-dimensional array. Answer: a[:,[1,3]]

Viii. NumPy: General functions
General function: A function that can operate on all elements of an array at the same time

"unary function": ABS, SQRT, exp, log, ceil, floor, rint, Trunc, MODF,#numpy.sqrt (array) square root function#array of Numpy.exp (array) e^array[i]#numpy.abs/fabs (Array) calculates the absolute value#Numpy.square (array) computes the square of each element equal to Array**2#numpy.log/log10/log2 (array) computes the various logarithm of each element#numpy.sign (Array) calculates the sign of each element#Numpy.isnan (Array) calculates whether the elements are Nan#Numpy.isinf (Array) calculates whether the elements are Nan#Numpy.cos/cosh/sin/sinh/tan/tanh (array) trigonometric functions#NUMPY.MODF (array) separates the worth of integers and decimals in an array, making two arrays return#Numpy.ceil (array) is rounded up, which is the integer larger than this number#Numpy.floor (array) is rounded down, which is an integer that is smaller than this number#numpy.rint (Array) rounding#Numpy.trunc (Array) rounding to 0#Numpy.cos (array) sine value#numpy.sin (array) cosine value#Numpy.tan (array) tangent value"Two-dollar function": Add, Substract, multiply, divide, power, mod,#Numpy.add (Array1,array2) element-level addition#numpy.subtract (Array1,array2) element-level subtraction#numpy.multiply (Array1,array2) element-level multiplication#numpy.divide (Array1,array2) element-level Division Array1./array2#numpy.power (Array1,array2) element-level exponent Array1.^array2#numpy.maximum/minimum (Array1,aray2) element-level maximum value#numpy.fmax/fmin (Array1,array2) element-level maximum value, ignoring Nan#numpy.mod (Array1,array2) element-level modulo#Numpy.copysign (Array1,array2) copies the second array to the value in the first array#numpy.greater/greater_equal/less/less_equal/equal/not_equal (array1,array2)#element-level comparison operations that produce Boolean arrays#Numpy.logical_end/logical_or/logic_xor (Array1,array2) element-level truth logic operations

Ix. Supplementary Knowledge: special value of floating-point number
1. Floating point: Float
2, floating point number has two special values:
--nan (not a number): does not equal any floating-point number (Nan! = Nan)
--inf (Infinity): Larger than any floating point
--In data analysis, Nan is often represented as a data missing value
2. Create special values in NumPy: Np.nannp.inf
3. In data analysis, Nan is often used to represent data missing values
Since Nan is not even equal to himself, how can you tell if it is Nan?
With A==a, you can tell if you return FALSE.

X. NUMPY: Mathematical and statistical methods

Common functions: # sum     sum #  cumsum prefix and #  mean averaging     #  std    standard deviation  #  var     asks for variance #  min to find    minimum #  Max to    find maximum value  #  argmin    Minimum index #  argmax    Max index

Xi. NumPy: Random number generation
Random number generation function within the Np.random sub-package

Common functions: # Rand    Given shape produces a random array (number between 0 and 1)#  randint a    given shape produces a random integer #  Choice    The given shape produces a random selection #  Shuffle    same as Random.shuffle #  uniform a random array for a    given shape 

Data analysis using python: "NumPy"

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.