Getting started with Numpy in Python

Source: Internet
Author: User
This article mainly introduces the Numpy tutorial in Python and focuses on array operations in the matrix. if you need it, refer 1. what is Numpy?

Numpy is a Python Scientific computing library that provides matrix computing functions. it is generally used with Scipy and matplotlib. In fact, list already provides a representation similar to a matrix, but numpy provides more functions for us. If you have been in touch with matlab and scilab, numpy is a good start. In the following code example, numpy is always first imported:

The code is as follows:


>>> Import numpy as np
>>> Print np. version. version
1.6.2


2. multi-dimensional array

The multidimensional array type is numpy. ndarray.

Use numpy. array method

Use the list or tuple variable as the parameter to generate a one-dimensional array:

The code is as follows:

>>> Print np. array ([1, 2, 3, 4])
[1 2 3 4]
>>> Print np. array (1.2, 4 ))
[1.2. 3. 4.]
>>> Print type (np. array (1.2, 4 )))


Use the list or tuple variable as the element to generate a two-dimensional array:

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:

The code is as follows:


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


Use 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 ))

>>> Print np. arange (15). reshape (3, 5)
[[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]
>>> Print type (np. arange (15). reshape (3, 5 ))


Use the numpy. linspace method

For example, a number of 9 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.]


You can use numpy. zeros, numpy. ones, numpy. eye, and other methods to construct a specific matrix.

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 3D array:

The code is as follows:


>>> Print np. zeros (2, 2, 2 ))
[[0. 0.]
[0. 0.]

[[0. 0.]
[0. 0.]


Get the attributes of the array:

The code is as follows:


>>> A = np. zeros (2, 2, 2 ))
>>> Print a. ndim # dimension of the array
3
>>> Print a. shape # size of each dimension of the array
(2, 2, 2)
>>> Print a. size # Number of elements in the array
8
>>> Print a. dtype # element type
Float64
>>> Print a. itemsize # Number of bytes occupied by each element
8


Array index, slice, and assignment

Example:

The code is as follows:


>>> A = np. array ([[2, 3, 4], [5, 6, 7])
>>> Print
[[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
[[2 3 4]
[8 9 10]


Use the for Operation ELEMENT

The code is as follows:


>>> For x in np. linspace (1, 3 ):
... Print x
...
1.0
2.0
3.0


Basic Array Operations

First, construct arrays a and B:

The code is as follows:


>>> A = np. ones (2, 2 ))
>>> B = np. eye (2)
>>> Print
[[1. 1.]
[1. 1.]
>>> Print B
[[1. 0.]
[0. 1.]


Addition, subtraction, multiplication, and division of arrays:

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.]

The method that comes with the array object:

The code is as follows:


>>> A. sum ()
4.0
>>> A. sum (axis = 0) # calculate the sum
Array ([2., 2.])
>>> A. min ()
1.0
>>> A. max ()
1.0

Use the numpy method:

The code is as follows:


>>> Np. sin ()
Array ([[0.84147098, 0.84147098],
[0.84147098, 0.84147098])
>>> Np. max ()
1.0
>>> Np. floor ()
Array ([[1., 1.],
[1., 1.])
>>> Np. exp ()
Array ([[2.71828183, 2.71828183],
[2.71828183, 2.71828183])
>>> Np. dot (a, a) # Matrix multiplication
Array ([[2., 2.],
[2., 2.])


Merge arrays

Use the vstack and hstack functions in numpy:

The code is 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.]

Check whether the two functions involve the shortest copy 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.]


We can see that the change of elements in a and B does not affect c.


Deep copy array

The array object comes with the method of shortest copy and deep copy, but the method of deep copy is usually more:

The code is as follows:

>>> A = np. ones (2, 2 ))
>>> B =
>>> B is
True
>>> C = a. copy () # deep copy
>>> C is
False

Basic matrix operations

Transpose:

The code is as follows:


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


Trace:

The code is as follows:

>>> Print np. trace ()
4


The numpy. linalg module has many matrix calculation methods:

The code is as follows:


>>> Import numpy. linalg as nplg

Feature value and feature vector:

The code is as follows:


>>> Print nplg. eig ()
(Array ([3., 1.]), array ([0., 0.70710678],
[1.,-0.70710678])

3. Matrix

Numpy can also construct a Matrix object, which is not discussed here.

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.