Table of Contents
1. Smaller memory usage
64 8 * len(lst) len(lst) * 28 bytes
96 len(a) * 8 bytes
2. Faster, built-in calculation method
Home Backend Development Python Tutorial Why must Python big data use Numpy Array?

Why must Python big data use Numpy Array?

May 04, 2023 pm 03:13 PM
python Big Data numpy arra

Python大数据为啥一定要用Numpy Array?

Numpy is a core module of Python scientific computing. It provides very efficient array objects, as well as tools for working with these array objects. A Numpy array consists of many values, all of the same type.

Python's core library provides List lists. Lists are one of the most common Python data types, and they can be resized and contain elements of different types, which is very convenient.

So what is the difference between List and Numpy Array? Why do we need to use Numpy Array when processing big data? The answer is performance.

Numpy data structures perform better in the following aspects:

1. Memory size—Numpy data structures take up less memory.

2. Performance - The bottom layer of Numpy is implemented in C language, which is faster than lists.

3. Operation methods - built-in optimized algebraic operations and other methods.

The following explains the advantages of Numpy arrays over Lists in big data processing.

1. Smaller memory usage

If you use Numpy arrays instead of Lists appropriately, you can reduce your memory usage by 20 times.

For Python's native List, since every time a new object is added, 8 bytes are needed to reference the new object, and the new object itself occupies 28 bytes (taking integers as an example). So the size of the list can be calculated with the following formula:

64 8 * len(lst) len(lst) * 28 bytes

Python大数据为啥一定要用Numpy Array?

while using Numpy , which can reduce a lot of space occupied. For example, a Numpy integer Array of length n requires:

96 len(a) * 8 bytes

Python大数据为啥一定要用Numpy Array?

It can be seen that the larger the array, the more money you save The more memory space there is. Assuming your array has 1 billion elements, then the difference in memory usage will be on the GB level.

2. Faster, built-in calculation method

Run the following script, which also generates two arrays of a certain dimension and adds them together. You can see the native List and Numpy Array. performance gap.

import time
import numpy as np
size_of_vec = 1000
def pure_python_version():
t1 = time.time()
X = range(size_of_vec)
Y = range(size_of_vec)
Z = [X[i] + Y[i] for i in range(len(X)) ]
return time.time() - t1
def numpy_version():
t1 = time.time()
X = np.arange(size_of_vec)
Y = np.arange(size_of_vec)
Z = X + Y
return time.time() - t1
t1 = pure_python_version()
t2 = numpy_version()
print(t1, t2)
print("Numpy is in this example " + str(t1/t2) + " faster!")
Copy after login

The results are as follows:

0.00048732757568359375 0.0002491474151611328
Numpy is in this example 1.955980861244019 faster!
Copy after login

As you can see, Numpy is 1.95 times faster than native arrays.

If you are careful, you can also find that Numpy array can directly perform addition operations. Native arrays cannot do this. This is the advantage of Numpy's operation method.

We will do several more repeated experiments to prove that this performance advantage is durable.

import numpy as np
from timeit import Timer
size_of_vec = 1000
X_list = range(size_of_vec)
Y_list = range(size_of_vec)
X = np.arange(size_of_vec)
Y = np.arange(size_of_vec)
def pure_python_version():
Z = [X_list[i] + Y_list[i] for i in range(len(X_list)) ]
def numpy_version():
Z = X + Y
timer_obj1 = Timer("pure_python_version()",
 "from __main__ import pure_python_version")
timer_obj2 = Timer("numpy_version()",
 "from __main__ import numpy_version")
print(timer_obj1.timeit(10))
print(timer_obj2.timeit(10)) # Runs Faster!
print(timer_obj1.repeat(repeat=3, number=10))
print(timer_obj2.repeat(repeat=3, number=10)) # repeat to prove it!
Copy after login

The results are as follows:

0.0029753120616078377
0.00014940369874238968
[0.002683573868125677, 0.002754641231149435, 0.002803879790008068]
[6.536301225423813e-05, 2.9387418180704117e-05, 2.9171351343393326e-05]
Copy after login

It can be seen that the second output time is always much smaller, which proves that this performance advantage is persistent.

So, if you are doing some big data research, such as financial data and stock data, using Numpy can save you a lot of memory space and have more powerful performance.

References:​​https://www.php.cn/link/5cce25ff8c3ce169488fe6c6f1ad3c97​

Our article ends here, if you like Please continue to follow us for today’s Python practical tutorial.

The above is the detailed content of Why must Python big data use Numpy Array?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download DeepSeek Xiaomi? Search for "DeepSeek" in the Xiaomi App Store. If it is not found, continue to step 2. Identify your needs (search files, data analysis), and find the corresponding tools (such as file managers, data analysis software) that include DeepSeek functions.

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

The key to using DeepSeek effectively is to ask questions clearly: express the questions directly and specifically. Provide specific details and background information. For complex inquiries, multiple angles and refute opinions are included. Focus on specific aspects, such as performance bottlenecks in code. Keep a critical thinking about the answers you get and make judgments based on your expertise.

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

Just use the search function that comes with DeepSeek. Its powerful semantic analysis algorithm can accurately understand the search intention and provide relevant information. However, for searches that are unpopular, latest information or problems that need to be considered, it is necessary to adjust keywords or use more specific descriptions, combine them with other real-time information sources, and understand that DeepSeek is just a tool that requires active, clear and refined search strategies.

How to program deepseek How to program deepseek Feb 19, 2025 pm 05:36 PM

DeepSeek is not a programming language, but a deep search concept. Implementing DeepSeek requires selection based on existing languages. For different application scenarios, it is necessary to choose the appropriate language and algorithms, and combine machine learning technology. Code quality, maintainability, and testing are crucial. Only by choosing the right programming language, algorithms and tools according to your needs and writing high-quality code can DeepSeek be successfully implemented.

How to access DeepSeekapi - DeepSeekapi access call tutorial How to access DeepSeekapi - DeepSeekapi access call tutorial Mar 12, 2025 pm 12:24 PM

Detailed explanation of DeepSeekAPI access and call: Quick Start Guide This article will guide you in detail how to access and call DeepSeekAPI, helping you easily use powerful AI models. Step 1: Get the API key to access the DeepSeek official website and click on the "Open Platform" in the upper right corner. You will get a certain number of free tokens (used to measure API usage). In the menu on the left, click "APIKeys" and then click "Create APIkey". Name your APIkey (for example, "test") and copy the generated key right away. Be sure to save this key properly, as it will only be displayed once

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

Quantitative currency trading software Quantitative currency trading software Mar 19, 2025 pm 04:06 PM

This article explores the quantitative trading functions of the three major exchanges, Binance, OKX and Gate.io, aiming to help quantitative traders choose the right platform. The article first introduces the concepts, advantages and challenges of quantitative trading, and explains the functions that excellent quantitative trading software should have, such as API support, data sources, backtesting tools and risk control functions. Subsequently, the quantitative trading functions of the three exchanges were compared and analyzed in detail, pointing out their advantages and disadvantages respectively, and finally giving platform selection suggestions for quantitative traders of different levels of experience, and emphasizing the importance of risk assessment and strategic backtesting. Whether you are a novice or an experienced quantitative trader, this article will provide you with valuable reference

deepseek how to deploy r1 model locally - deepseek locally install r1 model tutorial deepseek how to deploy r1 model locally - deepseek locally install r1 model tutorial Mar 12, 2025 pm 12:15 PM

DeepSeekR1 model local deployment guide: Unlocking data analysis and prediction potential DeepSeek is a powerful data analysis and prediction tool, and its R1 model can provide efficient and accurate model support for various application scenarios. This guide will provide you with detailed instructions on how to deploy the DeepSeekR1 model in your on-premises environment to help you get started quickly and take advantage of its power. System requirements for the local deployment step preparation stage of the DeepSeekR1 model: Please make sure that your local machine meets the minimum system configuration requirements of DeepSeek (for specific requirements, please refer to the official DeepSeek document). Software installation: Download and install the installation package provided by DeepSeek and follow the installation guide

See all articles