Python for machine learning: numPy – Part 1

In this post, we will look into the NumPy library in python. NumPy is a powerful python library which adds multidimensional array support and functions to manipulate the arrays to python. In this post, we will look at some basic things about numPy. You can learn more about numPy from here. Since we are dealing with some basic machine learning I won’t go deep into numPy.

NumPy library is well documented and very easy to understand with many examples. So it is better to look at the documentation.

Let us see how we can create a basic numPy array. This post is organized as follows

Part 1: (this post)

  1. Basic 1-D array creation
  2. 2D and 3D array creation

Part 2 : (next post)

  1. Functions for creating arrays
  2. Visualization (using matplotlib library)
  3. Indexing and slicing

If you install anaconda distribution then numPy is already preinstalled otherwise please look at numPy installation guide it is very easy to install using PIP.

Before we proceed to array creation we need to import numPy using

import numpy as np

Basic 1-D array creation

a 1-D array is a simple array with only one dimension. I’ve given the basic code to create a 1-D array

a = np.array([0, 1, 2, 3])
print(a)

The output will be

[0 1 2 3]

From this post onward i will not be including the examples by writing it separately as code and output instead i will present the above example as follows

>>> import numpy as np
>>> a = np.array([0, 1, 2, 3])
>>> print(a)
[0 1 2 3]

if you see the above example you can see the lines that have “>>>” are the code that we write and the lines without “>>>” is the output that we get when we execute the code.

Now lets see another example of 1-D array with words

>>> import numpy as np
>>> a = np.array(['hello','world','how', 'are','you'])
>>> print(a)
['hello' 'world' 'how' 'are' 'you']

if you see above we can create a word array also with numPy but for this topic we will stick to numbers.

we will see some basic functions to view the size and dimension of the array.

>>> import numpy as np
>>> a = np.array([0,1,2,3,4])
>>> print(a)
[0 1 2 3 4]
>>> a.size
5
>>> a.ndim
1
>>> len(a)
5

The size function will give the size of the array. The ndim function will give the dimension of the array. The length for the 1-D array will return the size of the array.

2D and 3D array creation

The 2D and 3D array unleashes the power of numPy. Many datasets will often include many dimensions so it is best to learn how to create multi dimension array. To see how we can create 2D array below

>>> b = np.array([[0, 1, 2], [3, 4, 5]])
>>> b
array([[0, 1, 2],
       [3, 4, 5]])

Now we will see some basic function in numPy. it is similar to the 1D array but the output will be somewhat different.

>>> b.ndim
2
>>> b.shape
(2, 3)
>>> len(b)
2

As you can see above the functions are very similar to 1D array.

We will now create 3D array and see how it is different

>>> c = np.array([[[1], [2]], [[3], [4]]])
>>> c
array([[[1],
        [2]],

       [[3],
        [4]]])
>>> c.shape
(2, 2, 1)

Now we know how to create array and some basic function to explore the dimension of it. In the next post we will see some functions for creating numPy array, visualizing the data in the numPy and Indexing and slicing of it.

Happy Coding

Python வழி Machine Learning தமிழில் – Collection

அனைவருக்கும் வணக்கம் இந்த post இல் python இல் உள்ள Collections பற்றி பார்போம். python இல் நான்கு வகையான collections உள்ளது.

  1. List
  2. Tuple
  3. Set
  4. Dictionary

நாம் இந்த நான்கு collections பற்றியும் கீழே விரிவாக பார்போம்.

List

list என்பது python இல் உள்ள ஒரு வகை collection ஆகும் இது வரிசையாகவும் மற்றும்  மாற்றக்கூடியதாகவும் இருக்கும். list இன் உதாரணம் கீழே கொடுக்கப்பட்டுள்ளது.

list ஐ குறிப்பதற்கு ” [ ] ” square brackets பயன்படுத்தபடும்.

thislist = ["apple", "banana", "cherry"]
print(thislist)

இந்த உதாரணத்திற்கு output கீழே உள்ளது போல இருக்கும்

['apple', 'banana', 'cherry']

நாம் இப்பொழுது data வை எப்படி access செய்வது என்பதை பார்க்கலாம்.

index and range of indexes

ஒரு element ஐ index பயன்படுத்தி வெளியே எடுப்பதற்க்கு கீழே ஒரு உதாரணம் கொடுக்கப்பட்டுள்ளது.

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

