Creating a one-dimensional NumPy array
×


Creating a one-dimensional NumPy array

137

Creating a One-Dimensional NumPy Array in Python

Creating a One-Dimensional NumPy Array in Python

In Python, NumPy is a powerful library that provides support for large, multi-dimensional arrays and matrices. One of the most fundamental structures in NumPy is the one-dimensional (1D) array. Understanding how to create and manipulate 1D arrays is essential for data analysis, scientific computing, and machine learning tasks.

Using np.array()

The most straightforward way to create a 1D NumPy array is by converting a Python list or tuple using the np.array() function:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)

Output:

[1 2 3 4 5]

This creates a 1D array with the elements 1, 2, 3, 4, and 5.

Using np.arange()

The np.arange() function returns evenly spaced values within a given interval. It's similar to Python's built-in range() function but returns a NumPy array:

arr = np.arange(0, 10, 2)
print(arr)

Output:

[0 2 4 6 8]

This creates a 1D array starting from 0 up to (but not including) 10, with a step size of 2.

Using np.linspace()

The np.linspace() function returns evenly spaced numbers over a specified range. It's particularly useful when you need a specific number of elements:

arr = np.linspace(0, 10, 5)
print(arr)

Output:

[ 0.   2.5  5.   7.5 10. ]

This creates a 1D array of 5 evenly spaced numbers between 0 and 10.

Using np.zeros()

The np.zeros() function returns a new array of given shape and type, filled with zeros:

arr = np.zeros(5)
print(arr)

Output:

[0. 0. 0. 0. 0.]

This creates a 1D array of 5 zeros. You can also specify the data type:

arr = np.zeros(5, dtype=int)
print(arr)

Output:

[0 0 0 0 0]

This creates a 1D array of 5 zeros with integer data type.

Using np.ones()

The np.ones() function returns a new array of given shape and type, filled with ones:

arr = np.ones(5)
print(arr)

Output:

[1. 1. 1. 1. 1.]

This creates a 1D array of 5 ones. You can also specify the data type:

arr = np.ones(5, dtype=int)
print(arr)

Output:

[1 1 1 1 1]

This creates a 1D array of 5 ones with integer data type.

Using np.fromiter()

The np.fromiter() function creates a new 1D array from an iterable object:

arr = np.fromiter(range(5), dtype=int)
print(arr)

Output:

[0 1 2 3 4]

This creates a 1D array from the range object.

Conclusion

Creating one-dimensional NumPy arrays is a fundamental skill in Python programming. Whether you're initializing arrays with specific values, generating sequences, or creating arrays filled with zeros or ones, NumPy provides efficient and flexible functions to meet your needs. Mastering these functions will enhance your ability to perform numerical computations and data analysis tasks effectively.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat