0% found this document useful (0 votes)
36 views4 pages

Numpy 2

The document demonstrates various operations on NumPy arrays including slicing, indexing, joining, splitting, and boolean operations. Arrays are created and basic operations like slicing and indexing are shown to extract elements or sub-arrays. Functions such as hstack, vstack and dstack are used to join arrays horizontally, vertically and depth-wise. The array_split function splits an array into multiple sub-arrays.

Uploaded by

gshjndhbdbud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views4 pages

Numpy 2

The document demonstrates various operations on NumPy arrays including slicing, indexing, joining, splitting, and boolean operations. Arrays are created and basic operations like slicing and indexing are shown to extract elements or sub-arrays. Functions such as hstack, vstack and dstack are used to join arrays horizontally, vertically and depth-wise. The array_split function splits an array into multiple sub-arrays.

Uploaded by

gshjndhbdbud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import numpy as np

# An exemplar array

arr = [Link]([[-1, 2, 0, 4],

[4, -0.5, 6, 0],

[2.6, 0, 7, 8],

[3, -7, 4, 2.0]])

#original array

print("Original array:\n", arr)

#returns every other rows

print("\nEvery other rows:arr[Link]:\n", arr[Link])

# Slicing array

temp = arr[:2,:3]

print ("\nArray with first 2 rows and 3 columns:arr[:2,:3]:\n", temp)

# Integer array indexing example

temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]]

print ("\nElements at indices (0, 3), (1, 2), (2, 1),"

"(3, 0):\n", temp)

# boolean array indexing example

cond = arr > 2

# cond is a boolean array

temp = arr[cond]

print ("\nElements greater than 2:\n", temp)

#Return every other element from the entire array:

arr = [Link]([1, 2, 3, 4, 5, 6, 7])


print("\nOriginal array:",arr)

print("\nReturns every other elements in the array:arr[::2]:",arr[::2])

#joining two array

arr1 = [Link]([1, 2, 3])

arr2 = [Link]([4, 5, 6])

arr = [Link]((arr1, arr2))

print("\nOriginal arrays:\n",arr1,arr2)

print("\nJoined array:\n",arr)

#Horizondal join

arr = [Link]((arr1, arr2))

print("\nHorizondal joining:\n",arr)

#Vertical join

arr = [Link]((arr1, arr2))

print("\nVertical joining:\n",arr)

#Depth join

arr = [Link]((arr1, arr2))

print("\nDepth joining:\n",arr)

#Splitting array

arr = [Link]([1, 2, 3, 4, 5, 6])

newarr = np.array_split(arr, 3)

print("\nOriginal Array:\n",arr)

print("\nSplitted array:\n",newarr)

#Displaying splitted array in another form

print("\nSplitted array in another form:\n")

print(newarr[0])

print(newarr[1])
print(newarr[2])

OUTPUT:

Original array:

[[-1. 2. 0. 4. ]

[ 4. -0.5 6. 0. ]

[ 2.6 0. 7. 8. ]

[ 3. -7. 4. 2. ]]

Every other rows:arr[Link]:

[[-1. 2. 0. 4. ]

[ 2.6 0. 7. 8. ]]

Array with first 2 rows and 3 columns:arr[:2,:3]:

[[-1. 2. 0. ]

[ 4. -0.5 6. ]]

Elements at indices (0, 3), (1, 2), (2, 1),(3, 0):

[4. 6. 0. 3.]

Elements greater than 2:

[4. 4. 6. 2.6 7. 8. 3. 4. ]

Original array: [1 2 3 4 5 6 7]

Returns every other elements in the array:arr[::2]: [1 3 5 7]

Original arrays:

[1 2 3] [4 5 6]
Joined array:

[1 2 3 4 5 6]

Horizondal joining:

[1 2 3 4 5 6]

Vertical joining:

[[1 2 3]

[4 5 6]]

Depth joining:

[[[1 4]

[2 5]

[3 6]]]

Original Array:

[1 2 3 4 5 6]

Splitted array:

[array([1, 2]), array([3, 4]), array([5, 6])]

Splitted array in another form:

[1 2]

[3 4]

[5 6]

You might also like