இந்த program banana என்ற output ஐ கொடுக்கும்.

நாம் இப்பொழுது range of indexes program ஐ பார்க்கலாம்.

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

இந்த program [‘cherry’, ‘orange’, ‘kiwi’] என்ற output ஐ கொடுக்கும்.

நான் இந்த post இல் machine learning சம்மந்தமுள்ள  முக்கியமான தலைப்புகளை மட்டுமே குறிப்பிடுகிறேன். நீங்கள் python முழுமையாக படிக்க ஆசைப்பட்டால் வேறு இணையதளங்களை பார்க்கவும்.

Tuple

நாம் இப்பொழுது tuple பற்றி பார்க்கலாம். list மற்றும் tuple இவை இரண்டும் ஒரு இடத்தில் வேறுபடுகிறது அவை tuple இல் data வரிசையாக இருக்காது.

tuple ஐ குறிப்பதற்கு ” ( ) ” curve brackets பயன்படுத்தபடும்.

tuple இன் உதாரணம் கீழே கொடுக்கப்பட்டுள்ளது.

thistuple = ("apple", "banana", "cherry")
print(thistuple)

இந்த உதாரணத்திற்கு output கீழே உள்ளது போல இருக்கும்

(‘apple’, ‘banana’, ‘cherry’)

நாம் மேலே குறிப்பிட்டுள்ள data வை வெளியே எடுக்கும் முறைகள் tuple லுக்கும் பொருந்தும்.

Sets

நாம் அடுத்து sets பற்றி பார்க்கலாம்.

set என்பது வரிசையில்லாமலும்(unordered) மற்றும் குறிப்பு(index) இல்லாமலும் இருக்கும்.

set  ஐ குறிப்பதற்கு ” { } ” curly brackets பயன்படுத்தபடும்.

Set இன் உதாரணம் கீழே கொடுக்கப்பட்டுள்ளது

thisset = {"apple", "banana", "cherry"}
print(thisset) 

இந்த உதாரணத்திற்கு output list மற்றும் tuple இன் output போல இருக்கும்.

Sets இல் குறிப்பு(index ) இல்லாததால் நாம் data வை வெளியே எடுக்க loop செய்து வெளியே எடுக்க வேண்டும். நாம் ஒரு உதாரணத்தை பார்ப்போம்.

thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x) 

இந்த உதாரணத்திற்கு output கீழே கொடுக்கப்பட்டுள்ளது

cherry
apple
banana

Dictionary

நாம் இந்த post இல் கடைசியாக பார்க்கபோவது dictionary. dictionary இல் data வரிசையில்லாமலும், குறிப்புடனும் (indexed) இருக்கும்.

Dictionary  ஐ குறிப்பதற்கு ” { } ” curly brackets பயன்படுத்தபடும். ஆனால் dictionary இல் key மற்றும் values இருக்கும்.

Dictionary  இன் உதாரணம் கீழே கொடுக்கப்பட்டுள்ளது

thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)

இந்த உதாரணத்திற்கு output கீழே கொடுக்கப்பட்டுள்ளது

{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 1964}

நாம் இப்பொழுது Dictionary இல் உள்ள Data வை எப்படி வெளியே எடுப்பது என்பதை பார்க்கலாம்.

நாம் Dictionary இல் இருந்து data வை key பயன்படுத்தி எடுக்கலாம். இதற்கு உதாரணம் கீழே கொடுக்கப்பட்டுள்ளது

x = thisdict["model"]

இந்த உதாரணத்திற்கு output கீழே கொடுக்கப்பட்டுள்ளது

“Mustang”

நாம் Dictionary இல் இருந்து Data வை எப்படி பாற்றுவது என்பதை பார்க்கலாம். இதற்கு உதாரணம் கீழே கொடுக்கப்பட்டுள்ளது

thisdict["year"] = 2018

இப்பொழுது year, 2018 என்று மாறியிருக்கும்.

நாம் இதில் இதற்குமேல் கவனம் செலுத்த தேவையில்லை ஏன் என்றால் நாம் machine learning program களில் numPy மற்றும் pandas dataframe பயன்படுத்துவோம் ஆனால் python collection பற்றி ஒரு அடிப்படை புரிதல் முக்கியம் அதனால் இந்த post முக்கியம் என்று நான் கருதிக்கிறேன். சரி நாம் அடுத்த post இல் numPy பற்றி பார்போம்.

வணக்கம் நண்பர்களே.