Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use array of Numpy

Shulou Source: shulou.com Published: 2022-06-01 12:04:47 09月29日 Update

This article mainly introduces "how to use array of Numpy". In daily operation, I believe many people have doubts about how to use array of Numpy. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use array of Numpy". Next, please follow the editor to study!

Array ([:]) > > import numpy as np > x=np.array ([1) > print (x [1:5]) # prints the array with index as 1: 5, the range is left closed and right open [2 345] > print (x [3:]) # the array after printing index=3, including index=3 [4 56 7 8 9 10 11] > print (x [: 9]) # the array before printing index=9 Does not include index=9 [1 2 3 4 5 6 7 8 9 9] > print (x [1: 2]) # prints the array from index=1 to the penultimate index [2 3 4 5 6 7 8 9 9 10] > > print (x [- 9 print 2]) # prints the array between the penultimate index and the penultimate index, opens left and closes [4 5 6 7 8 9 10] array ([:]) > > print (x [1: 3]) # with index=1 as the starting position Interval 3 [25 8 11] > > print (x [:: 3]) # starts from index=0 by default, interval 3 [1 47 10] > > print (x [3:]) # and [3:] are the same [4 5 6 7 8 9 10 11 12] > > print (x [:: 1]) # reverse printing data, starting from the last index, the interval is 1 [12 11 10 9 8 7 6 5 4 2 1] > print (x [::-3]) # reverse printing data Starting from the last index, the interval is 3 [12 9 6 3] > > print (x [7: 2 index=2 1]) # reverse printing data from index=2 (not included) to index=7 [8 7 6 5 4]

Also encountered in this aspect of the problem, do not understand, simply try to make it clear, should be [:] and [:] there are still a lot of interesting places.

Add: detailed explanation of Numpy.array (), discrimination between np.array and np.asarray, difference between np.array and np.ndarry

Record the detailed usage of numpy.array () and how it differs from np.asarray () and np.ndarray ().

1. Detailed explanation of Numpy.array ()

In a nutshell, the function of this function is to generate an array.

1.1 functional form numpy.array (object, dtype=None, copy=True, order='K', subok=False, ndmin=0) 1.2Parameters are explained in detail.

Object: a required parameter of type array_like, which can be of four types: an array, any object that exposes an array interface, an object that the _ _ array__ method returns an array, or any (nested) sequence. The purpose of np.array () is to convert object to an array according to certain requirements.

Dtype: an optional parameter that represents the type of array element. If not given, the type is determined to be the minimum type required to hold the objects in the sequence. Note: This argument can only be used to 'upcast' the array. For downcasting, use the .astype (t) method.

Copy: optional parameter, type is Bool value. If true (the default), the object is copied. Otherwise, copies will be returned only in the following three cases: (1). If _ array__ returns a copy; (2). If obj is a nested sequence; (3). If a copy is needed to satisfy any of the other requirements (dtype, order, etc.)

Order: {'Kwon,' Agar, 'Che,' F'}, optional. Specifies the memory layout of the array. I haven't come across a specific usage of this parameter so far, which means I can't, so it's omitted here.

Subok: optional parameter, type is Bool value. If True, the subclass is passed, otherwise the returned array is forced to be the base class array (the default). Or, True: uses the internal data type of object, and False: uses the data type of the object array.

Ndmin: optional parameter, type int. Specifies the minimum dimension that the resulting array should have.

Return object

Out: output ndarray, array objects that meet the specified requirements.

1.3 specific usage

Simple exampl

Import numpy as np arr01 = np.array print (arr01) # [12 3] print (type (arr01)) # print (arr01.dtype) # int32 # Upcastingarr02 = np.array ([1. 2. 2.]) print (arr02) # [1. 2. 3.] print (arr02.dtype) # float64 # More than one dimension:arr03 = np.array ([1 [1, 2], [3]]) print (arr03) "" [[12] [3]] "

Example of using dtype parameter

Import numpy as np # specifies that the array element type is the plural type DYX= np.array ([1mem2page3], dtype= complex) print (DYX) # [1.room0.j 2.room0.j 3.room0.j] print (DYX.dtype) # complex128 # data type consisting of multiple elements: HXH = np.array ([(1mag2), (3pyr4)], dtype= [('axiaojiao')

Tags: Array type parameter object sequence that is data method function subclass example learning influence primitive between two element memory matrix problem Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Huawei Shulou Tech Info OPPO Reno