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
What is the output of ? ``` a=np.array([2,8,6,78,55,94,45,4,8,23]) print(a[3:8:2]) ```
``` [78 94 4] ```
26
What is the output of ? ``` a=np.array([2,8,6,78,55,94,45,4,8,23]) print(a[0::2]) ```
[ 2 6 55 45 8]
27
What is the output of ? ``` a=np.array([2,8,6,78,55,94,45,4,8,23,89,90]) print(a[9::-2]) ```
[23 4 94 78 8]
28
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]) ```
``` [[1 2 3 4] [2 6 7 8]] ```
29
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]) ```
``` [[1 2 3 4 5] [2 6 7 8 9] [3 4 5 6 5]] ```
30
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,:]) ```
``` [1 2 3 4 5] ```
31
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]) ```
``` [1 2 3 4] ```
32
How to specify data type in Array declaration ?
``` b=np.array([2,5,6,8,7.7],dtype=int) print(b) print(b.dtype) ```
33
How to get row & columns of an 3D array and print the values through iteration ?
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
What is the output of ? ``` array1=np.array([1,2,3,4,5,6]) print(array1[0:3]) ```
``` [1,2,3] ```
35
How to create an single dimension array of 10 elements by using a list comprehension method ?
``` a=np.array([i for i in range(20)]) print(a) print(type(a)) ```
36
How to create an int Array of all 0s , array should be off three rows and four columns ?
``` a=np.ones([3,4],dtype=int) print(a) ```
37
How to create an Array of Strings of all ones, array should be of three Rows and four columns ?
``` a=np.ones([3,4],dtype=str) print(a) ```
38
How to create an int array of all ones, Array should be off Three rows and four columns ?
``` a=np.ones([3,4],dtype=int) print(a) ```
39
How to create an oneD Array of values 5to70 by using and inbuilt method ?
``` 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
How to create an Array, which exactly create six values between some range ?
We have to use linspace method. ``` a=np.linspace(3,70,6) print(a) ```
41
How to convert 1 dimension array to two dimension array ?
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
How to convert an existing array to single dimension to multi dimension ?
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
How to concatenate two arrays ?
``` a=np.array([5,7,8,6,4]) b=np.arange(3,78,4) c=np.concatenate([a,b]) print(c) ```
44
How to create an float value array
We have two types ``` a=np.array([1,2,3,4],dtype='f') a=np.array([1,2,3,4],dtype=float) ```
45
How to get the total row & columns of array
``` array.shape ```
46
What is the output of ? ``` arr = np.array([1,2,3,4,5,6,7]) print(arr[2:5]) ```
``` [3, 4, 5] ```
47
How to compared to arrays for equality using operators ?
``` # 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
48
How to perform less than operation on to arrays ?
``` # 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
49
How to perform equality operation by using an inbuilt method ?
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
What is the output of array_equal() method ?
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
How to perform logical or operation on two arrays?
``` # 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
How to perform logical and operation on two arrays ?
``` # 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
How to perform logical xor operation on two arrays ?
``` # 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
How to perform logical not operation on a array ?
``` # 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
How to add two arrays ?
``` # 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
How to convert negative value to positive of an array?
``` #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
How to print array value to the power of?
``` # 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
How to find some of all values of array ?
``` # 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
How to get product of each value of array ?
``` # Get Product of array values a1=np.array([3,6,1,2,8,9,1]) print(np.multiply.reduce(a1)) # Output: 2592 ```
60
How to do accumulate multification of array ?
``` # Multiply Accumulate a1=np.array([3,6,8,7,9,2]) print(np.multiply.accumulate(a1)) #Output : [ 3 18 144 1008 9072 18144] ```
61
How to do accumulate addition of array ?
``` # Multiply Accumulate a1=np.array([3,6,8,7,9,2]) print(np.multiply.accumulate(a1)) #Output : [ 3 18 144 1008 9072 18144] ```
62
How to find a maximum Valueof an array ?
``` # Find Maximum Value of Array a1=np.array([3,6,8,7,9,2]) print(np.max(a1)) # Output: 9 ```
63
How to find minimum value of an array ?
``` a1=np.array([3,6,8,7,9,2]) print(np.min(a1)) #Output: 2 ```
64
How to get Index value of minimum value of an array ?
``` # 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
How to How to get index value of maximum value of an array ?
``` # 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
How to find square root of array values ?
``` # 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
How to open new values to an existing array ?
``` #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] ```