Python--numpy basic data types, arithmetic operations, combinations, split functions

Source: Internet
Author: User
Tags arithmetic float double shallow copy

0 NumPy Array

NumPy array: The NumPy array is a multidimensional array object, called Ndarray. It consists of two parts:
The actual data
Metadata that describes the data

NumPy Array Properties:
Ndim (number of weft, X, y 2), shape (latitude, 2*3), reshape (latitude), size: Number of elements, Dtype: element data type, ItemSize: Byte size of all elements
To create an array:
Using the array function, a = Array ([2,3,4]), B = Array ([(1.5,2,3), (4,5,6)])
You can explicitly specify the type of the elements in the array at creation C = Array ([[up], [3,4]], Dtype=complex)
D = zeros ((3,4))
Ones ((2,3,4), dtype=int16) #手动指定数组中元素类型
Empty ((2,3))
Full ((2,3), 8)
NumPy provides a function similar to Arange to return an array in the form of a sequence:
Arange (10, 30, 5)
Array ([10, 15, 20, 25])
Arange (0,2,0.5)
Array ([0., 0.5, 1., 1.5])
A = Array ([1,2,3,4])
a2 = Array ([1,2,3,4],[1,2,3,4],[1,2,3,4])

Np.ones ((2,3))
Np.zeeros ((2,4))
Np.full ((2,2), 8)

1 basic data types in NumPy
Name Description
BOOL A Boolean type stored with one byte (True or FALSE)
Inti An integer (typically int32 or Int64) whose size is determined by the platform on which it resides
int8 one byte size, 128 to 127
Int16 integers, 32768 to 32767
Int32 integer, 2 * * 31 to 2 * * 32-1
Int64 integer, 2 * * 63 to 2 * * 63-1
uint8 unsigned integer, 0 to 255
UInt16 unsigned integer, 0 to 65535
UInt32 unsigned integer, 0 to 2 * * 32-1
UInt64 unsigned integer, 0 to 2 * * 64-1
Float16 semi-precision floating-point number: 16-bit, sign 1-bit, exponent 5-bit, precision 10-bit
float32 single-precision floating-point number: 32-bit, sign 1-bit, exponent 8-bit, precision 23-bit
Float64 or float double-precision floating-point number: 64-bit, 1-bit, exponent 11-bit, precision 52-bit
complex64 plural with two 32-bit floating-point numbers for real and imaginary parts
complex128 or complex complex numbers, with two 64-bit floating-point numbers representing real and imaginary parts respectively

Output array

2 NumPy Array 2

Array operations: The arithmetic operations of an array are calculated by element. The array operation creates a new array that contains the result of the operation, and some operators, such as + = and *=, are used to change an existing array without creating a new array.
Basic operations: +,-, *./by element
Index slices and iterations: As with lists and other Python sequences, one-dimensional arrays can be indexed, sliced, and iterated. A[2],a[2:5], a[::-1] # Invert A
a[:6:2]=-1000 # is equivalent to a[0:6:2]=-1000, from the beginning to the 6th position, every other element assigns it a value of-1000
For I in A:
Print i** (1/3.)

Multidimensional arrays can have an index on each axis. These indexes are given by a comma-separated tuple. B[0:5, 1]

Shapes Shape operations
Change the shape of an array: A.ravel () # flattening an array


3 Custom Structure Array
Student= Dtype ({' names ': [' name ', ' age ', ' weight '], ' formats ': [' S32 ', ' I ', ' F ']}, align = True)
A= Array (["Zhang", + 65.5), ("Wang", 55.2)], Dtype =student)

Combination function: 2 * A
Horizontal combination: Hstack ((A, b)), which can also be obtained by concatenate the function and specifying the corresponding axes: concatenate ((A, B), Axis=1)
Vertical combination: Vstack ((A, B))
Depth combination: Dstack ((A, b)) a combination of the third axis (that is, the depth) of the array
Row combination: Row_stack (one, both), each line is combined
Column combination: Column_stack ((Oned,two))
Split arrays: In NumPy, the functions that divide an array are hsplit, Vsplit, Dsplit, and split. You can split an array into sub-arrays of the same size, or specify where the original array is split

Split horizontally: Hsplit (A, 3), Split (A, 3, Axis=1)
Vertical split: Vsplit (A, 3), also available through the Split function and specifying the axis as a result: Split (A, 3, axis=0)
Depth-oriented segmentation: Dsplit (c, 3)

Copy and Mirror View
Do not copy at all:
Simple assignment without copying the array objects or their data.
Views view and Shallow copy:
The C = a.view () Slice array Returns a view of it, with different array objects sharing the same data. The view method creates a new array object that points to the same data.
Deep copy:
D = a.copy () This copy method completely copies the array and its data.

For more information, please refer to:

http://blog.csdn.net/sunny2038/article/details/9023797

Python--numpy basic data types, arithmetic operations, combinations, split functions

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.