Creating Arrays

Estimated reading: 1 minute 7 views

NumPy’s fundamental object is the array, a table of elements with the same type. You can create an array using the np.array() function:

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

Array Operations

Basic Operations

Perform basic operations on arrays like addition, subtraction, multiplication, and division:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

result = a + b
print(result)

Element-wise Operations

NumPy allows element-wise operations, where each element of an array is operated on individually:

arr = np.array([1, 2, 3])
squared = np.square(arr)
print(squared)

Shape and Reshape

Determine the shape of an array or reshape it:

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)  # Output: (2, 3)

reshaped_arr = arr.reshape(3, 2)
print(reshaped_arr)
Share this Doc

Creating Arrays

Or copy link