[Day-6]NumPy Array-Functions(4)

Advait Joshi
3 min readApr 17, 2024

--

Hello everyone! Today I delved into “how to create an array from an existing data.”

How to create an array from existing data?

You can easily create a new array from a section of an existing array.

Let’s say you have this array:

a = np.array([1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

You can create a new array from a section of your array any time by specifying where you want to slice your array.

arr1 = a[3:8]
arr1
#Output :
array([4, 5, 6, 7, 8])

Here, you grabbed a section of your array from index position 3 through index position 8.

You can also stack two existing arrays, both vertically and horizontally. Let’s say you have two arrays, a1 and a2:

a1 = np.array([[1, 1],
[2, 2]])

a2 = np.array([[3, 3],
[4, 4]])

You can stack them vertically with vstack:

np.vstack((a1, a2))
#Output :
array([[1, 1],
[2, 2],
[3, 3],
[4, 4]])

You can stack them horizontally with hstack:

np.hstack((a1, a2))
#Output :
array([[1, 1, 3, 3],
[2, 2, 4, 4]])

You can split an array into several smaller arrays using hsplit. You can specify the number of equally shaped arrays to return or the columns after which the division should occur.

Let’s say you have this array:

x = np.arange(1, 25).reshape(2, 12)
x
#Output :
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]])

If you wanted to split this array into three equally shaped arrays, you would run:

np.hsplit(x, 3)
#Output :
[array([[ 1, 2, 3, 4],
[13, 14, 15, 16]]), array([[ 5, 6, 7, 8],
[17, 18, 19, 20]]), array([[ 9, 10, 11, 12],
[21, 22, 23, 24]])]

If you wanted to split your array after the third and fourth columns, you’d run:

np.hsplit(x, (3, 4))
#Output :
[array([[ 1, 2, 3],
[13, 14, 15]]), array([[ 4],
[16]]), array([[ 5, 6, 7, 8, 9, 10, 11, 12],
[17, 18, 19, 20, 21, 22, 23, 24]])]

You can use the view method to create a new array object that looks at the same data as the original array (a shallow copy).

Views are an important NumPy concept! NumPy functions, as well as operations like indexing and slicing, will return views whenever possible. This saves memory and is faster (no copy of the data has to be made). However, it’s important to be aware of this — modifying data in a view also modifies the original array!

Let’s say you create this array:

a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

Now we create an array b1 by slicing a and modify the first element of b1. This will modify the corresponding element in a as well!

b1 = a[0, :]
b1
#Output :
array([1, 2, 3, 4])

b1[0] = 99
b1
#Output :
array([99, 2, 3, 4])

a
#Output :
array([[99, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])

Using the copy method will make a complete copy of the array and its data (a deep copy). To use this on your array, you could run:

b2 = a.copy()

This is all that I learned about the different functions of numpy arrays today. My learnings will be documented here on Medium for better understanding. Stay tuned for the next blog.

All The Best People☺️.

Follow me on Linkedin:

www.linkedin.com/in/advaitszone

To read my previous blog “[Day-5]NumPy Array-Functions(3)”:

https://medium.com/@advaitszone/day-5-numpy-array-functions-3-cfe97458d935

--

--