Numpy: Numerical Computation in Python Flashcards

1
Q

This is prompt.

What stands for Numpy

This is Footnote

A

Numerical Python

This is Footnote in Answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Numpy developed by ?

A

Travis Oliphant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an array in Python ?

A

Array is the data structure that stores values of Same data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How Array is created in Python ?

A

To create a array in Python we have to use Numpy library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to import numpy Library ?

A

import numpy as np

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to Define array in python ?

A
#We have to use numpy library 
import numpy as np
a1 = np.array([1,2,3,4,5])
print(a1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to print last element of numpy array ?

A

print(a1[-1])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to print the second last and third last element of numpy array ?

A
a1[-2]
a1[-3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to get the size of numpy array ?

A

We have to use the array property- array1.size
~~~
a.numpy.array([1,2,3,4])
print(a.size())
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to get the total bytes of array in python without using byte property ?

A
import numpy as np
a1=np.array([1,2,3,4])
print(f"Total Array Size is {a1.size} Items")
print(f"Each Element Size is {a1.itemsize} bytes")
print(f"Total size of Array in bytes is : {a1.itemsize* a1.size} bytes")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to Print the total bytes of Array ?

A

We Have to use an array property - nbytes
~~~
a1=np.array([1,2,3,4])
print(f”Total Bytes of Array {a1.nbytes}”)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to get the size of array element ?

A

We have to use the property items size.
~~~
print(a1.itemsize)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to get the data type of an array elements ?

A

We have to use dtype property of array ?
~~~
print(a1.dtype)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to update value at given index in an array ?

A
array[3]=10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to create three dimension array ?

A
threeDArray=np.array([[1,2,3,4,5],
                      [1,2,3,4,5],
                      [1,2,3,4,5]])
print(threeDArray)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to iterate three dimensional array ?

A

We have to use shape property to get row and colum value.
~~~
import numpy as np
threeDArray=np.array([[1,2,3,4,5],
[2,6,7,8,9],
[3,4,5,6,5]])

for i in range(twoDArray.shape[0]):
for j in range(twoDArray.shape[1]):
print(f”{twoDArray[i][j]} “,end=” “)
print(“\n”)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to know the array is One dimensional or two dimensional ?

A

We have to use the property ndim
~~~
a1.ndim
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to get the row and colum count of array ?

A

We have to use the shape property
~~~
threeDArray=np.array([[1,2,3,4,5],
[2,6,7,8,9],
[3,4,5,6,5]])
print(threeDArray.shape)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the different properties of numpy array?

A

Some property are: size,itemsize,nbytes, ndim,shape,dtype
~~~
print(threeDArray.size)
print(threeDArray.itemsize)
print(threeDArray.nbytes)
print(threeDArray.shape)
print(threeDArray.ndim)
print(threeDArray.dtype)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to Print array values by adding one ?

A

print(array1 + 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How to print array values by myltiplying by two ?

A
print(array1 * 2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to access array elements by using slice notation ?

A

It Prints the elements 0,1,2
~~~
array1=np.array([1,2,3,4,5,6])
print(array1[0:3])
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the output of this ?
~~~
a=np.array([2,8,6,76,56,87])
print(a[2:])
~~~

A
[ 6 76 56 87]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the output of ?
~~~
a=np.array([2,8,6,78,55,94])
print(a[:5])
~~~

A
[ 2  8  6 78 55]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the output of ?
~~~
a=np.array([2,8,6,78,55,94,45,4,8,23])
print(a[3:8:2])
~~~

A
[78 94  4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the output of ?
~~~
a=np.array([2,8,6,78,55,94,45,4,8,23])
print(a[0::2])
~~~

A

[ 2 6 55 45 8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the output of ?
~~~
a=np.array([2,8,6,78,55,94,45,4,8,23,89,90])
print(a[9::-2])
~~~

A

[23 4 94 78 8]

28
Q

What is the output of ?
~~~
a=np.array([[1,2,3,4,5],
[2,6,7,8,9],
[3,4,5,6,5],
[4,8,9,8,6]])
print(a[:2,:4])
~~~

A
[[1 2 3 4]
 [2 6 7 8]]
29
Q

What is the output of ?
~~~
a=np.array([[1,2,3,4,5],
[2,6,7,8,9],
[3,4,5,6,5],
[4,8,9,8,6]])
print(a[:3,:5])
~~~

A
[[1 2 3 4 5]
 [2 6 7 8 9]
 [3 4 5 6 5]]
30
Q

What is the output of?
~~~
a=np.array([[1,2,3,4,5],
[2,6,7,8,9],
[3,4,5,6,5],
[4,8,9,8,6]])
print(a[0,:])
~~~

A
[1 2 3 4 5]
31
Q

What is the output of ?
~~~
a=np.array([[1,2,3,4,5],
[2,6,7,8,9],
[3,4,5,6,5],
[4,8,9,8,6]])
print(a[:,0])
~~~

A
[1 2 3 4]
32
Q

How to specify data type in Array declaration ?

A
b=np.array([2,5,6,8,7.7],dtype=int)
print(b)
print(b.dtype)
33
Q

How to get row & columns of an 3D array and print the values through iteration ?

A

We have to use shape property to get row and columns
~~~
a=np.array([[1,2,3,4,5],
[2,6,7,8,9],
[3,4,5,6,5],
[4,8,9,8,6]])
for i in range(a.shape[0]):
for j in range(a.shape[1]):
print(f”{a[i][j]}”,end=” “)
print(“\n”)
~~~

34
Q

What is the output of ?
~~~
array1=np.array([1,2,3,4,5,6])
print(array1[0:3])
~~~

A
[1,2,3]
35
Q

How to create an single dimension array of 10 elements by using a list comprehension method ?

A
a=np.array([i for i in range(20)])
print(a)
print(type(a))
36
Q

How to create an int Array of all 0s , array should be off three rows and four columns ?

A
a=np.ones([3,4],dtype=int)
print(a)
37
Q

How to create an Array of Strings of all ones, array should be of three Rows and four columns ?

A
a=np.ones([3,4],dtype=str)
print(a)
38
Q

How to create an int array of all ones, Array should be off Three rows and four columns ?

A
a=np.ones([3,4],dtype=int)
print(a)
39
Q

How to create an oneD Array of values 5to70
by using and inbuilt method ?

A
a=np.arange(5,20,2)
print(a)

Output :
[ 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

40
Q

How to create an Array, which exactly create six values between some range ?

A

We have to use linspace method.
~~~
a=np.linspace(3,70,6)
print(a)
~~~

41
Q

How to convert 1 dimension array to two dimension array ?

A

We have to use reshape method.
~~~
a=np.array([5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]).reshape(5,3)
print(a)
~~~

42
Q

How to convert an existing array to single dimension to multi dimension ?

A

We have to use reshape method.
~~~
a=np.array([5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])
b=np.reshape(a,(3,5))
print(b)
~~~

43
Q

How to concatenate two arrays ?

A
a=np.array([5,7,8,6,4])
b=np.arange(3,78,4)
c=np.concatenate([a,b])
print(c)
44
Q

How to create an float value array

A

We have two types
~~~
a=np.array([1,2,3,4],dtype=’f’)
a=np.array([1,2,3,4],dtype=float)
~~~

45
Q

How to get the total row & columns of array

A
array.shape
46
Q

What is the output of ?
~~~
arr = np.array([1,2,3,4,5,6,7])
print(arr[2:5])
~~~

A
 [3, 4, 5]
47
Q

How to compared to arrays for equality using operators ?

A
# Compare Two Arrays and return True/False for each index values 
arr1=np.array([5,4,7,9])
arr2=np.array([6,4,8,9])
print(arr1==arr2)
print(arr1<arr2)
# Output :[False  True False  True]
# Output :[ True False  True False]
48
Q

How to perform less than operation on to arrays ?

A
# Compare Two Arrays and return True/False for each index values 
arr1=np.array([5,4,7,9])
arr2=np.array([6,4,8,9])
print(arr1<arr2)
# Output :[ True False  True False]
49
Q

How to perform equality operation by using an inbuilt method ?

A

We have to use array_equal() method,which returns True/False
~~~
# Compare Two Arrays and return only True or False
arr2=np.array([1,10,2])
arr3=np.array([2,10,1])
print(np.array_equal(arr1,arr2))
#Output : False
~~~

50
Q

What is the output of array_equal() method ?

A

It returns only true or false by comparing all index values of array, if array are exactly mirror copy then this Method returns True

51
Q

How to perform logical or operation on two arrays?

A
# Perform Logical OR operation on two Arrays, returns True/False for each index
arr1=np.array([0,1,1,0])
arr2=np.array([1,1,1,0])
print(np.logical_or(arr1,arr2))    
#Output:[False  True  True False]
52
Q

How to perform logical and operation on two arrays ?

A
# Perform Logical AND operation on two Arrays, returns True/False for each index
arr1=np.array([0,1,1,0])
arr2=np.array([1,1,1,0])
print(np.logical_and(arr1,arr2))    
#Output [False  True  True False]
53
Q

How to perform logical xor operation on two arrays ?

A
# Perform Logical XOR operation on two Arrays,Returns True if both are same,
# return false if both are dfferent for each index values
# 
arr1=np.array([0,1,1,0])
arr2=np.array([1,1,1,0])
print(np.logical_xor(arr1,arr2)) 
#Output: [ True False False False]
54
Q

How to perform logical not operation on a array ?

A
# Perform Logical Not operation on two Arrays,Returns True if 1,
# return false if 0
# 
arr1=np.array([0,1,1,0])
print(np.logical_not(arr1)) 
# Output : [ True False False  True]
55
Q

How to add two arrays ?

A
# Add two same size arrays
a1=np.array([-1,-4,7,6,4,3,-9])
a2=np.array([3,6,1,2,8,9,1])
print(np.add(a1,a2))
print(a1+a2)
# Output:[ 2  2  8  8 12 12 -8]
# Output:[ 2  2  8  8 12 12 -8]
56
Q

How to convert negative value to positive of an array?

A
#Change negative values to positive
a1=np.array([-1,-4,7,6,4,3,-9])
print(np.abs(a1))
# Output: [1 4 7 6 4 3 9]
57
Q

How to print array value to the power of?

A
# print array values to the power of 
a1=np.array([3,6,1,2,8,9,1])
print(np.power(a1,3))
#Output :[ 27 216   1   8 512 729   1]
58
Q

How to find some of all values of array ?

A
# Get sum of all values of array 
a1=np.array([3,6,1,2,8,9,1])
print(np.add.reduce(a1))
# Output: 30
59
Q

How to get product of each value of array ?

A
# Get Product of array values
a1=np.array([3,6,1,2,8,9,1])
print(np.multiply.reduce(a1))
# Output: 2592
60
Q

How to do accumulate multification of array ?

A
# Multiply Accumulate
a1=np.array([3,6,8,7,9,2])
print(np.multiply.accumulate(a1))
#Output : [    3    18   144  1008  9072 18144]
61
Q

How to do accumulate addition of array ?

A
# Multiply Accumulate
a1=np.array([3,6,8,7,9,2])
print(np.multiply.accumulate(a1))
#Output : [    3    18   144  1008  9072 18144]
62
Q

How to find a maximum Valueof an array ?

A
# Find Maximum Value of Array 
a1=np.array([3,6,8,7,9,2])
print(np.max(a1))
# Output: 9
63
Q

How to find minimum value of an array ?

A
a1=np.array([3,6,8,7,9,2])
print(np.min(a1))
#Output: 2
64
Q

How to get Index value of minimum value of an array ?

A
# Get get index value of minimum value of array
a1=np.array([3,6,8,7,9,2])
print(np.argmin(a1))
#Output: 5
65
Q

How to How to get index value of maximum value of an array ?

A
# Get get index value of minimum value of array
a1=np.array([3,6,8,7,9,2])
print(np.argmax(a1))
#Output: 4
66
Q

How to find square root of array values ?

A
# Get  Get the square root of arre values
a1=np.array([4,16,25,81,9,121])
print(np.sqrt(a1))
#Output: [ 2.  4.  5.  9.  3. 11.]
67
Q

How to open new values to an existing array ?

A
#Appned values  To an array
a1=np.array([4,16,25,81,9,121])
print(f"Old Values of array   : {a1}")
print(f"array with new values : {np.append(a1,[[100,200,30]])}")
#Output : Old Values of array   : [  4  16  25  81   9 121]
#Output : array with new values : [  4  16  25  81   9 121 100 200  30]