Python & NumPy Tutorials (next) __python

Source: Internet
Author: User
Tags cos mathematical functions sin square root

NumPy

NumPy is the core repository of Python scientific computing. It provides high-performance multidimensional array objects, as well as tools for using these arrays. If you are already familiar with MATLAB, you can find this tutorial to start using NumPy.

Arrays

An NumPy array is a network (grid) of values of the same type, and is indexed by a nonnegative integer. The dimension is rank in the array, and the shape of the array is an integer tuple that gives the size of each dimension of the array.

We can use the nested Python lists to initialize the NumPy array and use square brackets to access the elements.

Import NumPy as np

a = Np.array ([1, 2, 3])  # Create a rank 1 array
print type (a)            # prints ' <type ' numpy . Ndarray ' > "
print a.shape            # prints" (3,) "
print A[0", a[1], a[2]   # prints "1 2 3"
a[0] = 5                 #  Change an element of the array
print a                  # prints "[5, 2, 3]"

B = Np.array ([[[1,2,3],[4,5,6]])   # Create A Rank 2 array
print B.shape                     # prints "(2, 3)"
print b[0, 0], b[0, 1], b[1, 0]   # prints "1 2 4"

NumPy also provides a number of functions to create arrays.

Import NumPy as np

a = Np.zeros ((2,2))  # Create An array of ' all zeros
print a              # prints ' [0.  0.]
                     #          [0.  0.]] "
    
B = np.ones ((1,2))   # Create An array of ' all ones
print b              # prints ' [1.  1.]] "

C = np.full ((2,2), 7) # Create a constant array
print C               # prints" [7.  7.]
                      #          [7.  7.]] "

d = Np.eye (2)        # Create a 2x2 identity matrix
print D              # prints" [[1].  0.]
                     #          [0.  1.]] "
    
e = Np.random.random ((2,2)) # Create An array filled with random values
print e                     # might print" [[0.9 1940167  0.08143941]
                            #               [0.68744134  0.87236687]] "

You can find more methods of array creation in the official document.

Array Indexing

NumPy provides a number of ways to index an array.

slicing: like Python lists, numpy arrays can be split. Arrays may be multidimensional, and you must define a shard for each dimension.

Import NumPy as NP

# Create The following rank 2 array with shape (3, 4)
# [[1  2 3 4  ]
# [  5
  6  7  8]
#  [9]]
a = Np.array ([[[1,2,3,4], [5,6,7,8], [9,10,11,12]]) # Use

slicing To pull out the subarray consisting of the the the The 2 rows
# and columns 1 and 2. B is the following array of Shape 2, 2):
# [[2 3]
# [  6 7]]
B = a[:2, 1:3]

# A slice of an array is a view to the same data, so modif Ying It
# would modify the original array.
Print a[0, 1]   # prints "2"
b[0, 0] = +    # b[0, 0] is the same piece of data as a[0, 1]
print a[0, 1]
  # Prints "77"

You can mix the integer index and the Shard index. However, doing so produces a lower-order array. This and MATLAB in the operation of array segmentation is completely different.

Import NumPy as NP

# Create The following rank 2 array with shape (3, 4)
# [[1  2 3 4  ]
# [  5
  6  7  8]
#  [9]]
a = Np.array ([[[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Two ways of AC Cessing the data in the middle row of the array.
# mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the Same rank
as the # Original array:
ROW_R1 = a[1,:]    # rank 1 view of the second row of a  
row_r2 = a[1  : 2,:]  # Rank 2 view of the second row of a
print row_r1, row_r1.shape  # prints "[5 6 7 8] (4,)"
print  ROW_R2, Row_r2.shape  # prints "[[5 6 7 8]] (1, 4)"

# We can make the same distinction when accessing columns of An array:
COL_R1 = a[:, 1]
col_r2 = a[:, 1:2]
print col_r1, col_r1.shape  # prints "[2  6 10] (3, "
print col_r2, col_r2.shape  # prints" [[2]
                            #          [6] # [          10]] (3, 1) "

Integerarray Indexing: when using slicing index into the numpy array, the resulting array is a child array of the original array. In contrast, an integer-form array index allows you to create arbitrary arrays using data from other arrays. Examples are as follows:

Import NumPy as np

a = Np.array ([[[1,2], [3, 4], [5, 6]])

# An example of integer array indexing.
# The returned array would have shape (3,) and 
print a[[0, 1, 2], [0, 1, 0]]  # prints "[1 4 5]"

# The above Exa Mple of integer array indexing is equivalent to this:
print Np.array ([a[0, 0], a[1, 1], a[2, 0]])  # prints ' [1 4 5 "

# When using an integer array indexing, can reuse the same
# element from the source array:
print a[[0, 0], [1, 1]]  # prints ' [2 2] '

# equivalent to the previous integer array indexing example
print Np.array ([a[ 0, 1], a[0, 1]]  # prints "[2 2]"

A useful technique for integer indexing is to select or change an element from each row of the matrix.

Import NumPy as NP

# Create a new array from which we select elements
a = Np.array ([[[1,2,3], [4,5,6], [7,8,9] , [Ten, One]])

print a  # prints Array ([[1,  2,  3],
         #                [4,  5,  6],
         # [                7,< C12/>8,  9],
         #                [A, one,]] "

# Create an array
of indices b = Np.array ([0, 2, 0, 1])

# Sele CT one element from all row of a using the indices in B
print A[np.arange (4), b]  # prints "[1  6  7 11 ] "

# mutate one element from each row of ' a using the ' indices
in B a[np.arange (4), b] = = Ten

print a  # p Rints "Array ([[  2,  3],
         #                [4,  5,], #                [  8,  9],
         #                [ 10, 21, 12]]

Booleanarray Indexing: A Boolean array index allows you to select any element of a group. This method of indexing is used to select elements in an array that meet specific conditions. Examples are as follows:

Import NumPy as np

a = Np.array ([[[1,2], [3, 4], [5, 6]])

Bool_idx = (A > 2) # Find the  elements of a Re bigger than 2;
                    # This returns a numpy array
                    of Booleans to the same # shape as a, where each slot of Bool_idx tells
                    # whether Tha t element of A is > 2.
            
Print Bool_idx      # prints [[False false]
                    #          [true  ]
                    #          [true]  '

# We use Boolean array indexing to construct a rank 1 array
# Consisting of the elements of a corresponding to the True values
# of Bool_idx
print A[bool_idx]  # prints ' [3 4 5 6] '

# We can do all of the ' above in a single concise Statement:
print a[a > 2]     # prints "[3 4 5 6]"

For simplicity, many details about the NumPy array index are not covered, and if you want to learn more, you can read the official documentation.

 

Datatypes

Each numpy array is a network of elements of the same type. NumPy provides a number of numeric types that you can use to build arrays. When you create an array, NumPy tries to guess the data type, but the function that creates the array usually contains an optional parameter to determine the data type. Here's an example:

Import NumPy as np

x = Np.array ([1, 2])  # Let NumPy choose datatype
print x.dtype         # prints "Int64" 
  
   x = Np.array ([1.0, 2.0])  # Let NumPy choose the datatype
print x.dtype             # prints "float64"

x = Np.array ([1 , 2], Dtype=np.int64)  # Force A particular datatype
print X.dtype                         # prints "Int64"

  

You can also read all the NumPy data types in the official document.

Array Math

Basic mathematical functions are element-level operations on an array, and are available in operator overloading and in the functions of the NumPy module:

Import NumPy as np

x = Np.array ([[[[1,2],[3,4]], dtype=np.float64)
y = Np.array ([[[5,6],[7,8]], Dtype=np.float64) c2/># elementwise sum; Both produce the array
# [[6.0  8.0]
#  [10.0 12.0]]
print x + y
print np.add (x, y)

# Elementwi SE difference; Both produce the array
# [[ -4.0-4.0]
# [  -4.0-4.0]]
print x
-y print np.subtract (x, y)

# E Lementwise product; Both produce the array
# [[5.0 12.0]
#  [21.0 32.0]]
print x * y
print np.multiply (x, y)

# E Lementwise Division; Both produce the array
# [[0.2         0.33333333]
#  [0.42857143  0.5       ]]
print x/y
Print np.divide (x, y)

# elementwise square root produces the array
# [1.          1.41421356]
#  [1.73205081  2        ]]
Print np.sqrt (x)

Note that unlike MATLAB, "*" means multiplication of elements, not matrix multiplication. We use the DOT function to compute the vector's inner product, the vector is multiplied by the matrix, and the matrix multiplication. The dot can be either a function in the NumPy module or an example method of an Array object:

Import NumPy as np

x = Np.array ([[[1,2],[3,4]])
y = Np.array ([[[[[5,6],[7,8]]])

v = Np.array ([9,10])
w = np.ar Ray ([one])

# Inner product of vectors both produce 219
print V.dot (w)
print Np.dot (V, W)

# matrix/ve ctor product; Both produce the rank 1 array [i]
print X.dot (v)
print Np.dot (x, v)

# Matrix/matrix product; both Produ Ce the rank 2 array
# [[] # [$]  ]
print X.dot (y)
print np.dot (x, y)

NumPy provides a number of useful functions to perform operations on arrays; The most useful one is sum:

Import NumPy as np

x = Np.array ([[[[1,2],[3,4]])

print np.sum (x)  # Compute sum of all elements; prints "ten" 
  print np.sum (x, axis=0)  # Compute sum of each column; prints "[4 6]"
print np.sum (x, Axis=1)  # Compute sum of each row; Prints "[3 7]"

You can find a list of the mathematical functions provided by NumPy in the official document.

In addition to using arrays in mathematical functions, we often need to manipulate or refactor the data in an array. The simplest example of this operation is to transpose a matrix; to transpose a matrix, you can use the T property of an array object.

Import NumPy as np

x = Np.array ([[[[[1,2], [3,4]])
print x    # prints "[[1 2]
           # [          3 4]]"
print x.t  # Prints "[[1 3]
           #          [2 4]]"

# Note this taking the transpose of a rank 1 array does nothing:
v = Np.array ([ 1,2,3])
print v    # prints [1 2 3] "
print v.t  # prints" [1 2 3] "

NumPy provides a number of functions for manipulating arrays; You can find them in official documents.

Broadcasting

Broadcasting is a powerful mechanism that allows numpy to use arrays of different shapes when doing arithmetic operations. Often this happens, we have a decimal group and a large array, and we use the decimal group to operate on large arrays multiple times.

For example, imagine that we're going to add a constant vector to each row of the matrix. We can do this:

Import NumPy as NP

# We Add the vector v to each row of the Matrix X, # Storing the ' result ' in the
matrix Y
  x = Np.array ([[[1,2,3], [4,5,6], [7,8,9], [Ten, One]])
v = Np.array ([1, 0, 1])
y = np.empty_like (x)   # creat e an empty matrix and the same shape as x

# ADD The vector v to each row of the matrix X with a explicit loop
fo R i in range (4):
    y[i,:] = X[i,:] + v

# now Y is the following
# [[2  2  4]
# [  5  5< C16/>7]
#  [8  8]
#  [one]]
print y

This works, but when the matrix X is very large, it is very slow to compute an explicit loop in Python. Notice that adding a vector v to each row of the matrix X is equivalent to building a matrix vv by vertically storing multiple copies of V, and then performing an element-level summation on X and VV. We can do this in this way:

Import NumPy as NP

# We Add the vector v to each row of the Matrix X, # Storing the ' result ' in the
matrix Y
  x = Np.array ([[[1,2,3], [4,5,6], [7,8,9], [Ten, One]])
v = Np.array ([1, 0, 1])
vv = Np.tile (V, (4, 1))  # St Ack 4 copies of V on top of each of
the other print vv                 # prints [[1 0 1]
                         #          [1 0 1] # [
                         1 0 +] # #
  [1 0 1]] "
y = x + vv  # ADD x and vv elementwise
print y  # prints" [[2  2  4
         #          [ 5  5  7]
         #          [8  8]
         #          [11 11 13]] "

NumPy broadcasts allow us to do this, but we don't really have to create multiple copies of V. Consider using a broadcast version:

Import NumPy as NP

# We Add the vector v to each row of the Matrix X, # Storing the ' result ' in the
matrix Y
  x = Np.array ([[[[1,2,3], [4,5,6], [7,8,9], [Ten, One]])
v = Np.array ([1, 0, 1])
y = x + v  # ADD v to each RO W of x using broadcasting
print y  # prints [[2  2  4]
         #          [5  5  7] # [          8
  8]
         #          [11 11 13]] "

Even if the shape of x is (4,3), and the Shape of V is (3,), y = x + v can still work, thanks to the broadcast; this line works as if V's shape is (4,3), each row is a copy of V, and the sum is executed at the element level.

Broadcast two arrays together follow these rules:

(1) If the array does not have the same order, consider the lower order array in advance until the two shapes have the same length.

(2) If they have the same size on one dimension, or if one of the sizes on that dimension is 1, say they are compatible.

(3) If they are compatible on all dimensions, they can be broadcast together.

(4) After broadcasting, the shape of each array behaves as if it were the largest element in the shape of two arrays.

(5) In any dimension, if the size of an array is 1 and the other is greater than 1, the first array behaves as if it were copied on the dimension.

If this explanation doesn't work, you can read the official document or the explanation here.

The

functions that support broadcasting become common functions. You can find a list of common functions in the official document. Here are some of the applications for broadcasting:

Import NumPy as NP # Compute outer product of vectors v = np.array ([1,2,3]) # V has shape (3,) w = Np.array ([4,5]) # W has shape (2,) # to compute a outer product, we have a column # Vector of shape (3, 1). 
We can then broadcast it against W to yield # A output of shape (3, 2), which is the outer product of V and W: # [[4 5] # [8] # [a]] print np.reshape (V, (3, 1)) * W Add a vector to each row of a matrix x = Np.array ([[[1,2,3], [4,  5,6]] # x has shape (2, 3) and V has shape (3,) so they broadcast to (2, 3), # giving the following matrix: # [[2 4 6] #
[5 7 9]]
Print x + V # Add a vector to each column of a matrix # X has shape (2, 3) and W has shape (2,). # If We transpose x then it has shape (3, 2) and can is broadcast # against W to yield a result of shape (3, 2); Transposing this result # yields the final result of shape (2, 3) which is the Matrix X and # The vector W added to each Column. Gives the following matrix: # [[5 6 7] # [9 10 11]] Print (x.t + W).
T # Another solution is to reshape W. A row vector of shape (2, 1);
# we can then broadcast it directly against X to produce the same # output. Print X + np.reshape (W, (2, 1)) # Multiply a matrix by a constant: # x has shape (2, 3).
NumPy treats scalars as arrays of shape ();  # These can is broadcast together to shape (2, 3), producing the # following array: # [[2 4 6] # [8]] Print x *
 2

Broadcasting makes the code more concise and fast, and you should try to use it wherever possible.

NumPy Documentation

This brief overview covers a lot of important things you should know about NumPy, but it's far from complete. Look at the NumPy index to find out more about NumPy.

scipy

NumPy provides a high performance multidimensional degree group and a basic tool for calculating and manipulating these arrays. Built on this basis, SCIPY provides a large number of functions that operate on numpy arrays and can be used in different types of scientific and engineering applications.

The best way to familiarize yourself with scipy is to browse the document. We will highlight some of the useful parts of this course.

Image Operations SCIPY provides some basic functions to handle images. For example, the image is read from the hard disk into the NumPy array, the NumPy array is written to the hard disk in image format, and the image size is reset. Examples are as follows:

From Scipy.misc import Imread, Imsave, imresize

# Read A JPEG image into a numpy array
img = Imread (' ASSETS/CAT.J PG ')
print Img.dtype, img.shape  # prints "uint8 (248, 3)"

# We can tint the image by scaling each of the Color Channels
# by a different scalar constant. The image has shape (248, 3);
# We multiply it by the array [1, 0.95, 0.9] of shape (3,);
# NumPy Broadcasting means that this leaves the red channel unchanged,
# and multiplies the green and blue channels by 0.95 and 0.9
# respectively.
img_tinted = img * [1, 0.95, 0.9]

# Resize The tinted image to be the pixels.
img_tinted = Imresize (img_tinted, ())

# Write The tinted image back to disk
Imsave (' ASSETS/CAT_TINTED.J PG ', img_tinted)

---------------------------------------------------------------------------------------


Left:the original image. Right:the Tintedand resized image.

---------------------------------------------------------------------------------------

MATLAB Files

Functions Scipy.io.loadmat and Scipy.io.savemat allow you to read and write MATLAB files, which you can learn from the official documentation.

Distance between points

SciPy defines a number of useful functions to calculate the distance between points.

Function Scipy.spatial.distance.pdist calculates the distance of a pair in a given set.

Import NumPy as NP
from scipy.spatial.distance import pdist, Squareform

# Create The following array where each RO W is a point in 2D spaces:
# [[0 1]
#  [1 0]
#  [2 0]]
x = Np.array ([[0, 1], [
1,], [0, 2]]) PRI NT x

# Compute The Euclidean distance between all rows of X.
# D[i, J] is the Euclidean distance between X[i,:] and X[j,:],
# and D are the following array:
# [0.          1.41421356  2.23606798]
#  [1.41421356  0.          1.        ]
#  [2.23606798  1.          0.        ]]
d = squareform (Pdist (x, ' Euclidean '))
print D

You can get more details about the function from the official website documentation.

A similar function (scipy.spatial.distance.cdist) calculates the distance between pairs in two sets; you can read them here.

matplotlib

Matplotlib is a drawing library. This part mainly introduces the Matplotlib.pyplot module, which provides a drawing system with MATLAB type.

plotting

The most important function in Matplotlib is plot, which allows us to draw 2D of data. Here's a simple example:

Import NumPy as NP
import Matplotlib.pyplot as Plt

# Compute The x and Y coordinates for points on a sine curve
  x = Np.arange (0, 3 * np.pi, 0.1)
y = np.sin (x)

# Plot the points using matplotlib plt.plot
(x, y)
Plt.sho W ()  # You must call Plt.show () to make graphics appear.

Run the code to get the following graphic.

-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------

With a little extra work, we can draw more than one line at a time, adding titles, legends, and axis labels.

Import NumPy as NP
import Matplotlib.pyplot as Plt

# Compute The x and Y coordinates for points on sine and cosine Curves
x = Np.arange (0, 3 * np.pi, 0.1)
Y_sin = Np.sin (x)
Y_cos = Np.cos (x)

# Plot the points using MATPL Otlib
plt.plot (x, Y_sin)
plt.plot (x, Y_cos)
Plt.xlabel (' x axis label ')
Plt.ylabel (' Y axis label ')
plt.title (' Sine and cosine ')
plt.legend ([' Sine ', ' cosine '])
plt.show ()

-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------

You can read more about plot in the official document.

Subplots

You can draw different things in the same diagram, and you need to use the subplot function. Here is an example:

Import NumPy as NP
import Matplotlib.pyplot as Plt

# Compute The x and Y coordinates for points on sine and cosine Curves
x = Np.arange (0, 3 * np.pi, 0.1)
Y_sin = Np.sin (x)
Y_cos = Np.cos (x)

# Set up a subplot grid that has height 2 and width 1,
# and set the such subplot as active.
Plt.subplot (2, 1, 1)

# make the plot
plt.plot (x, Y_sin)
plt.title (' Sine ')

# Set the second Subplo T as active, and make the second plot.
Plt.subplot (2, 1, 2)
plt.plot (x, Y_cos)
plt.title (' cosine ')

# show the figure.
Plt.show ()

-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------

In the same way, the official document provides more information about subplot.

Images

You can use the Imshow function to show the picture. Examples are as follows:

Import NumPy as NP
from Scipy.misc import imread, imresize
import matplotlib.pyplot as Plt

img = imread (' Asset S/cat.jpg ')
img_tinted = img * [1, 0.95, 0.9]

# show the original image
Plt.subplot (1, 2, 1)
plt.imshow (im g)

# Show the tinted image
Plt.subplot (1, 2, 2)

# A Slight gotcha with imshow are that it might give Strange re Sults
# If presented with data, is not uint8. To work around this, we are
explicitly cast the image to uint8 before displaying it.
Plt.imshow (Np.uint8 (img_tinted))
plt.show ()


-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------




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.