The numpy of Python

Source: Internet
Author: User

1.查询矩阵的大小:.shape
import numpy as npa = np.array([1, 2, 3, 4])b = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])print (a.shape)print (‘---‘)print (b.shape)# 输出 (4,)---(3, 4)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

(4,) shape has an element that is a one-dimensional array with 4 elements in the array
(3, 4) shape has two elements that are two-dimensional arrays, with arrays of 3 rows and 4 columns

1.1 Change the length of each axis of the array by modifying the Shape property of the array to keep the number of array elements constant. The following example changes the shape of array b to (4, 3), from (3, 4) to (4, 3) not to transpose the arrays, but only changes the size of each axis, and the position of the array elements in memory does not change:

4, 3print (b)# 输出 [[ 1  2  3] [ 4  4  5] [ 6  7  7] [ 8  9 10]]
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

1.2. When the element of an axis is-1, the length of the axis is automatically calculated based on the number of elements in the array, and the following program changes the shape of array B to (2, 6):

2, -1print (b)# 输出 [[ 1  2  3  4  4  5] [ 6  7  7  8  9 10]]
    • 1
    • 2
    • 3
    • 4

2. Using the reshape method of the array, you can create a new array that changes the dimensions, and the shape of the original array remains the same :

a = np.array((1, 2, 3, 4))b = a.reshape((2, 2))b# 输出 array([[1, 2], [3, 4]])

3. Create an array:. Array
You first need to create an array to perform other operations on it by creating an array of sequence objects that pass Python to the array function, and if you are passing multiple nested sequences, you will create a multidimensional array (such as C):

Import NumPy as NPA = Np.array ([1,2,3,4]) B = Np.array ((5, 6, 7,  8) c = Np.array ([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, ten]]) print (A ) print ( '---') print (b) print ( '---') print (c) # output [1 2 3 4]---[5 6 7 8]---[[1 2 3 4] [4 5 6 7] [7 8 9]]   /span>                
    • If you import numpy with a import numpy command, the form used when creating the array a = numpy.array([1, 2, 3, 4])
    • If you import numpy with a import numpy as np command, use thea = np.array([1, 2, 3, 4])

http://blog.csdn.net/lwplwf/article/details/55506896
http://blog.csdn.net/sinat_34474705/article/details/74458605

The numpy of Python

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.