NumPy Getting Started tutorial in Python

Source: Internet
Author: User
Tags shallow copy

1. What is NumPy?

Very simply, NumPy is a scientific computing library of Python that provides the functionality of matrix operations, which are generally used in conjunction with SCIPY and Matplotlib. In fact, the list already provides a matrix-like representation, but NumPy provides us with more functions. If contact with Matlab, Scilab, then numpy very good start. In the following code example, NumPy is always imported first:

The code is as follows:>>> import NumPy as NP
>>> Print Np.version.version
1.6.2

2. Multidimensional arrays

The type of the multidimensional array is: Numpy.ndarray.

Using the Numpy.array method

Produces a one-dimensional array with a list or tuple variable as a parameter:

The code is as follows:>>> print Np.array ([1,2,3,4])
[1 2 3 4]
>>> Print Np.array ((1.2,2,3,4))
[1.2 2. 3.4. ]
>>> Print Type (Np.array ((1.2,2,3,4)))
<type ' Numpy.ndarray ' >


Produces a two-dimensional array of elements as a list or a tuple variable:

The code is as follows:>>> print Np.array ([[[1,2],[3,4]])
[[1 2]
[3 4]]

When generating an array, you can specify the data type, such as Numpy.int32, Numpy.int16, and Numpy.float64, and so on:

The code is as follows:
>>> Print Np.array ((1.2,2,3,4), Dtype=np.int32)
[1 2 3 4]


Using the Numpy.arange method

The code is as follows:>>> print Np.arange (15)
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
>>> Print Type (Np.arange (15))
<type ' Numpy.ndarray ' >
>>> print Np.arange (reshape) (3,5)
[[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]]
>>> Print type (np.arange. Reshape (3,5))
<type ' Numpy.ndarray ' >


Using the Numpy.linspace method

For example, a 9 number is generated from 1 to 3:

The code is as follows:>>> print np.linspace (1,3,9)
[1.1.25 1.5 1.75 2.  2.25 2.5 2.75 3. ]


a specific matrix can be constructed using methods such as Numpy.zeros,numpy.ones,numpy.eye

For example:

The code is as follows:>>> print Np.zeros ((3,4))
[0.0. 0.0.]
[0.0. 0.0.]
[0.0. 0.0.]
>>> Print Np.ones ((3,4))
[1.1. 1.1.]
[1.1. 1.1.]
[1.1. 1.1.]
>>> print Np.eye (3)
[1.0. 0.]
[0.1. 0.]
[0.0. 1.]


Create a three-dimensional array:

The code is as follows:>>> print Np.zeros ((2,2,2))
[[0.0.]
[0.0.]

[0.0.]
[0.0.]]


Gets the properties of the array:

The code is as follows:>>> a = Np.zeros ((2,2,2))
>>> Print A.ndim #数组的维数
3
>>> Print A.shape #数组每一维的大小
(2, 2, 2)
>>> Print A.size #数组的元素数
8
>>> Print A.dtype #元素类型
Float64
>>> Print A.itemsize #每个元素所占的字节数
8


Array index, slice, assignment

Example:

The code is as follows:>>> a = Np.array ([[[2,3,4],[5,6,7]])
>>> Print a
[[2 3 4]
[5 6 7]]
>>> Print a[1,2]
7
>>> print a[1,:]
[5 6 7]
>>> Print A[1,1:2]
[6]
>>> a[1,:] = [8,9,10]
>>> Print a
[[2 3 4]
[8 9 10]]


Using the For Action element

The code is as follows:>>> for x in Np.linspace (1,3,3):
... print X
...
1.0
2.0
3.0


Basic array Operations

First construct the array A, B:

The code is as follows:>>> a = Np.ones ((2,2))
>>> B = Np.eye (2)
>>> Print a
[1.1.]
[1.1.]
>>> Print B
[1.0.]
[0.1.]


The subtraction of the array:

The code is as follows:>>> print a > 2
[[FALSE]
[FALSE]]
>>> Print A+b
[2.1.]
[1.2.]
>>> Print A-b
[0.1.]
[1.0.]
>>> Print B*2
[2.0.]
[0.2.]
>>> Print (a*2) * (b*2)
[4.0.]
[0.4.]
>>> Print b/(a*2)
[[0.5 0.]
[0.0.5]]
>>> print (a*2) **4
[16.16.]
[16.16.]

Use the Array object's own method:

The code is as follows: >>> A.sum ()
4.0
>>> a.sum (axis=0) #计算每一列 (a matrix-like column in a two-dimensional array) and
Array ([2., 2.])
>>> A.min ()
1.0
>>> A.max ()
1.0

Use the method under NumPy:

The code is as follows:
>>> Np.sin (a)
Array ([[0.84147098, 0.84147098],
[0.84147098, 0.84147098]])
>>> Np.max (a)
1.0
>>> Np.floor (a)
Array ([[1., 1.],
[1., 1.]])
>>> Np.exp (a)
Array ([[2.71828183, 2.71828183],
[2.71828183, 2.71828183]])
>>> Np.dot (a,a) # #矩阵乘法
Array ([[2., 2.],
[2., 2.]])


Merging arrays

Use the Vstack and Hstack functions under NumPy:

The code is as follows:
>>> a = Np.ones ((2,2))
>>> B = Np.eye (2)
>>> Print Np.vstack ((b))
[1.1.]
[1.1.]
[1.0.]
[0.1.]
>>> Print Np.hstack ((b))
[1.1. 1.0.]
[1.1. 0.1.]

See if these two functions involve a shallow copy of the problem:

The code is as follows:
>>> C = Np.hstack ((A, B))
>>> Print C
[1.1. 1.0.]
[1.1. 0.1.]
>>> a[1,1] = 5
>>> b[1,1] = 5
>>> Print C
[1.1. 1.0.]
[1.1. 0.1.]


As you can see, the changes in the elements in A and b do not affect C.


Deep Copy Array

The array object comes with a shallow copy and a deep copy method, but it is generally more deep-copy:

The code is as follows:>>> a = Np.ones ((2,2))
>>> B = A
>>> B is a
True
>>> C = a.copy () #深拷贝
>>> C is a
False

Basic matrix Operations

Transpose:

The code is as follows:
>>> a = Np.array ([[1,0],[2,3]])
>>> Print a
[[1 0]
[2 3]]
>>> Print A.transpose ()
[[1 2]
[0 3]]


Trace:

The code is as follows:>>> print Np.trace (a)
4


There are many methods for matrix operations in the NUMPY.LINALG module:

The code is as follows:
>>> Import Numpy.linalg as NPLG

Eigenvalues, eigenvectors:

The code is as follows:
>>> print Nplg.eig (a)
(Array ([3., 1.]), array ([[0]. , 0.70710678],
[1.,-0.70710678]])

3. Matrix

NumPy can also construct matrix objects, which are not discussed here.

NumPy Getting Started tutorial in Python

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.