numpy --- การดำเนินการอาร์เรย์ที่เข้ากันได้กับ numpy¶
โมดูลย่อย numpy ของ ulab มอบ API ที่เข้ากันได้กับ numpy โดยสร้างขึ้นรอบ ๆ ชนิดอาร์เรย์ n มิติ ndarray ซึ่งนำชุดย่อยที่คัดสรรแล้วของ CPython numpy มาใช้งาน ได้แก่ การสร้างอาร์เรย์ คณิตศาสตร์เชิงองค์ประกอบ การลดค่าและสถิติ พีชคณิตเชิงเส้น FFT การสร้างตัวเลขสุ่ม การฟิตพหุนาม และ I/O พื้นฐาน
โมดูลย่อยนี้มักถูกนำเข้าด้วยชื่อ np
from ulab import numpy as np
a = np.array([1, 2, 3, 4], dtype=np.float)
b = np.linspace(0, 1, num=5)
c = np.dot(a.reshape((2, 2)), a.reshape((2, 2)))
Each dtype argument is one of the integer constants exposed at module
level: numpy.bool, numpy.uint8, numpy.int8, numpy.uint16,
numpy.int16, and numpy.float (the default). The result type ndarray refers to
numpy.ndarray.
โมดูลย่อย¶
class ndarray --- อาร์เรย์ n มิติ¶
ndarray คือคอนเทนเนอร์ n มิติที่รู้จัก dtype ซึ่งเป็นแกนหลักของ numpy / ulab ข้อมูลถูกเก็บในบล็อกต่อเนื่องซึ่งการตีความถูกอธิบายโดยส่วนหัวขนาดเล็ก (dtype, shape, strides, ndim, itemsize) การดำเนินการหลายอย่าง เช่น reshape, transpose, การแบ่งส่วน จะปรับแค่ส่วนหัวจึงมีต้นทุนต่ำ ส่วนเมธอดที่จัดสรรหน่วยความจำใหม่ (copy, flatten, เลขคณิตส่วนใหญ่) จะคืนอาร์เรย์หนาแน่นใหม่
ชนิดเดียวกันนี้เข้าถึงได้ในชื่อ ulab.ndarray, numpy.ndarray และ (ในหน้านี้) เพียงแค่ ndarray
- class numpy.ndarray(values: ndarray | bytes | list | tuple, *, dtype: int = numpy.float)¶
สร้าง
ndarrayใหม่- พารามิเตอร์:
values -- ข้อมูลต้นทาง อาจเป็น
ndarrayอื่น (ซึ่งจะถูก deep copy พร้อมแปลงชนิดหากdtypeต่างกัน) หรือ iterable ใด ๆ ของ MicroPython อาร์เรย์ซ้อนกันหลายมิติจะสร้างอาร์เรย์หลายมิติ โดย iterable ชั้นในทุกตัวต้องมีความยาวเท่ากัน ไม่เช่นนั้นจะเกิดValueErrordtype -- Element type for the new array. One of the type-code integers exposed by
numpy(numpy.bool,numpy.uint8,numpy.int8,numpy.uint16,numpy.int16, andnumpy.float), or adtypeinstance. Defaults tonumpy.float.
ฟังก์ชันโรงงาน
numpy.arrayคือวิธีทั่วไปในการสร้างndarrayซึ่งจะส่งต่อไปยัง constructor นี้- byteswap(*, inplace: bool = False) ndarray¶
Swap the byte order of every element. For
uint16,int16, andfloatarrays this reverses the per-element byte order, which is useful when consuming data from peripherals whose endianness does not match the microcontroller's. For single-byte dtypes (bool,uint8,int8) this is a no-op that returns a view or copy.หาก
inplaceเป็นFalse(ค่าเริ่มต้น) จะคืนndarrayใหม่และต้นฉบับจะไม่ถูกแตะต้อง หากinplaceเป็นTrueไบต์ของselfจะถูกสลับในที่และคืน view ของself
- copy() ndarray¶
คืนสำเนา deep copy แบบหนาแน่นใหม่ของอาร์เรย์ สำเนานี้เป็นเจ้าของข้อมูลของตัวเอง การแก้ไขจะไม่กระทบต้นฉบับ
- flatten(*, order: str = 'C') ndarray¶
คืนสำเนาแบบหนึ่งมิติใหม่ของอาร์เรย์
- พารามิเตอร์:
order --
'C'(ค่าเริ่มต้น) จะท่องข้อมูลตามลำดับ C (แกนสุดท้ายเปลี่ยนเร็วที่สุด) ส่วน'F'จะท่องตามลำดับ Fortran (แกนแรกเปลี่ยนเร็วที่สุด)
- reshape(shape: int | tuple[int, ...]) ndarray¶
คืน view ของอาร์เรย์ที่มีรูปร่างใหม่ จำนวนองค์ประกอบทั้งหมดต้องไม่เปลี่ยนแปลง ไม่เช่นนั้นจะเกิด
ValueErrorมีให้ใช้เฉพาะเมื่อULAB_MAX_DIMS > 1เทียบเท่ากับการกำหนดค่าให้shape
- sort(*, axis: int | None = -1) None¶
เรียงลำดับอาร์เรย์ในที่
- พารามิเตอร์:
axis -- แกนที่ใช้เรียงลำดับ
-1(ค่าเริ่มต้น) จะเรียงตามแกนสุดท้าย ส่วนNoneจะ flatten อาร์เรย์ก่อนแล้วจึงเรียง
- tobytes() bytearray¶
คืน
bytearrayที่เป็น alias ของบัฟเฟอร์ข้อมูลพื้นฐานของอาร์เรย์ การเขียนผ่านbytearrayที่คืนมาจะแก้ไขอาร์เรย์ในที่ จะเกิดValueErrorหากอาร์เรย์ไม่หนาแน่น (เช่น sliced view)
- transpose() ndarray¶
คืน transpose ของอาร์เรย์ (แกนกลับ) สำหรับอาร์เรย์หนึ่งมิติจะคืน
selfมีให้ใช้เฉพาะเมื่อULAB_MAX_DIMS > 1แอตทริบิวต์Tคือ shorthand ของเมธอดนี้
- dtype: dtype | int¶
ชนิดข้อมูลขององค์ประกอบของอาร์เรย์ คืนอินสแตนซ์
dtypeเมื่อ firmware สร้างด้วยULAB_HAS_DTYPE_OBJECTเปิดใช้งาน มิฉะนั้นจะคืนรหัสชนิดแบบอักขระเดี่ยวเป็นจำนวนเต็ม
- flat: flatiter¶
iterator แบบ flat ที่ yield ทุกองค์ประกอบของอาร์เรย์ตามลำดับ C ต่างจาก
flatten()การวนซ้ำflatจะไม่จัดสรรอาร์เรย์ใหม่
- shape: tuple[int, ...]¶
ความยาวของอาร์เรย์ตามแต่ละแกน การกำหนด tuple ให้
shapeจะ reshape อาร์เรย์ในที่ (เทียบเท่ากับreshape())
- strides: tuple[int, ...]¶
จำนวนไบต์ที่ต้องก้าวในหน่วยความจำตามแต่ละแกนเพื่อไปยังองค์ประกอบถัดไปตามแกนนั้น
- T: ndarray¶
Transpose ของอาร์เรย์ เทียบเท่ากับ
transpose()
โอเปอเรเตอร์ที่รองรับ¶
ndarray instances support the following operators. Binary operators
broadcast their operands following standard numpy broadcasting rules
and follow numpy's upcasting rules (e.g. uint8 + int8 => int16,
uint16 + int16 => float).
Arithmetic (binary): +, -, *, /, //, %, **.
Reflected (right-hand) operands and the in-place variants
+=, -=, *=, /=, %=, **= are also supported. Both
ndarray-with-ndarray and ndarray-with-scalar forms are
accepted.
การเปรียบเทียบ: ==, !=, <, <=, >, >= แต่ละตัวคืน boolean ndarray ของรูปร่างที่ broadcast แล้ว
Bitwise (integer arrays only): &, |, ^. Applying
these to a float array raises TypeError.
แบบ Unary: + (คืนสำเนา), - (ค่าลบ; กับ dtype ไม่มีเครื่องหมายค่าจะ wrap modulo \(2^N\)), abs() (ค่าสัมบูรณ์เชิงองค์ประกอบ; กับ dtype ไม่มีเครื่องหมายคืนสำเนาโดยไม่คำนวณ), ~ (การกลับบิต เฉพาะ dtype จำนวนเต็ม), len() (คืนความยาวของแกนแรก)
การ index และ slicing: a[i], a[i, j, ...], a[start:stop:step], การ index ด้วย boolean-mask (a[mask]) และการ index ด้วยอาร์เรย์จำนวนเต็ม (fancy) ทั้งหมดรองรับทั้งการอ่านและเขียน
การวนซ้ำ: การวนซ้ำบน ndarray จะ yield sub-array ตามแกนแรก (ทีละองค์ประกอบสำหรับอาร์เรย์ 1 มิติ) ใช้ flat เพื่อวนซ้ำทุกองค์ประกอบ scalar โดยไม่คำนึงถึงจำนวนมิติ
โอเปอเรเตอร์คูณเมทริกซ์ @ ยังไม่ถูกนำมาใช้งาน ให้ใช้ numpy.dot (np.dot(a, b)) แทน
โอเปอเรเตอร์ shift (<<, >>) ยังไม่ถูกนำมาใช้งานในระดับโอเปอเรเตอร์ ให้ใช้ numpy.left_shift และ numpy.right_shift สำหรับการ shift จำนวนเต็มเชิงองค์ประกอบ
การสร้างอาร์เรย์¶
- numpy.array(values: ndarray | list | tuple, *, dtype: int = float) ndarray¶
สร้าง
ndarrayใหม่จาก iterable ซ้อนกันของตัวเลข เป็น constructor สำรองที่เทียบเท่ากับnumpy.ndarray
- numpy.arange(start: int | float, stop: int | float | None = None, step: int | float = 1, *, dtype: int | None = None) ndarray¶
คืนค่าที่เว้นระยะสม่ำเสมอบนช่วงกึ่งเปิด
[start, stop)หากมีอาร์กิวเมนต์ตำแหน่งเพียงตัวเดียวจะถือเป็นstopโดยstart = 0หากละ dtype ไว้จะอนุมานจากอินพุต (จำนวนเต็มหาก start, stop, step ทั้งหมดเป็น int และอยู่ในช่วง)
- numpy.asarray(a: ndarray | list | tuple, *, dtype: int | None = None) ndarray¶
หาก a เป็น
ndarrayที่มี dtype ตรงกับ dtype (หรือ dtype เป็นNone) จะคืน a โดยไม่เปลี่ยนแปลง มิฉะนั้นจะสร้างอาร์เรย์ใหม่ (พร้อมแปลง dtype ที่ร้องขอเมื่อระบุ) Iterable จะถูกแปลงเช่นเดียวกับnumpy.array
- numpy.concatenate(arrays: tuple, *, axis: int = 0) ndarray¶
ต่อลำดับของ
ndarrayตาม axis ที่มีอยู่ อาร์เรย์อินพุตทั้งหมดต้องมี dtype, ndim และรูปร่างที่เหมือนกันในทุกแกนนอกจาก axis
- numpy.diag(a: ndarray, *, k: int = 0) ndarray¶
สำหรับ a แบบ 2 มิติ คืนอาร์เรย์ 1 มิติที่ประกอบด้วย diagonal ที่ k สำหรับ a แบบ 1 มิติ คืนอาร์เรย์สี่เหลี่ยมจัตุรัส 2 มิติที่มี a อยู่บน diagonal ที่ k โดย k อาจเป็นบวก (เหนือ diagonal หลัก) หรือลบ (ใต้ diagonal หลัก)
- numpy.empty(shape: int | tuple[int, ...], *, dtype: int = float) ndarray¶
เป็น alias ของ
zerosคืนอาร์เรย์ที่เต็มไปด้วยศูนย์ตาม shape และ dtype (ulab จะไม่ปล่อยบัฟเฟอร์ไว้โดยไม่ได้กำหนดค่า)
- numpy.eye(N: int, M: int | None = None, k: int = 0, *, dtype: int = float) ndarray¶
คืนอาร์เรย์ N x M แบบ 2 มิติ (สี่เหลี่ยมจัตุรัส N x N หาก M เป็น
None) ที่มีค่าหนึ่งบน diagonal ที่ k และค่าศูนย์ที่อื่น
- numpy.frombuffer(buffer: bytes, *, dtype: int = float, count: int = -1, offset: int = 0) ndarray¶
ตีความออบเจกต์ buffer-protocol เป็น
ndarray1 มิติของ dtype โดย count คือจำนวนรายการที่อ่าน (-1อ่านทุกรายการที่มี) และ offset ข้ามไบต์จำนวนนั้นที่ต้นบัฟเฟอร์
- numpy.full(shape: int | tuple[int, ...], fill_value: int | float | bool, *, dtype: int = float) ndarray¶
คืนอาร์เรย์ใหม่ตาม shape และ dtype โดยทุกองค์ประกอบตั้งค่าเป็น fill_value
- numpy.linspace(start: float, stop: float, num: int = 50, *, endpoint: bool = True, retstep: bool = False, dtype: int = float) ndarray | tuple[ndarray, float]¶
Return num evenly spaced samples over the closed interval
[start, stop](or half-open if endpoint isFalse). When retstep isTrue, return a tuple(samples, step).
- numpy.logspace(start: float, stop: float, num: int = 50, *, base: float = 10.0, endpoint: bool = True, dtype: int = float) ndarray¶
คืน num ตัวอย่างที่เว้นระยะสม่ำเสมอบน log scale โดยผลลัพธ์เริ่มที่
base ** startและสิ้นสุดที่base ** stop
- numpy.meshgrid(*xi: ndarray, indexing: str = 'xy') tuple[ndarray, ...]¶
คืน tuple ของเมทริกซ์พิกัดจากลำดับของอาร์เรย์พิกัดหนึ่งมิติ เมื่อ indexing เป็น
'xy'(ค่าเริ่มต้น) อินพุตสองตัวแรกจะถูกถือเป็นพิกัด Cartesian และแกนผลลัพธ์จะถูกสลับ เมื่อ'ij'จะใช้การ index แบบเมทริกซ์ การนำไปใช้สอดคล้องกับ NumPy ที่เทียบเท่าด้วยcopy=Trueและsparse=False
การตรวจสอบ / การพิมพ์¶
- numpy.get_printoptions() dict¶
คืนตัวเลือกการพิมพ์อาร์เรย์ปัจจุบันเป็น dict ที่มีคีย์
thresholdและedgeitems
- numpy.set_printoptions(*, threshold: int | None = None, edgeitems: int | None = None) None¶
ตั้งค่าตัวเลือกการพิมพ์อาร์เรย์ threshold คือจำนวนสูงสุดขององค์ประกอบอาร์เรย์ที่พิมพ์แบบเต็ม ส่วน edgeitems คือจำนวนรายการที่แสดงที่ปลายแต่ละด้านของแกนเมื่อสรุปอาร์เรย์
การเปรียบเทียบ¶
- numpy.clip(a: ndarray | int | float, a_min: ndarray | int | float, a_max: ndarray | int | float) ndarray | int | float¶
ตัดค่าของ a ให้
a_min <= result <= a_maxเทียบเท่ากับmaximum(a_min, minimum(a, a_max))โดยการ broadcast ใช้กฎเดียวกับminimum
- numpy.equal(x1: ndarray | int | float, x2: ndarray | int | float) ndarray | bool¶
x1 == x2เชิงองค์ประกอบ คืน booleanndarray(หรือboolscalar หากอินพุตทั้งสองเป็น scalar) ให้ไว้เพื่อความสามารถในการพกพา โอเปอเรเตอร์==บนอาร์เรย์ให้ผลลัพธ์เดียวกัน
- numpy.not_equal(x1: ndarray | int | float, x2: ndarray | int | float) ndarray | bool¶
x1 != x2เชิงองค์ประกอบ คู่ตรงข้ามของ boolean กับequal
- numpy.isfinite(x: ndarray | int | float) ndarray | bool¶
คืนอาร์เรย์ boolean (หรือ scalar) ที่เป็น
Trueเมื่ออินพุตมีค่าจำกัด อินพุตจำนวนเต็มมีค่าจำกัดเสมอ
- numpy.isinf(x: ndarray | int | float) ndarray | bool¶
คืนอาร์เรย์ boolean (หรือ scalar) ที่เป็น
Trueเมื่ออินพุตเป็นอนันต์ อินพุตจำนวนเต็มไม่มีทางเป็นอนันต์
- numpy.maximum(x1: ndarray | int | float, x2: ndarray | int | float) ndarray | int | float¶
ค่าสูงสุดเชิงองค์ประกอบของสองอาร์เรย์ / scalar อาร์กิวเมนต์จะถูก broadcast รวมกัน หาก dtype ต่างกัน output จะถูก upcast
- numpy.minimum(x1: ndarray | int | float, x2: ndarray | int | float) ndarray | int | float¶
ค่าต่ำสุดเชิงองค์ประกอบของสองอาร์เรย์ / scalar คู่ตรงข้ามกับ
maximum
การลดค่าเชิงตัวเลข¶
- numpy.all(a: ndarray | list | tuple, *, axis: int | None = None) ndarray | bool¶
ทดสอบว่าองค์ประกอบทั้งหมดตาม axis ประเมินเป็น
Trueหรือไม่ ด้วยaxis=None(ค่าเริ่มต้น) อาร์เรย์แบบ flatten จะถูกทดสอบและคืนboolเดี่ยว
- numpy.any(a: ndarray | list | tuple, *, axis: int | None = None) ndarray | bool¶
ทดสอบว่ามีองค์ประกอบใดตาม axis ประเมินเป็น
Trueหรือไม่ ด้วยaxis=Noneอาร์เรย์แบบ flatten จะถูกทดสอบ
- numpy.argmax(a: ndarray | list | tuple, *, axis: int | None = None) ndarray | int¶
คืน index ขององค์ประกอบสูงสุดตาม axis ด้วย
axis=Noneอาร์เรย์จะถูก flatten และคืนจำนวนเต็มเดี่ยว
- numpy.argmin(a: ndarray | list | tuple, *, axis: int | None = None) ndarray | int¶
คืน index ขององค์ประกอบต่ำสุดตาม axis ด้วย
axis=Noneอาร์เรย์จะถูก flatten และคืนจำนวนเต็มเดี่ยว
- numpy.argsort(a: ndarray, *, axis: int = -1) ndarray¶
คืน
ndarrayindex จำนวนเต็มไม่มีเครื่องหมายที่รายการจะเรียง a ตามลำดับจากน้อยไปมากตาม axis dtype ผลลัพธ์คือuint16ดังนั้นแกนใดก็ตามต้องไม่เกิน 65535 องค์ประกอบaxis=Noneไม่รองรับ
- numpy.diff(a: ndarray, *, n: int = 1, axis: int = -1) ndarray¶
คืนผลต่างไปข้างหน้าแบบไม่ต่อเนื่องลำดับที่ n ของ a ตาม axis โดย n ต้องอยู่ใน
0..9(stencil การหาอนุพันธ์ถูกเก็บในint8) ความยาวของ axis จะลดลง n คีย์เวิร์ดprependและappendของ numpy ไม่ถูกนำมาใช้งาน
- numpy.flip(a: ndarray, *, axis: int | None = None) ndarray¶
คืนอาร์เรย์ใหม่ที่ลำดับขององค์ประกอบถูกกลับตาม axis ด้วย
axis=Noneอาร์เรย์จะถูกกลับตามทุกแกน
- numpy.max(a: ndarray | list | tuple, *, axis: int | None = None, keepdims: bool = False) ndarray | int | float¶
คืนองค์ประกอบสูงสุดตาม axis ด้วย
axis=None(ค่าเริ่มต้น) อาร์เรย์แบบ flatten จะถูกลดเป็น scalar คีย์เวิร์ดoutของ numpy ไม่ถูกนำมาใช้งาน
- numpy.min(a: ndarray | list | tuple, *, axis: int | None = None, keepdims: bool = False) ndarray | int | float¶
คืนองค์ประกอบต่ำสุดตาม axis คู่ตรงข้ามกับ
max
- numpy.mean(a: ndarray | list | tuple, *, axis: int | None = None, keepdims: bool = False) ndarray | float¶
คืนค่าเฉลี่ยเลขคณิตตาม axis ด้วย
axis=None(ค่าเริ่มต้น) ค่าเฉลี่ยของอาร์เรย์แบบ flatten จะถูกคืนเป็นfloat
- numpy.median(a: ndarray, *, axis: int | None = None) ndarray | float¶
คืนค่ามัธยฐานตาม axis ด้วย
axis=Noneอาร์เรย์จะถูก flatten ก่อน dtype ผลลัพธ์เป็น float เสมอ
- numpy.roll(a: ndarray, shift: int, *, axis: int | None = None) ndarray¶
คืน a ที่มีองค์ประกอบถูกหมุน (เลื่อนแบบวนรอบ) shift ตำแหน่ง ด้วย
axis=None(ค่าเริ่มต้น) อาร์เรย์จะถูก flatten ก่อน การ shift ค่าลบจะหมุนในทิศทางตรงข้าม
- numpy.sort(a: ndarray, *, axis: int = -1) ndarray¶
คืนสำเนาที่เรียงแล้วของ a ตาม axis โดยใช้ heap sort ด้วย
axis=Noneอาร์เรย์จะถูก flatten ก่อน คีย์เวิร์ดkindและorderของ numpy ไม่ถูกนำมาใช้งาน
สถิติ¶
- numpy.bincount(x: ndarray, *, weights: ndarray | None = None, minlength: int | None = None) ndarray¶
นับจำนวนการเกิดของแต่ละค่าในอาร์เรย์จำนวนเต็มบวกหนึ่งมิติ x dtype ของ x ต้องเป็น
uint8หรือuint16หาก weights ถูกระบุ แต่ละรายการของ x จะมีส่วนร่วมตามน้ำหนักที่ตรงกันแทนที่จะเป็น1และผลลัพธ์จะมี dtypefloatมิฉะนั้น output จะมี dtypeuint16หาก minlength ถูกระบุ อาร์เรย์ผลลัพธ์จะมีองค์ประกอบอย่างน้อยจำนวนนั้น (รายการพิเศษเป็นศูนย์)
การแปลง¶
- numpy.compress(condition: ndarray | list | tuple, a: ndarray, *, axis: int | None = None) ndarray¶
คืน slice ของ a ที่เลือกตาม axis โดย boolean condition ด้วย
axis=Noneจะใช้อาร์เรย์แบบ flatten
การประมาณ¶
- numpy.interp(x: ndarray, xp: ndarray, fp: ndarray, *, left: float | None = None, right: float | None = None) ndarray¶
การ interpolation เชิงเส้นหนึ่งมิติ xp ต้องเป็นอาร์เรย์ 1 มิติที่เพิ่มขึ้นแบบ monotonic ของค่าอิสระ fp ประกอบด้วยค่าตาม x คือจุดที่ประเมิน interpolant left และ right แทนที่ค่าที่คืนสำหรับ
x < xp[0]และx > xp[-1]ตามลำดับ (ค่าเริ่มต้น:fp[0]และfp[-1])
การเลือก¶
- numpy.take(a: ndarray, indices: ndarray | list | tuple, *, axis: int | None = None, out: ndarray | None = None, mode: str | None = None) ndarray¶
นำองค์ประกอบจาก a ที่ indices ที่กำหนดตาม axis ด้วย
axis=Noneจะใช้อาร์เรย์แบบ flatten mode เลือกพฤติกรรมนอกขอบเขต:"raise"(ค่าเริ่มต้น -- เกิดValueError),"wrap"(modulo ความยาวแกน) หรือ"clip"(ตัดให้อยู่ในช่วงที่ถูกต้อง index ค่าลบไม่อนุญาต) หาก out ถูกระบุผลลัพธ์จะถูกเขียนลงในนั้น
ตรรกะระดับบิต¶
- numpy.bitwise_and(x1: ndarray, x2: ndarray) ndarray¶
AND ระดับบิตเชิงองค์ประกอบของสองอาร์เรย์จำนวนเต็ม รองรับการ broadcast dtype ที่ไม่ใช่จำนวนเต็มจะเกิด exception
- numpy.bitwise_or(x1: ndarray, x2: ndarray) ndarray¶
OR ระดับบิตเชิงองค์ประกอบของสองอาร์เรย์จำนวนเต็ม
- numpy.bitwise_xor(x1: ndarray, x2: ndarray) ndarray¶
XOR ระดับบิตเชิงองค์ประกอบของสองอาร์เรย์จำนวนเต็ม
การกรอง¶
พหุนาม¶
I/O¶
- numpy.load(file: str) ndarray¶
อ่านอาร์เรย์ที่เคยเขียนด้วย
saveจาก file (รูปแบบ.npyอิสระจากแพลตฟอร์มของ numpy) Endianness จะถูกแปลงตามต้องการหากลำดับไบต์ของไฟล์ต่างจากโฮสต์
- numpy.loadtxt(file: str, *, delimiter: str | None = None, comments: str = '#', max_rows: int = -1, usecols: int | ndarray | list | tuple | None = None, dtype: int = float, skiprows: int = 0) ndarray¶
อ่านข้อมูลตัวเลขจาก file ข้อความและคืนเป็น
ndarraydelimiter มีค่าเริ่มต้นเป็น whitespace comments คือ marker comment บรรทัด max_rows จำกัดจำนวนแถวข้อมูลที่อ่าน (-1สำหรับทั้งหมด) usecols เลือกคอลัมน์ตาม index และ skiprows ข้ามแถวนำจำนวนนั้น หาก dtype ไม่ใช่ float ค่าจะถูกแปลงโดยการปัดเศษ
- numpy.savetxt(file: str, a: ndarray, *, delimiter: str = ' ', header: str | None = None, footer: str | None = None, comments: str = '# ') None¶
เขียน a ลงใน file เป็นข้อความ delimiter คั่นค่าภายในแถว header และ footer หากระบุจะถูกเขียนก่อน/หลังข้อมูล โดยแต่ละบรรทัดนำด้วย comments ค่าจะถูกเขียนเป็นทศนิยม
Universal functions¶
ฟังก์ชันคณิตศาสตร์เชิงองค์ประกอบ แต่ละตัวรับ scalar หรือ ndarray และคืนผลลัพธ์ที่มีรูปร่างตรงกัน (scalar float สำหรับอินพุต scalar, ndarray สำหรับอินพุตอาร์เรย์) เมื่อเรียกด้วย ndarray ผลลัพธ์จะเป็น ndarray ทศนิยมใหม่ คีย์เวิร์ด out ที่เป็นตัวเลือกอาจถูกส่งเพื่อเขียนผลลัพธ์ลงใน ndarray float ที่จัดสรรไว้ล่วงหน้าที่มีขนาดเดียวกัน
- numpy.acos(x: ndarray | float, /) ndarray | float¶
คำนวณ inverse cosine (arc cosine) ของแต่ละองค์ประกอบของ x ผลลัพธ์อยู่ในหน่วยเรเดียน
- numpy.acosh(x: ndarray | float, /) ndarray | float¶
คำนวณ inverse hyperbolic cosine ของแต่ละองค์ประกอบของ x
- numpy.arctan2(y: ndarray | float, x: ndarray | float, /) ndarray | float¶
คำนวณ inverse tangent สองอาร์กิวเมนต์
atan2(y, x)เชิงองค์ประกอบ รองรับการ broadcast ระหว่างอินพุตสองตัว
- numpy.around(x: ndarray, /, decimals: int = 0) ndarray¶
ปัดเศษองค์ประกอบของ
ndarrayx ให้มี decimals ทศนิยมที่กำหนด อาร์กิวเมนต์แรกต้องเป็นndarray
- numpy.asin(x: ndarray | float, /) ndarray | float¶
คำนวณ inverse sine (arc sine) ของแต่ละองค์ประกอบของ x ผลลัพธ์อยู่ในหน่วยเรเดียน
- numpy.asinh(x: ndarray | float, /) ndarray | float¶
คำนวณ inverse hyperbolic sine ของแต่ละองค์ประกอบของ x
- numpy.atan(x: ndarray | float, /) ndarray | float¶
คำนวณ inverse tangent (arc tangent) ของแต่ละองค์ประกอบของ x ผลลัพธ์อยู่ในหน่วยเรเดียน
- numpy.atanh(x: ndarray | float, /) ndarray | float¶
คำนวณ inverse hyperbolic tangent ของแต่ละองค์ประกอบของ x
- numpy.ceil(x: ndarray | float, /) ndarray | float¶
คำนวณ ceiling (จำนวนเต็มที่เล็กที่สุดที่ไม่น้อยกว่าค่า) ของแต่ละองค์ประกอบของ x
- numpy.cos(x: ndarray | float, /) ndarray | float¶
คำนวณ cosine ของแต่ละองค์ประกอบของ x (ในหน่วยเรเดียน)
- numpy.exp(x: ndarray | float, /) ndarray | float¶
Compute the exponential
e**xof each element of x.
- numpy.expm1(x: ndarray | float, /) ndarray | float¶
คำนวณ
exp(x) - 1ของแต่ละองค์ประกอบของ x ด้วยความแม่นยำที่ดีขึ้นใกล้ศูนย์
- numpy.floor(x: ndarray | float, /) ndarray | float¶
คำนวณ floor (จำนวนเต็มที่ใหญ่ที่สุดที่ไม่เกินค่า) ของแต่ละองค์ประกอบของ x
- numpy.sin(x: ndarray | float, /) ndarray | float¶
คำนวณ sine ของแต่ละองค์ประกอบของ x (ในหน่วยเรเดียน)
- numpy.sinc(x: ndarray | float, /) ndarray | float¶
คำนวณฟังก์ชัน sinc ที่ normalize แล้ว
sin(pi*x) / (pi*x)ของแต่ละองค์ประกอบของ x
- numpy.sqrt(x: ndarray | float, /, *, dtype: int = float) ndarray | float¶
Compute the square root of each element of x.