Python Scientific Computing Library Numpy__python

Source: Internet
Author: User
1. What is NumPy

Machine learning is commonly used in a scientific computing library called NumPy:

NumPy is an array of matrices. is a multidimensional array object, called Ndarray.

Such as:

Import NumPy
a = Numpy.array ([[[[1,2,3,4],
                 [5,6,7,8],
                [1,3,5,7]])
print (a)
print (A.ndim)
Print (a.shape) print (a.size) print (a.dtype) print (
a.itemsize)
It is important to note that all elements in the same NumPy array must be the same.

2, numpy attribute introduction

The dimensions of the NumPy array are called rank (rank), the rank of the one-dimensional array is 1, and the rank of the two-dimensional array is 2. Each linear array is called an axis (axes), and the rank is the number of axes. The Ndarray mentioned above is a two-dimensional array with a rank of 2.
Properties of objects commonly used in NumPy:
1, Ndarray.ndim: The dimension of the array, equal to the rank;
2, Ndarray.shape: The dimensions of an array, an integer tuple that identifies the size of an array on each dimension, the shape of the Ndarray above is (3, 4), and the length of the tuple is the dimension of the array, that is, the Ndim;
3, Ndarray.size: The total number of elements of the array, equal to the product of the elements in shape, the size of the above ndarray is 12;
4, Ndarray.dtype: The element type in the array;
5, Ndarray.itemsize: The byte size of each element in the array, the above Ndarray element is the int64 type, the int64 occupies 64 bits, each byte length is 8, therefore occupies 64/8 is equal to 8 bytes. 3, the use of NumPy

1, create a matrix of elements of 0--14, (3,5) dimension

Vector = Numpy.arange (
vector)
Vector2 = Vector.reshape (3,5) # vector converted to matrix
print (VECTOR2)

# The result of the output is
[0 1 2 3 4 5 6 7 8 9 A  ]
[[0  1  2  3  4]
 [5  6  7  8  9]
 [10 11 12 13 14]]

2, specify a matrix, the elements are 0

Vector3 = Numpy.zeros (3,4))
print (Vector3)

# The result of output is [
[0].  0.  0.  0.]
 [0.  0.  0.  0.]
 [0.  0.  0.  0.]]

3,

A = Numpy.array ([[1,2,3,4],
                 [5,6,7,8], [
                1,3,5,7]])
print (a)
print (a[:,1])

# The result of the output is
[[1 2 3 4]
 [5 6 7 8]
 [1 3 5 7]]
[2 6 3] #这个结果意思是取任意行中第1列的元素

4,

The result of print (Numpy.arange (10,30,5))

# output is:

# If you need to know the meaning of this method, you can print the Help function query, such as:
print ( Numpy.arange))
5,
# Vector3 = Numpy.ones ((2,3,4), Dtype=numpy.int32)
# Vector3 = Numpy.random.random ((2,3)) # construct an array of 2 rows 3 columns using random fetch of random module
Vector3 = numpy.linspace (0, 9,) # Linspace from 0 to 9 an average of 10 numbers constructs an array containing 0 and 9
print (Vector3)

6, the calculation of Ndarray

# ndarray calculation
a = Numpy.array ([20,30,40,50])
B = Numpy.arange (4)
print (a-b)
print (A-1) # The

output is:
[PDF]
[19 29 39 49]

7, The matrix of the * operation and. dot Operation

A = Numpy.array ([[[1,2],
                 [2,3]])

B = Numpy.array ([[[1,3],
                 [2,4]])

# Print (a*b)
print (A.dot (b) ))
# Print (Numpy.dot (a,b))
8, The Matrix power operation and the root operation

A = Numpy.arange (3) print (a) print (Numpy.exp (a))
print (
numpy.square (a))

#输出结果是
[0 1 2]
[1.          2.71828183  7.3890561]
[0 1 4]

9, Numpy.floor (a) and Numpy.random.random ((3,4))

A = 10*numpy.random.random ((3,4))
print (a)
a = Numpy.floor (a)
print (a) print (
a.revel ()) # Matrix steering Amount
A.shape = (6,2)
print (A.T) # matrix transpose

# output result:
[[5.40834281  4.269162    7.92982643  4.279437  ]
 [7.89479131  4.06550228  1.76543625  1.47728591]
 [9.26177306  8.97762938  6.38632298  6.02355195]]

[5.  4.  7.  4.]
 [7.  4.  1.  1.]
 [9.  8.  6.  6.]]

[6.  6.9.  3.0.  9.2.  2.7.  7.0.  6.]

[[6.  4.9.  5.2.  7.]
[5.  1.0.  8.6. 1.]]

10. Matrix Stitching

A = Numpy.floor (10*numpy.random.random (2,2))
B = Numpy.floor (10*numpy.random.random ((2,2)))
print (a)
print (b)
print (Numpy.hstack (a,b)) #横着拼
# Print (Numpy.vstack (a,b)) #竖着拼

# Output Result:
[6.  3.]
 [8.  4.]]
[[6.  8.]
 [2.  2.]]
[[6.  3.  6.  8.]
 [8.  4.  2.  2.]]

11, The Matrix cutting

A = Numpy.floor (10*numpy.random.random (4,6)) print (
a) print (
numpy.hsplit (a,3))
print (Numpy.vsplit ( a,2))

# Results of output
[[9].  6.  3.  3.  5.  3.]
 [3.  4.  4.  7.  8.  7.]
 [9.  9.  0.  1.  0.  1.]
 [3.  1.  9.  1.  5.  6.]]
[Array ([[9.,  6.],
       [3.,  4.],
       [9.,  9.],
       [3.,  1.]], Array ([[3.,  3.],
       [4.,  7.],
       [0.,  1.],
       [9.,  1.]]), Array ([[5.,  3.],
       [8  ., 7.],
       [0.,  1.],
       [5.,  6.]]
] [Array ([9.,  6.,  3.,  3.,  5.,  3.],
       [3  ., 4., 4., 7., 8, 7.]]), Array ([[9.,  9.,  0.,  1.,  0.,  1.],
       [3  ., 1., 9., 1., 5.  6.]])]

12, the matrix copy operation

Define a Ndarray object A,

If B = A, then A and B are the same, the value of a is the same as B, and B changes the value of a.

If C = A.view (), then A and B point to the same address, a is the same as C, the value of C changes, the value of a will also change;

If d = a.copy (), then A and d are not the same, and a is not associated with the value of D, the value of D is changed, and the value of a is unchanged.

13. Find the row of the largest element in each column in the matrix and get the maximum value:

A = Numpy.sin (numpy.arange. Reshape (5,4))
print (a)
Maxindex = A.argmax (Axis = 0)
print (Maxindex)
Maxdata = A[maxindex, Range (a.shape[1])
print (maxdata)

# The result of the output is:
[[0.          0.84147098  0.90929743  0.14112001]
 [ -0.7568025  -0.95892427-0.2794155   0.6569866]
 [ 0.98935825  0.41211849-0.54402111-0.99999021]
 [ -0.53657292  0.42016704  0.99060736  0.65028784]
 [ -0.28790332-0.96139749-0.75098725  0.14987721]]
[2 0 3 1]
[0.98935825  0.84147098  0.99060736  0.6569866]






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.