Basic concept of NumPy

Vandana Srivastava
3 min readJul 5, 2024

Before starting to NumPy lets learn some basic concepts of NumPy

NumPy data types:

  • i — -> integer(int8, int16, int32, int64)
  • b → boolean (True,False)
  • u — → unsigned integer (uint8, uint16, uint32, uint64)
  • f — → float ( float16, float32, float64)
  • c → complex (complex64, complex128)
  • s — -> String
  • U — -> Unicode string
  • M — -> date time
  • O — → object
  • m — — -> timedelta

etc….

Array Attributes:

  • a.ndim — → return the dimension of array
  • a.shape — → return the shape of the array (m,) for 1-D array . (m,n) for 2-D array. (i,m,n) for 3-D array
  • a.size — -> to get total number of elements
  • a.dtype → to get datatype of an array
  • a.itemsize → each element going to occupy how much memory. Means number of bytes.

Example-

Arithmetic operations:

As we all know arithmetic operation are —> +, -, *, /, //, %, **.

  1. Addition + ⇒ 10+20 = 30. with add function ==> np.add(10,20)
  2. Subtraction - ⇒ 20-10 =10. with subtract function ==> np.subtract(10,20)
  3. Multiplication * ⇒ 20*10 = 200. with multiply function ==> np.multiply(10,20)
  4. Modulo % ⇒ 20%10 = 0 (remainder). with mod function ==> np.mod(10,20)
  5. Power or Exponential ** ⇒ 10**2 = 100. with power function ==> np.power(10,20)
  6. Divide / ⇒ 20/10 = 2.0 (always returns float value only). with division function ==> np.divide(10,20)
  7. Floor Divide // ⇒ (if both argument is int type then return int value, if both are float then return float value). Below are some examples.

with floor_divide function ==>

np.floor_divide(10,20)

20//10 = 2

10//3 = 3

10.0 // 3 = 3.333

All these operations will be performed at element level. That means all this operation will be performed at every element of an array.

Eg-

a= np.array([10,20,30,40])
a+2 

or

np.add(a,20)

Output = array([12,22,32,42])

Universal functions(ufunc):

Arithmetic operations functions are called Universal functions(ufunc) because they operate element by element on the whole array. Hence all the above functions are ufuncs.

  1. ) Array with Scalar: These are constant numeric values. All universal functions(ufunc) are Array with Scalar because all these operations will be performed at element level.

2.) Zero division Error:

10/2 ===> will show Zero division Error in python

But in Numpy we don’t get Zero Division Error, it will return Infinity (inf)

10/0 ⇒ inf

0/0 ⇒Undefined (nan)

nan means not a number.

2.) Array with Array:

If 2 arrays are not same in size, dimension and shape then on arithmetic operation it will throw an error.

In below example- the dimension of both the arrays are same but the shape is different thus throwing an error.

That’s it for now, let’s deep dive into NumPy in my upcoming blogs.

And can learn more from my Github profile:

Also, Matplotlib learnings from github:

Pandas Learnings from github:

Check some of my other blogs in this series:

Happy Learning:)

--

--

No responses yet