Python numpy, pythonnumpy

Source: Internet
Author: User

Python numpy, pythonnumpy

import numpya=numpy.array([1,2,3,4])b=numpy.array([[1,2,3],[4,5,6],[7,8,9]])print(a.shape)print(b.shape)

Creates a one-dimensional vector and a three-row matrix.

Note: Data must be in the same structure. The shape function is used to specify rows and columns.

 

Valid value:

Import numpyb = numpy. array ([[, 3], [, 6], [, 9]) print (B [:, 1]) # print the second column of the Matrix print (B [:, 0: 2]) # obtain the first and second columns here.

 

Modify the value in the matrix:

Change the values of 5 and 7 to 10.

import numpyb=numpy.array([[1,2,3],[4,5,6],[7,8,9]])b[(b==5)|(b==7)] = 10print(b)

 

Conversion Type:

Convert int type to str type

import numpyb=numpy.array([[1,2,3],[4,5,6],[7,8,9]])c = b.astype(str)print(c)

 

Other operations:

Import numpyb = numpy. array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]) print (B. min () # Calculate the minimum value print (B. max (axis = 1) # calculate the maximum print (B. sum (axis = 0) # sum by Column

 

Import numpy as npa = np. arange (10 ). reshape () print (a) ''' create a matrix: [[0 1 2 3 4] [5 6 7 8 9] ''' print (. ndim) # evaluate the Dimension print (. shape) # print (. dtype. name) # print (. size) # Number of Elements

 

Matrix initialization:

Import numpy as np # matrix initialization method: np. zeros (3, 4) # initialize the 3-row 4-column matrix to 0 (float type by default) np. ones (3, 4), dtype = np. int32) # Three rows, four columns, int type with an initial value of 1

 

Create a matrix:

Import numpy as npnp. arange (10, 30, 5) # from 10 to 30, every 5 # array ([10, 15, 20, 25]) np. random. random (2, 3) ''' is randomly created: two rows and three columns, between-1 and 1. Note: It must be two randomarray ([0.20925672, 0.09790786, 0.00158854], [0.73711854, 0.83033327, 0.22525092]) ''' np. linspace (100, 100) # obtain an average of numbers from 1 to 3 (float type)

 

 

Operation:

Import numpy as npa = np. array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]) print (np. hstack (a, a) print (np. vstack (a, a) print (. t) print (a + a) print (a * a) print (. dot (a) print (np. dot (a, a) print (np. exp (a) print (np. sqrt (a) print (. shape) print (. ravel () ''' is not explained, [1 2 3 1 2 3] [4 5 6 4 5 6] [7 8 9 7 8 9] [[1 2 3] [4 5 6] [7 8 9] [1 2 3] [4 5 6] [7 8 9] [[1 4 7] [2 5 8] [3 6 9] [2 4 6] [8 10 12] [14 16 18] [[1 4 9] [16 25 36] [49 64 81] [[30 36 42] [66 81 96] [102 126 150] [[30 36 42] [66 81 96] [102 126 150] [[2.71828183e + 00 7.38905610e + 00 2.00855366e + 01] [5.45981500e + 01 1.48413159e + 02 4.03428793e + 02] [1.09663316e + 03 2.98095799e + 03 8.10308393e + 03] [[1. 1.41421356 1.73205081] [2. 2.23606798 2.44948974] [2.64575131 2.82842712 3.] (3, 3) [1 2 3 4 5 6 7 8 9] '''

 

Import numpy as npa = np. array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]) print (. argmax (axis = 0) #[2 2 2] maximum column index value print (. argmin (axis = 1) #[0 0 0] minimum index value of the row

 

import numpy as npa=np.arange(0,40,10)print(a)b=np.tile(a,(3,2))c=np.tile(a,(2,3))print(b)print(c)'''[ 0 10 20 30][[ 0 10 20 30  0 10 20 30] [ 0 10 20 30  0 10 20 30] [ 0 10 20 30  0 10 20 30]]  [[ 0 10 20 30  0 10 20 30  0 10 20 30] [ 0 10 20 30  0 10 20 30  0 10 20 30]]'''

 

Sort:

Import numpy as npa = np. array ([[, 6], [, 7], [, 8]) print () ''' [[1 4 6] [2 9 7] [5 3 8] ''' B = np. sort (a, axis = 1) # print (B) by row) ''' [[1 4 6] [2 7 9] [3 5 8] ''' c = np. sort (a, axis = 0) # print (c) by Column) ''' [[1 3 6] [2 4 7] [5 9 8] ''' d = np. argsort (a) # Sort index values print (d) ''' [[0 1 2] [0 2 1] [1 0 2] '''

 

 

Note:

Import numpy as npa = np. array ([[, 3], [, 6], [, 9]) c =. view () print (c is a) # false (c and a point to the memory address is different) # copy a and assign it to c # If c =, then c and a are the same (pointing to the same address) # print (c is a), The truec [100] = print (a) will be printed) ''' [[1 2 3] [4 5 100] [7 8 9] ''' # Here c is modified, then a is modified. # c and a share a group of data, even though their addresses are different. d =. copy () print (d is a) # falsed [100] = # The aprint (a) is not changed here)

 

 

Read txt files:

Import numpy # The first parameter is the path, the second parameter is the separator, and the third parameter is the read type # The last parameter indicates whether to remove the first line a = numpy. genfromtxt ("d:/a.txt", delimiter = ",", dtype = "str", skip_header = 1) print ()

 

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.