2022年1月4日 星期二

NumPy 是什麼

首先,什麼是Numpy?

Numpy 是 Python 的一個重要模組(Python 是一個高階語言,可以透過整合其他低階語言同時擁有效能和高效率的開發),主要用於資料處理上。
Python 處理龐大資料時,其原生 list 效能表現並不理想(但可以動態存異質資料),而 Numpy 具備平行處理的能力,可以將操作動作一次套用在大型陣列上。

此外 Python 其餘重量級的資料科學相關套件(例如:Pandas、SciPy、Scikit-learn 等)都幾乎是奠基在 Numpy 的基礎上。因此學會 Numpy 對於往後學習其他資料科學相關套件打好堅實的基礎。

Numpy 基礎操作

import numpy as np

np.array(): 有矩陣加減乘除的應用

np.zeros( (x,y) ): x*y全零矩陣

np.empty(): 用法與np.zeros一樣,但唯一的差別是NumPy不會初始化陣列內元素的初始值,所以內容將會是不確定的。

np.arange( 起始值, 結束值, 步幅, 資料型別 )

np.linspace( 起始值, 結束值, 起始與結束的區間內要產生幾個元素 )

np.sum:矩陣加總
np.min:矩陣最小值
np.max:矩陣最大值
np.mean:矩陣平均值

np.sqrt:矩陣內所有元素開平方根
np.exp:矩陣內所有元素進行Exponential function(e)運算
np.add:矩陣或陣列相加

  1. Indexing

索引(Indexing)的用途不外乎就是為了要從陣列和矩陣中取值,但除此之外有很多種功能!
可以取出連續區間,還可以間隔取值!

選取連續區間 [a:b]

2. slicing

切片(Slicing)的用途和索引(Indexing)很像!
若能活用便能加快程式撰寫速度!

間隔選取[::c]

以1維陣列來說明x[a:b:c]
a:選取資料的起始索引
b:選取資料的結束索引+1
c:選取資料間隔,以索引值可以被此值整除的元素,不指定表示1

3. 迭代

迭代(Iterating)比較熟悉一點,可以說就像foreach一樣的使用方法

# Iteracted overa = np.arange(10) ** 2for i in a:print(“a**(1/2)=> {0}”.format(np.round(i**(1/2), 0)))

np.reshape(a, b):這是最基本的塑形功能,可以直接將陣列重新塑形成a-by-b或是更高維度的形狀!

np.ravel():此功能會回傳一個將陣列或矩陣經扁平化(flattened)處理後的陣列

ndarray.T:轉置矩陣(transpose)

這功能可以方便運算時矩陣串接的需求!

np.vstack(a, b):將a, b矩陣沿著垂直軸堆疊!
np.hstack(a, b):將a, b矩陣沿著水平軸堆疊!

a = np.arange(1,11).reshape(2,5)
print(“a=>\n{0}”.format(a))
print()# 水平堆疊print(“np.vstack((a,a))=>\n{0}”.format(np.hstack((a,a))))print()# 垂直堆疊print(“np.hstack((a,a))=>\n{0}”.format(np.vstack((a,a))))

#本篇只簡單介紹numpy較常用到的指令 

NumPy is the fundamental package for scientific computing with Python.

It provides:

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities

Testing:

NumPy requires pytest and hypothesis. Tests can then be run after installation with:

python -c 'import numpy; numpy.test()'

Code of Conduct

NumPy is a community-driven open source project developed by a diverse group of contributors. The NumPy leadership has made a strong commitment to creating an open, inclusive, and positive community. Please read the NumPy Code of Conduct for guidance on how to interact with others in a way that makes our community thrive.

Call for Contributions

The NumPy project welcomes your expertise and enthusiasm!

Small improvements or fixes are always appreciated; issues labeled as "good first issue" may be a good starting point. If you are considering larger contributions to the source code, please contact us through the mailing list first.

Writing code isn’t the only way to contribute to NumPy. You can also:

  • review pull requests
  • triage issues
  • develop tutorials, presentations, and other educational materials
  • maintain and improve our website
  • develop graphic design for our brand assets and promotional materials
  • translate website content
  • help with outreach and onboard new contributors
  • write grant proposals and help with other fundraising efforts

If you’re unsure where to start or how your skills fit in, reach out! You can ask on the mailing list or here, on GitHub, by opening a new issue or leaving a comment on a relevant issue that is already open.

Our preferred channels of communication are all public, but if you’d like to speak to us in private first, contact our community coordinators at numpy-team@googlegroups.com or on Slack (write numpy-team@googlegroups.com for an invitation).

We also have a biweekly community call, details of which are announced on the mailing list. You are very welcome to join.

If you are new to contributing to open source, this guide helps explain why, what, and how to successfully get involved.

沒有留言:

張貼留言

Python 標準函式庫 (Standard Library

Python 標準函式庫 (Standard Library) — Python 3.12.0a3 說明文件 ...