How to Create a NumPy ndarray Object, explain with example.
How to Create a NumPy ndarray Object, explain with example.
IT-Hardware & Networking
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Creating a NumPy ndarray object means building an array using the NumPy library in Python. The
ndarray(N-dimensional array) is the core data structure used for numerical computing.1. Import NumPy
2. Create ndarray from a Python List
You can also create multi-dimensional arrays:
3. Using Built-in Functions
a) Zeros Array
Creates a 2x3 array filled with 0s.
b) Ones Array
Creates a 2x2 array filled with 1s.
c) Empty Array
Creates an array with uninitialized values.
d) Range of Values
Output:
[0 2 4 6 8]e) Evenly Spaced Values
Creates 5 values between 0 and 1.
4. Create Identity Matrix
5. Specify Data Type
6. Random Arrays
7. Check Type
Output:
Summary
You can create a NumPy
ndarrayusing:np.array()→ from listsnp.zeros(),np.ones()→ initialized arraysnp.arange(),np.linspace()→ sequencesnp.random→ random values