Getting started with Numpy in Python

1、 What is Numpy

Very simple, Numpy is a scientific computing library of Python, which provides the function of matrix operations. It is generally used with Scipy and matplotlib. In fact, list already provides a representation similar to a matrix, but numpy provides us with more functions. If you have been exposed to matlab and scilab, then numpy is a good starting point. In the following code example, numpy is always imported first:

code show as below:

import numpy as np
print np.version.version
1.6.2

2、 Multidimensional Arrays

The type of multidimensional array is: numpy.ndarray.

Use numpy.array method

Generate a one-dimensional array with list or tuple variables as parameters:

code show as below:

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'>

Generate a two-dimensional array with list or tuple variables as elements:

code show as below:

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, etc.:

code show as below:

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

Use numpy.arange method

code show as below:

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(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))
< type 'numpy.ndarray'>

Use numpy.linspace method

For example, to generate 9 numbers from 1 to 3:

code show as below:

print np.linspace(1,3,9)
[ 1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. ]

Use numpy.zeros, numpy.ones, numpy.eye and other methods to construct a specific matrix

E.g:

code show as below:

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:

code show as below:

print np.zeros((2,2,2))
[[[ 0. 0.]
[ 0. 0.]]

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

**Get the attributes of the array: **

code show as below:

a = np.zeros((2,2,2))
print a.ndim #Dimensions of the array
3

print a.shape #The size of each dimension of the array
(2, 2, 2)

print a.size #number of elements of the array
8

print a.dtype #Element type
float64

print a.itemsize #The number of bytes occupied by each element
8

Array index, slice, assignment

Example:

code show as below:

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

Use for to manipulate elements

code show as below:

for x in np.linspace(1,3,3):
... print x
...
1.0
2.0
3.0

Basic array operations

First construct the arrays a and b:

code show as below:

a = np.ones((2,2))
b = np.eye(2)
print a
[[ 1. 1.]
[ 1. 1.]]

print b
[[ 1. 0.]
[ 0. 1.]]

Addition, subtraction, multiplication and division of arrays:

code show as below:

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 (a2)(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 method that comes with the array object:

code show as below:

a.sum()
4.0

a.sum(axis=0) #Calculate the sum of each column (a column similar to a matrix in a two-dimensional array)
array([ 2., 2.])

a.min()
1.0

a.max()
1.0

Use the method under numpy:

code show as below:

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) ##Matrix multiplication
array([[ 2., 2.],
[ 2., 2.]])

Combine arrays

Use the vstack and hstack functions under numpy:

code show as below:

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 shallow copy issues:

code show as below:

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

It can be seen that the change of elements in a and b does not affect c.

Deep copy array

Array objects come with shallow copy and deep copy methods, but generally use deep copy more:

code show as below:

a = np.ones((2,2))
b = a
b is a
True

c = a.copy() #deep copy
c is a
False

Basic matrix operations

Transpose:

code show as below:

a = np.array([[1,0],[2,3]])
print a
[[1 0]
[2 3]]

print a.transpose()
[[1 2]
[0 3]]

trace:

code show as below:

print np.trace(a)
4

There are many methods for matrix operations in the numpy.linalg module:

code show as below:

import numpy.linalg as nplg

Eigenvalue, eigenvector:

code show as below:

print nplg.eig(a)
( array([ 3., 1.]), array([[ 0. , 0.70710678],
[ 1. , -0.70710678]]))

3、 matrix

Numpy can also construct matrix objects, which is not discussed here.

Recommended Posts

Getting started with Numpy in Python
Getting started with Python(18)
Getting started with Python(9)
Getting started with Python(8)
Getting started with Python(4)
Getting started with Python (2)
Getting started with python-1
Getting started with Python(14)
Getting started with Python(7)
Getting started with Python(17)
Getting started with Python(15)
Getting started with Python(10)
Getting started with Python(11)
Getting started with Python(6)
Getting started with Python(3)
Getting started with Python(12)
Getting started with Python(5)
Getting started with Python (18+)
Getting started with Python(13)
Getting started with Python(16)
Getting Started with Python-4: Classes and Objects
Getting started with python-2: functions and dictionaries
04. Conditional Statements for Getting Started with Python
Getting started with Ubuntu
Getting started python learning steps
Use of numpy in Python development
How to get started quickly with Python
Functions in python
First acquainted with python, the grammatical rules in python
python requests.get with header
03. Operators in Python entry
Join function in Python
Play WeChat with Python
12. Network Programming in Python3
print statement in python
Web Scraping with Python
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
Concurrent requests in Python (part 2)
Talking about inheritance in Python
Python numpy implements rolling case
Noteworthy update points in Python 3.9
Containerize Python applications in 3 minutes
What is introspection in python
What is object-oriented in python
Generators and iterators in Python
Talking about strings in Python