dtype के Basics
हर array याद रखता है कि यह किस तरह के numbers hold करता है (whole numbers, decimals वगैरह) और यह choice memory और precision को affect करती है।
In this page:
Syntax
arr = np.array([value1, value2], dtype=np.float64)
arr.dtype
arr.astype(np.int32)
dtype Basics
dtype हर element का type describe करता है, जैसे int64, float64, bool, या complex128। NumPy इसे आपके data से infer करता है, लेकिन आप इसे dtype argument से set कर सकते हैं या बाद में astype से convert कर सकते हैं। ints और floats mix करना सब कुछ float में upcast कर देता है।
Note:
बड़े arrays पर memory बचाने के लिए int8 या float32 जैसे छोटे dtypes इस्तेमाल करें।
उदाहरण: dtype basics
import numpy as np
a = np.array([1, 2, 3])
b = np.array([1, 2.5, 3])
c = np.array([1, 2, 3], dtype=np.float32)
print(a.dtype, b.dtype, c.dtype)
print("astype int:", b.astype(int))
# Output:
# int64 float64 float32
# astype int: [1 2 3]
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- यह मान लेना कि floats के साथ mix होने के बाद ints ints रहेंगे
- छोटे integer types को silently overflow करना
- यह भूल जाना कि astype एक नया array return करता है
चैप्टर सारांश
- dtype element type set करता है
- NumPy dtype infer करता है लेकिन आप इसे override कर सकते हैं
- astype एक नए dtype में convert करता है
- Mixed int और float float बन जाता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: