← Back to NumPy Course | Chapter 11: File I/O | Lesson 5 of 5

np.frombuffer() का इस्तेमाल

frombuffer सीधे memory में raw bytes से, बिना copy किए, एक array बनाता है।

In this page:

  1. np.frombuffer()
Syntax
python
arr = np.frombuffer(buffer, dtype=dtype)

np.frombuffer()

np.frombuffer एक bytes-like object को आपके नाम दिए dtype के 1-D array की तरह interpret करता है। यह source buffer के साथ memory share करता है, इसलिए fast है। Buffer को पूरे elements के लिए काफी बड़ा होना चाहिए। Immutable bytes से results read-only हैं।

Note: अगर आपको data modify करना है .copy() से एक writable copy बनाएँ।

उदाहरण: np.frombuffer()

python
import numpy as np

raw = b"\x01\x02\x03\x04"
a = np.frombuffer(raw, dtype=np.uint8)
print(a)
print(a.flags.writeable)
b = np.arange(3, dtype=np.int16).tobytes()
print(b)
print(np.frombuffer(b, dtype=np.int16))

# Output:
# [1 2 3 4]
# False
# b'\x00\x00\x01\x00\x02\x00'
# [0 1 2]
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. #}
आम गलतियां
  1. dtype specify करना भूल जाना
  2. एक ऐसा buffer pass करना जिसकी length itemsize का multiple न हो
  3. एक read-only array modify करने की कोशिश करना
चैप्टर सारांश
  • frombuffer raw bytes wrap करता है
  • यह data copy नहीं करता
  • dtype interpretation define करता है
  • Bytes input read-only arrays देता है
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.