NumPy Fundamentals

在选择高低级语言时需权衡开发速度和程序运行效率。Python的NumPy和pandas库可兼顾两者,其高效的秘诀是向量化,利用SIMD特性加速数据处理。还介绍了NumPy库的导入、数组转换、操作符、函数与方法区别等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When choosing between a high and low-level language, you have to make a trade-off between being able to work and quickly, and having programs that run quickly and efficiently. Luckily, there are two Python libraries that were created to give us the best of both-worlds: NumPy and pandas. Together, pandas and NumPy provide a powerful toolset for working with data in Python. They allow us to write code quickly without sacrificing performance. But how do they do this? What is it that makes these libraries faster than raw Python? The answer is vectorization.

1. why?

Vectorization takes advantage of a processor feature called Single Instruction Multiple Data (SIMD) to process data faster. Most modern computer processors support SIMD. SIMD allows a processor to perform the same operation, on multiple data points, in a single processor cycle.

2. NumPy

  1. To start working with NumPy, we’ll first need to start by importing the NumPy library into our Python environment. For this, we use a simple import statement:
    在这里插入图片描述We used the as syntax in our import statement. This allows us to access the NumPy library using another name. When working with NumPy, the convention is to import the library as np for brevity.
  2. To convert our list of lists into a NumPy n-dimensional array, or ndarray, you can think of it as NumPy’s version of a list of lists format. To convert from the list type to ndarray, we use the numpy.array() constructor. ndarray.shape可以知道该数组各个dimensional信息
    在这里插入图片描述
  3. With a list of lists, we use two separate pairs of square brackets back-to-back. With a NumPy ndarray, we use a single pair of brackets with comma separated row and column locations.在这里插入图片描述在这里插入图片描述
  4. operator:在这里插入图片描述Or you can use the arithmetic functions,like devide()
  5. the differences between Function and Method:
    Functions act as stand alone segments of code that usually take an input, perform some processing, and return some output. When we’re working with Python lists, we can use the len() function to calculate the length of a list, but if we’re working with Python strings, we can also use len(). In this case, it calculates the numbers of characters (or length) of the string.In contrast, methods are special functions that belong to a specific type of object. Python lists have a list.append() method that we can use to add an item to the end of a list. If we try to use that method on a string, we will get an error在这里插入图片描述
  6. add rows and columns to an ndarray: numpy.concatenate() function. This function accepts:

· A list of ndarrays as the first, unnamed parameter.
· An integer for the axis parameter, where 0 will add rows and 1 will add columns.
numpy.expand_dims() function. : to provide the same type在这里插入图片描述
7. numpy.argsort() function: It returns the indices which would sort an array. 返回索引在这里插入图片描述
8.oolean Indexing with Numpy : numpy.genfromtxt()—— reads a text file into a NumPy ndarray. While it has over 20 parameters, for most cases you need only two:在这里插入图片描述在这里插入图片描述
skip_header:跳过第n行,此处跳过第一行
9 . We can use the ndarray.dtype attribute to see the internal datatype that has been used.在这里插入图片描述
10.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值