day 3, while I still can't understand how I can slice an array.

day 3, while I still can't understand how I can slice an array.

in the end i give you illustration image.

First, I wrote this note for myself on data camp workplace, then, recently i decided to share this note with everyone. note, you can comment if you can't understand specific topics in this article. i hope this article clarifies what is slice, especially with 2D array.

this article is part of my series called Data science journey, actually this third article in my series. i decided to write everything i figure out in the field until I get a paid job in the future, forgive me if write silly sentences that might be confusing, also feel free to correct me if I'm wrong.

install numpy

pip install numpy

Importing numpy

import numpy as np
arra_list=[2,1,3,5,6,7,8,9,10]
print(arra_list)

difference between lists and NumPy

list is a data type in Python to store data as rows, some times you get confusion about why i have to use NumPy instead of the list. actually, there are many reasons to use NumPy.

  • Lists: Lists can contain elements of different data types. For example, a list can contain integers, strings, floats, or even other lists.

  • NumPy arrays: NumPy arrays are homogeneous, meaning they can only contain elements of the same data type.

  • This homogeneity allows for more efficient storage and operations on the data.

what does homogeneous mean in programming?

"homo-" means same, "hetero-" means different.

In this case in particular they mean that a single numpy array can only store one type, say, only numbers, or only strings.

import numpy as np
array=np.array([1,2,3,4,"a"]).dtype.type
print(array)

here I create a NumPy array and give it 4 numbers and one letter, array automatically changes the array's data to string because numpy is homogenous.

Numpy Datatype

NumPy data types are used to represent the different kinds of data that can be stored in an array.

So pay attention to your data in the numpy array cause it can easily change without notice.

Python Vs NumPy data type

  • Python has datatype such as

    • float

    • Int

    • string

    • etc...

  • NumPy, is more specific than Python, cause you can control the amount of memory in your array

    • float 32

    • float 64

    • int 32

    • int 64

    • etc...

      you can check the datatype of your numpy array by using the .dtype.

        import numpy as np
        array=np.array([1,2,3,4,5,5]).dtype
        print(array)
      

the output of the code above = int64 as you will see.

NumPy index and slicing

  • NumPy is a zero-based index, which the first element is assigned the index 0

  • in a 2D array, you should give the array Row, and column index in order to return the single value

import numpy as np

#create NumPy array
arr = np.arange(24).reshape(6,4)

#view NumPy array
print(arr)
print(arr[1:3,1:4])

index in NumPy a little bit hard, but make sure you understand the structure of what is inside of the brackets!

1D array

  • if the array is 1D array you have to get it the single number, that can index the value in array import numpy as np arr = np.array([1,2,3]) print(arr[1]) So, it will return the second data cause Numpy Array is zero based index "start with zero index" it mean 0 = first data

this image above can reveal that you can add 3 values separated by colon.

  import numpy as np
  arr = np.array([1,2,3,4,5,6])
  print(arr[1:6:1])
  • first number: start index

  • second number: end index

  • third number: let's break down here and explain something that might be important. For the third number you haven't to write, it's ok to write start and end numbers and it still works. So the third number is step size by default it's 1

2D array

the trick here is, you have to deal with two different dimensional like Row and column. The 2D array is the hardest topic in an array I can't understand, and trigger me to write this note.

if the array is 2D array, and if you give the array a single number as an index it will return the ROW. but why?

in 2D array you have a Row and column, by this sequence, if you give the array a single index, automatically it understands that you index on the ROW, and it will give you back the whole ROW let's try this code below

we created the numpy array and try to give us the second ROW

  import numpy as np

  #create NumPy array
  arr = np.arange(24).reshape(6,4)

  #view NumPy array
  print(arr)
  print(arr[1])

the output from the code above!

[4 5 6 7]

the bridge, in 2D array you need to write 2 value in the index

  1. ROW

  2. column

and each value of above value you can slice as 1D array.

see this image below!

conclusion

in 1D array you have to give the array 2 values separated by :. because it's one-dimensional.

in a 2D array, you have to give the array two values separated by ,.

in the first value return row and you can slice it to return a specific value.

second, the second value is related to the column, so you can slice this column to return a specific value.


thanks for reading this note, actually this note was trying for me to clarify the index for myself, then I decide to publish it in Youth Loves Egypt

that's me Mohammed sharaki on LinkedIn: https://www.linkedin.com/in/moammed-sharaki-66a2371b0/