# Importing the 'numpy' package
import numpy
from numpy import *
# Creating arrays using array() function:
ary1 = array([1, 2, 3, 4, 5], int)
ary2 = array([0, 2, 7, 4, 1])
print('The array1:', ary1)
print('The array2:', ary2)
Output:
The array1: [1 2 3 4 5]
The array2: [0 2 7 4 1]
# Mathematical Operations on Arrays:
ary3 = ary1+3
ary4 = ary1+ary2
print('The result of ary1+3:', ary3)
print('The result of ary1+ary2:', ary4)
Output:
The result of ary1+3: [4 5 6 7 8]
The result of ary1+ary2: [ 1 4 10 8 6]
# Logical comparison of arrays:
c1 = (ary1 == ary2)
c2 = (ary1 <= ary2)
print('the result of ary1==ary2:', c1)
print('the result of ary1<=ary2:', c2)
Output:
the result of ary1==ary2: [False True False True False]
the result of ary1<=ary2: [False True True True False]
# Checking all() or any() of the element is true:
print('Check if any one element is true:', any(c2))
print('Check if all the elements are true:', all(c2))
Output:
Check if any one element is true: True
Check if all the elements are true: False
# Logical functions of arrays:
a = array([1, 2, 3, 5])
b = array([3, 0, 2, 7])
c = logical_not(a)
d = logical_and(a >= 2, a < 4)
e = logical_or(b > 0, b <= 2)
print('The result of logical_not is:', c)
print('The result of logical_and is:', d)
print('The result of logical_or is:', e)
Output:
The result of logical_not is: [False False False False]
The result of logical_and is: [False True True False]
The result of logical_or is: [ True True True True]
# Comparing two arrays and return bigger elements to another array:
a = array([10, 15, 13, 23, 33])
b = array([7, 17, 11, 25, 27])
c = where(a > b, a, b)
print('Compare a and b then return bigger elements:', c)
Output:
Compare a and b then return bigger elements: [10 17 13 25 33]
# Finding the index of non-zero elements:
ary1 = array([1, 0, 3, 4, 0, 7, 9], int)
i = nonzero(ary1)
print('The array elements are:', ary1)
# display the index of non-zero elements
print('The non zero element found at indexes:')
for j in i:
print(j)
# display the non zero elements
print('\nThe non zero elements are:', ary1[i])
Output:
The array elements are: [1 0 3 4 0 7 9]
The non zero element found at indexes:
[0 2 3 5 6]
The non zero elements are: [1 3 4 7 9]
# Aliasing the Arrays:
ary1 = array([1, 0, 3, 4, 0, 7, 9], int)
ary2 = ary1 # aliasing the array
ary2[1] = 99 # if effects in both ary1 and ary2
print('The original array:', ary1)
print('The alias array:', ary2)
Output:
The original array: [ 1 99 3 4 0 7 9]
The alias array: [ 1 99 3 4 0 7 9]
# Viewing and Copying the Arrays:
ary1 = array([1, 0, 3, 4, 0, 7, 9], int)
ary2 = ary1.view() # creating a view of ary1 and call into ary2
ary2[1] = 99 # if effects in both ary1 and ary2
print('The original array:', ary1)
print('The alias array:', ary2)
Output:
The original array: [ 1 99 3 4 0 7 9]
The alias array: [ 1 99 3 4 0 7 9]
# Slicing and Indexing array elements:
a = arange(10, 16)
print('The array a of range 10 to 16:', a)
n = len(a)
print('The size n of array a is:', n)
# start=1, stop=(6-1), step=2
b = a[1:6:2]
print('The sliced array from a is:', b)
Output:
The array a of range 10 to 16: [10 11 12 13 14 15]
The size n of array a is: 6
The sliced array from a is: [11 13 15]
# start=(n-2)=4, stop=(2-1), step=-1(decreasing size)
b = a[-2:2:-1]
print('The sliced array from a is:', b)
Output:
The sliced array from a is: [14 13]
# start=0, stop=(n-2)-1=3, step=0
b = a[:-2:]
print('The sliced array from a is:', b)
Output:
The sliced array from a is: [10 11 12 13]
# Retrieving all the elements from a
c = a[::]
print('The array c:', c)
Output:
The array c: [10 11 12 13 14 15]
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.