NumPy Basics: Arrays and vector calculations

Source: Internet
Author: User

The boss fire today, or continue to transcription it, to appease my wounded little heart. Knowledge still has to accumulate slowly, step by step, this perhaps is the quickest shortcut.

------2015-2-16------------------------------------------------------------------

    • NumPy Ndarray: A multidimensional Array object

An important feature of NumPy is the n-dimensional array object (Ndarray), which is a fast and flexible large data set container. Ndarray is a generic homogeneous data multidimensional container, which means that all elements must be of the same type. Each array has a shape (a tuple that represents the size of each dimension) and Dtype (an object representing the array data type).

Import NumPy as npin[3]: data=[[1,2,3],[4,5,6]]in[4]: arr=np.array (data) in[6]: arrout[ 6]: Array ([[1, 2, 3],       [4, 5, 6]]) in[7]: arr.shapeout[7]: (2L, 3L) in[ 8]: arr.dtypeout[8]: Dtype ('int32')

Create Ndarray

Array creation function
Function Description
Array Converts the input data (list, tuple, array, or other sequence type) to Ndarray. Either infer the Dtype, or display the specified dtype. Default direct copy of input data
Asarray Converts the input to Ndarray if the input itself is a ndarray and does not replicate
Arange Returns a ndarray instead of a list
Ones, Ones_like Creates a full 1 array based on the specified shape and dtype. Ones_like takes another array as an argument and creates a full 1 array based on its shape and dtype
Zeros, Zeros_like Similar to ones and ones_likes only produce a full 0 array
Empty, Empty_like Creates a new array, allocates only memory space but does not populate any values
Eye, identity Create a NXN unit matrix

Ndarray Data types

Int8,int16,int32,int64 signed integral type

Uint8,uint16,uint32,uint64 unsigned integral type

float16,float32,float64,float128 single precision, multi-precision, extended accuracy

The plural of complex64,complex128,complex256, respectively, with 32,64,128

bool

Object Python Data Objects

String_ fixed-length string data types

Unicode_ fixed-length Unicode types

In[23]: Arr.astype (np.float64) out[: Array ([1.,  2.,  3.,  4.,  5.]) in[]: h1=arr.astype (np.int16) in[[]: h1.dtypeout[[]: Dtype ('int16 ')

Operations between arrays and scalars

IN[2]:ImportNumPy as npin[3]: Arr=np.array ([[1,2,3],[4,5,6]]) in[4]: arr*arrout[4]: Array ([[1, 4, 9],       [16, 25, 36]]) in[5]: arr+arrout[5]: Array ([[2, 4, 6],       [ 8, 10, 12]]) in[6]: arr*4out[6]: Array ([[4, 8, 12],       [16, 20, 24]]) in[7]: arr**0.5out[7]: Array ([[1., 1.41421356, 1.73205081],       [ 2., 2.23606798, 2.44948974]])

Basic indexes and slices

IN[8]: Np.arange (ten) out[8]: Array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) in[9]: Arr=np.arange (ten) in[10] : Arr[5:8]out[]: Array ([5, 6, 7]) in[]: arr_slice=arr[5:8]in[]: arr_slice[1]out[ ]: 6in[]: arr_slice[1]=123456in[]:arrout[]: Array ([     0,      1,      2,      3,      4,      5, 123456,      7,            8,      9])

Warning: Ndarray a copy of a slice rather than a view, you need to display arr[5:8].copy ()

Transpose of arrays and axes swapping

IN[16]: Arr=np.arange (0). Reshape ((3,5)) in[[]: arrout[]: Array (  [[[1], "  2", "["]] "." [[[]]].  3,  4],       5,  6,  7,  8,  9],       [Ten, one, one, and up]]) in[ ]: arr. tout[]: Array ([[0  ,5, ten],       1,  6, one),       2,  7, 12 ],       3,  8, [],       4,  9,]]) in[]: Np.dot (Arr,arr. T) out[         , 80, 255, 430],

    • General functions: Fast element Progression Group functions

    • Using Arrays for data processing

    • File input and output for arrays

    • Linear algebra

    • Random number generation

IN[20]: Samples=np.random.normal (size= (bis)) in[]: samplesout[1.2160082,  0.34629744, -0.70813727,  2.59673398],       [-1.32110632,  1.19660352,  0.08227731,  0.24075048],       [-0.29301216,  0.42639032, -1.76321448, -1.05558718],       0.0872803 ,  0.25871173,  0.63373105,  0.59362002])

The Numpy.random module is faster than the python built-in random module.

Partial numpy.random function

NumPy Basics: Arrays and vector calculations

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.