Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python numpy

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use Python numpy", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "how to use Python numpy" article.

I. Preface

Three major pieces of machine learning: numpy, pandas, matplotlib

Numpy (Numerical Python) is an open source Python scientific computing library that is used to quickly process arrays of any dimension.

Numpy supports common array and matrix operations. For the same numerical calculation task, using Numpy is much simpler than using Python directly.

Numpy uses the ndarray object to handle multidimensional arrays, which is a fast and flexible big data container.

NumPy provides an N-dimensional array type ndarray

Import numpy as npscore = np.array ([[80, 89, 86, 67, 79], [78, 97, 89, 67, 81], [90, 94, 78, 67, 74], [91, 91, 90, 67, 69], [76, 87, 75, 67, 86], [70, 79, 84, 67, 84], [94, 92, 93, 67, 64], [86, 85, 83, 67, 80])

Score

Array ([80, 89, 86, 67, 79]

[78, 97, 89, 67, 81]

[90, 94, 78, 67, 74]

[91, 91, 90, 67, 69]

[76, 87, 75, 67, 86]

[70, 79, 84, 67, 84]

[94, 92, 93, 67, 64]

[86, 85, 83, 67, 80])

Numpy is specially designed for the operation and operation of ndarray, so the storage efficiency and input and output performance of arrays are much better than those of nested lists in Python. The larger the array, the more obvious the advantages of Numpy.

When ndarray stores data, the data and its address are contiguous, which makes it faster to manipulate array elements in batches.

List-split storage with diversified storage content

Ndarray-Integrated storage, storage type must be the same

Ndarray supports parallel operations (vectorization operations)

The bottom layer of ndarray is written in C language, which is more efficient and releases GIL

2. Basic operation # generate arrays of 0 and 1 ones = np.ones ([4jing8]) ones

Array ([[1.1,1.1.1.1.1.1.1.1.1.]

[1., 1., 1., 1.]

[1., 1., 1., 1.]

[1, 1, 1, 1, 1.])

# generate equally spaced array np.linspace (0,100,11)

Array ([0, 10, 20, 30, 40, 50, 60, 70, 80, 90.

100.])

# create an isometric array-specify the step size np.arange (10,50,2)

Array ([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48])

Third, positive Pacific distribution

The rand function generates data between [0Power1) based on a given dimension, including 0, but not 1.

Np.random.rand (4Jing 2)

Array ([0.02173903, 0.44376568]

[0.25309942, 0.85259262]

[0.56465709, 0.95135013]

[0.14145746, 0.55389458]])

The randn function returns a sample or group of samples with standard normal distribution.

Np.random.randn (2pr 4)

Array ([0.27795239,-2.57882503, 0.3817649, 1.42367345]

[- 1.16724625,-0.22408299, 0.63006614,-0.41714538])

Randint returns a random integer with a range of [low,high), including low and excluding high

Np.random.randint (1prime5) # returns a random integer of [1p5) time

four

Generate uniformly distributed random numbers, for example 1: generate normal distribution data with an average of 1.75 and a standard deviation of 1, 100000000

X1 = np.random.normal (1.75,1, 100000000)

Array ([2.90646763, 1.46737886, 2.21799024, … , 1.56047411, 1.87969135, 0.9028096])

Uniform distribution

# generate a uniformly distributed random number x2 = np.random.uniform (- 1, 1, 100000000)

Array ([0.22411206, 0.31414671, 0.85655613, … ,-0.92972446, 0.95985223, 0.23197723])

4. Indexing and slicing of the array # 3D A1 = np.array ([1dje 2dag3], [4mae5pas 6], [12pr 3d34], [5pr 6pr 7]) # returns the result array ([1pr 2pr 3], [4pr 5pr 6]], [[12pr 3pr 34], [5pr 6pr 7]) # index and slice A1 [0,0] 1] # output: 2 5. Shape modification stock_change.reshape ([5,4]) # 5*4stock_change.reshape ([- 1Magol 10]) # 2mm 10 -1: by transposing stock_ change.T. shape 7, type conversion arr = np.array ([1, 2, 3], [4, 5, 6]], [[12, 3, 34], [5, 6, 7]]) arr.tostring () 8, array deduplicated temp = np.array ([[1, 2, 3, 4], [3, 4, 5, 6]) > > np.unique (temp) array ([1, 2, 3]) 4, 5, 6])

Logical operation

Directly judge if the value is greater than or less than: test_score > 60

Once appropriate, you can assign the value directly: test_ score [test _ score > 60] = 1

General judgment function

Np.all () np.all (score [0:2,:] > 60)

Np.any () np.any (score [0:2,:] > 80)

Statistical operation

Np.max ()

Np.min ()

Np.median ()

Np.mean ()

Np.std ()

Np.var ()

Np.argmax (axis=)-the subscript for the largest element

Np.argmin (axis=)-the subscript for the smallest element

IX. Broadcasting mechanism

Array operation, meet the broadcast mechanism, on OK

1. Equal dimensions

2.shape (where the corresponding place is 1, which is also possible)

Arr1 = np.array ([[0], [1], [2], [3]]) arr1.shape# (4,1) arr2 = np.array ([1je 2jue 3]) arr2.shape# (3,) arr1+arr2# results are: array ([[1je 2jue 3], [2pm 3,4], [3,4,5], [4,5,6]]) these are the contents of the article "how to use Python numpy". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report