The NumPy introductory tutorial in Python _python

Source: Internet
Author: User
Tags shallow copy in python

1. What is NumPy

Very simply, NumPy is a scientific computing library of Python that provides the function of matrix operations, which are commonly used with scipy and matplotlib. In fact, the list already provides a representation similar to a matrix, but NumPy provides us with more functions. If contacted Matlab, Scilab, then numpy very good start. In the following code example, the NumPy is always imported first:

Copy Code code as follows:

>>> Import NumPy as NP
>>> Print Np.version.version
1.6.2


2, multidimensional array

The type of multidimensional array is: Numpy.ndarray.

Using the Numpy.array method

To produce a one-dimensional array of parameters as a list or tuple variable:

Copy Code code 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 ' >

To produce a two-dimensional array of elements in a list or tuple variable:
Copy Code code 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:
Copy Code code as follows:

>>> Print Np.array ((1.2,2,3,4), Dtype=np.int32)
[1 2 3 4]

using the Numpy.arange method
Copy Code code 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, generate 9 numbers from 1 through 3:

Copy Code code 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:

Copy Code code 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:
Copy Code code as follows:

>>> Print Np.zeros ((2,2,2))
[[[0.0.]
[0.0.]]

[[0.0.]
[0.0.]]]


to get the properties of an array:
Copy Code code 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 indexes, slices, assigning values

Example:

Copy Code code 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
Copy Code code as follows:

>>> for X in Np.linspace (1,3,3):
... print X
...
1.0
2.0
3.0


Basic array Operations

First construct array A, B:

Copy Code code as follows:

>>> a = Np.ones ((2,2))
>>> B = Np.eye (2)
>>> Print a
[[1.1.]
[1.1.]]
>>> Print B
[[1.0.]
[0.1.]]

Subtraction of the array:
Copy Code code as follows:

>>> Print a > 2
[[False false]
[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.]]

Ways to use an array object from the band:

Copy Code code as follows:

>>> A.sum ()
4.0
>>> a.sum (axis=0) #计算每一列 (columns in a two-dimensional array similar to matrices)
Array ([2., 2.])
>>> A.min ()
1.0
>>> A.max ()
1.0

Use the method under NumPy:

Copy Code code 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:

Copy Code code as follows:

>>> a = Np.ones ((2,2))
>>> B = Np.eye (2)
>>> Print Np.vstack ((a,b))
[[1.1.]
[1.1.]
[1.0.]
[0.1.]]
>>> Print Np.hstack ((a,b))
[[1.1. 1.0.]
[1.1. 0.1.]]

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

Copy Code code 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 change of elements in A and B does not affect C.


Deep Copy Array

The array object has a method of shallow copy and deep copy, but generally uses more deep copies:

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

Basic matrix Operations

Transpose:

Copy Code code as follows:

>>> a = Np.array ([[[1,0],[2,3]])
>>> Print a
[[1 0]
[2 3]]
>>> Print A.transpose ()
[[1 2]
[0 3]]

Trace:
Copy Code code as follows:
>>> print Np.trace (a)
4

There are a number of methods for matrix operations in the NUMPY.LINALG module:
Copy Code code as follows:

>>> Import Numpy.linalg as NPLG

Eigenvalues, eigenvectors:

Copy Code code 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.

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.