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 numpy Library in python

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

Share

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

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

What is 1.Numpy?

NumPy (short for Numerical Python) is an open source Python scientific computing library. With NumPy, you can naturally use arrays and matrices. NumPy contains many practical mathematical functions, including linear algebraic operation, Fourier transform and random number generation.

The predecessor of this library is a library for array operations, which was developed in 1995. After a long period of development, it has basically become the basic package for most Python scientific computing, including all the deep learning frameworks that provide Python interfaces.

two。 Why use Numpy? A) convenience:

For the same numerical calculation task, using NumPy is much more convenient than writing Python code directly. This is because NumPy can manipulate arrays and matrices directly, omitting a lot of looping statements, and its numerous mathematical functions make writing code much easier.

B) performance:

The storage efficiency and input-output performance of arrays in NumPy are much better than the equivalent basic data structures in Python (such as nested list containers). The performance it can improve is proportional to the number of elements in the array. For large array operations, using NumPy does have an advantage. For TB-level large files, NumPy uses memory-mapped files to achieve optimal data read and write performance.

C) efficient:

Most of the code in NumPy is written in C, which makes NumPy much more efficient than pure Python code.

Of course, NumPy also has its disadvantages, because NumPy uses memory-mapped files to achieve optimal data read and write performance, and the size of memory limits its processing of large files at the TB level; in addition, the versatility of the NumPy array is not as general as the list container provided by Python. Therefore, in areas other than scientific computing, the advantages of NumPy are not so obvious.

Installation of 3.Numpy:

Official website installation: http://www.numpy.org/.

Pip installation: pip install numpy.

LFD installation: for windows users

Anaconda installation (recommended): Anaconda integrates a lot of third-party libraries about python scientific computing, mainly for easy installation.

4.numpy fundamentals:

The main object of NumPy is a multi-dimensional array of the same element. This is the one where all elements are of one type. In NumPy, the dimensions is called axes, and the number of axes is called rank. The array class of NumPy is called ndarray (matrix is also called array. It is often called an array.

Common ndarray object properties are:

Ndarray.ndim (the number of axes in an array, which is called rank)

Ndarray.shape (the dimension of the array. This is an integer tuple that indicates the size of the array on each dimension. For example, for a matrix with n rows and m columns, its shape attribute will be (2p3), and the length of this tuple is obviously rank, that is, dimension or ndim attribute).

Ndarray.size (the total number of array elements, equal to the product of tuple elements in the shape attribute)

Ndarray.dtype (an object used to describe the type of elements in an array, you can use the standard Python type by creating or specifying dtype. In addition, NumPy provides its own data types.

Data type of Numpy: import numpy as npa = np.dtype (np.int_) # np.int64, np.float32... Built-in signature of print (a) Numpy:

Int8, int16, int32,int64 can be replaced by the strings'i1','i2','i4','i8', and so on.

Import numpy as npa = np.dtype ('i8') #' f8','i4 characters','i4 characters' c16 characters # string),'> i4 '... Print (a)

You can indicate the byte order of the data type in memory, and'> 'means it is stored as a big end,'

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