CS Electrical And Electronics
@cselectricalandelectronics
All PostsData ScienceDeep Learning, Machine Learning, & Artificial IntelligenceProgrammingPython

Python Programming For Data Science And Machine Learning

Hello guys, welcome back to my blog. In this article, I will discuss python programming for data science and machine learning, I will share code from basic concepts to advance.

If you have any electrical, electronics, and computer science doubts, then ask questions. You can also catch me on Instagram – CS Electrical & Electronics.

Also, read:

Python Programming For Data Science And Machine Learning

#Data Types

Number: int, float, complex
Text: str
Boolean: bool
Sequence: list, tuple, range
Set: set, frozenset
Mapping: dict
Binary: bytes, memoryview
#Command to print hello world

print("!!!Hello World")
!!!Hello World
#Command to print world in next line

print("Hello\nWorld")
Hello
World
#Code to store string value and display it

strworld = "Dear Chetan"
print(strworld)
Dear Chetan
#Command to know type

type(strworld)
str
#BEDMAS

print(2 - 4 * 2 + 9 / 10)
-5.1
#Arthematic operation

a = 9
b = 5
print(a + b)
print(a - b)
print(a / b)
print(a // b) #Quotient
print(a ** b) #Exponential
print(a % b) #Gives remainder
14
4
1.8
1
59049
4
#Increment and decrement of number
inc = 8
inc += 1
print(inc)
dec = 8
dec -= 1
print(dec)
9
7
#float type checking

num = 30.3
print(num)
type(num)
30.3
float
#Complex number

com = 3 + 8j
print(com)
type(com)
(3+8j)
complex
#Logical operation

print(13<25)
print(True and False)
#It will print true if both are True
print(True or False)
#It will print true if any one is True
True
False
True
#Text conversion from upper case to lower or vise-versa

sentence = "i am lower case now"
print(sentence.upper())

sentence = "I AM UPPER CASE NOW"
print(sentence.lower())
I AM LOWER CASE NOW
i am upper case now
#right strip sentence

strip_sentence = "There is a space at the end       "
print(strip_sentence)
print(strip_sentence.rstrip())
#rstrip will delete the space
There is a space at the center end       
There is a space at the center end
#left strip sentence

strip_sentence = "     There is a space at the Beginning"
print(strip_sentence)
print(strip_sentence.lstrip())
     There is a space at the Beginning
There is a space at the Beginning
#Strip all or delete all space

chetan = "     Shidling     "
print(chetan)
print(chetan.strip())
     Shidling     
Shidling
#Strip which is mentioned

percent = "555%"
print(percent.rstrip("%"))
555
#mixed strip

mixedstr = "*****SSSSSS######"
print(mixedstr.rstrip("#").lstrip("*"))
#rstrip means strip right side
#lstrip means strip left side
SSSSSS
#Sentence operation

mixedstr = "20 people affected by Carona"
no_of_people = mixedstr[0:2]
print("Number of people are :", no_of_people)
Number of people are : 20
#rest of string

Rest_of_string = mixedstr[2:]
print(Rest_of_string)
 people affected by Carona
groupstr = "10 Apple 5 Banansa 3 Orage"
fruit = groupstr[19 :27]
print(fruit)
3 Orage
print(groupstr[:-2])
10 Apple 5 Banansa 3 Ora
#Find even or odd
#even
nums_seq = "123456789"
even_nums = nums_seq[1::2]
print(even_nums)
2468
#Find odd

odd_nums = nums_seq[0::2]
print(odd_nums)
13579
odd_nums_again = nums_seq[ ::2]
print(odd_nums_again)
13579
#Print first name and last name

first_name = "Chetan"
last_name = "Shidling"
name = first_name + " " + last_name
print(name)
Chetan Shidling
#My age

my_age = input("Enter my age :")
my_age_sen = ("I am " + my_age +" years old")
print(my_age_sen)
Enter my age :21
I am 21 years old
#Scentence formation

A = "Data"
B = "Science"
C = "Python"
print("{0} {1} using {2}".format(A, B, C))
Data Science using Python
#Arrays
#How to create list

create_list = []
print(create_list)
type(create_list)
[]
list
#Another way using constructor list()

list_1 = list()
type(list_1)
list
Methods performed on lists

#append() - adds an element at the end of the list
#insert() - adds an element at the specified position
#extend() - adds the elements of a list ( or any iterable), to the end of the current list
#copy() - returns a copy of the list
#count() - retuns the number of elements with the specified value
#clear() - removes all the elements from the list
#index() - returns the index of the first element with the specified value
#remove() - removes the item with the specified value
#pop() - removes the order of the list
#reverse() - reverses the order of the list
#sort() - sorts the list
#Data Structures

fruits = ['Orange', 'Apple', 'Pear', 'Banana', 'Kiwi', 'Apple', 'Banana']

#How to find count of elements inside a list

fruits.count('Apple')
2
#How to find index

fruits.index('Banana')
fruits.index('Banana', 4)#It will find banana index starting a position 4
6
#How to reverse

fruits.reverse()
fruits
['Banana', 'Apple', 'Kiwi', 'Banana', 'Pear', 'Apple', 'Orange']
#How to append

fruits.append('Grape')
fruits
['Banana', 'Apple', 'Kiwi', 'Banana', 'Pear', 'Apple', 'Orange', 'Grape']
#How to sort

fruits.sort()
fruits
['Apple', 'Apple', 'Banana', 'Banana', 'Grape', 'Kiwi', 'Orange', 'Pear']
#How to pop or delete element

fruits.pop()
fruits
['Apple', 'Apple', 'Banana', 'Banana', 'Grape', 'Kiwi', 'Orange']
#How to print particular element

print(fruits[0])
print(fruits[4])
Apple
Grape
#How to print elements from range(this to this)

print(fruits[2:5])
['Banana', 'Banana', 'Grape']
#How to print last element

print(fruits[-1])
Orange
#How to print elements from particular point

print(fruits[3:])
['Banana', 'Grape', 'Kiwi', 'Orange']
#How to print elements from particular point

print(fruits[:-3])
['Apple', 'Apple', 'Banana', 'Banana']
#How to copy all elemets and insert new element

new_fruits = fruits
new_fruits.append('gauva')
new_fruits
['Apple', 'Apple', 'Banana', 'Banana', 'Grape', 'Kiwi', 'Orange', 'gauva']
#How to find id

print(id(new_fruits))
1481053139080
#Sentence into words in sentence

a_sentence = "Corona virus is spreading all over the world"
words_in_sentence = a_sentence.split()
print(words_in_sentence)
['Corona', 'virus', 'is', 'spreading', 'all', 'over', 'the', 'world']
#mutli sentence

multi_sentence = "I am Chetan. I have a pet dog. It's color is white and black, but it looks big."
sentence_inText = multi_sentence.split('.')
print(sentence_inText)
['I am Chetan', ' I have a pet dog', " It's color is white and black, but it looks big", '']
#Inser $ between all character

join_sentence = '$'.join(multi_sentence)
print(join_sentence)
I$ $a$m$ $C$h$e$t$a$n$.$ $I$ $h$a$v$e$ $a$ $p$e$t$ $d$o$g$.$ $I$t$'$s$ $c$o$l$o$r$ $i$s$ $w$h$i$t$e$ $a$n$d$ $b$l$a$c$k$,$ $b$u$t$ $i$t$ $l$o$o$k$s$ $b$i$g$.
#Print sentence three time

var_list = ["My name is Chetan"]
print(var_list*3)
['My name is Chetan', 'My name is Chetan', 'My name is Chetan']
#Cancate

another_list = ["I live in Raichur"]
print(var_list + another_list)
['My name is Chetan', 'I live in Raichur']
#Find length

print(len(another_list))
1
#Find length

print(len(another_list))
print(len(fruits))
1
8
#Remove element from a list using for loop

list1 = ['Apple','Banana','Orange','Orange','Banan','Banana','Apple']
count1 = list1.count('Banana')
for i in range(0,count1):
    list1.remove('Banana')
print(list1)
['Apple', 'Orange', 'Orange', 'Banan', 'Apple']
#Find length of number list

num_list = [2, 3, 5, 1, 9, 3, 1]
print(len(num_list))
7
#How to sort numbers

print(sorted(num_list))
[1, 1, 2, 3, 3, 5, 9]
#How to find minimum value

print(min(num_list))
1
#How to find maximum value

print(max(num_list))
9
#How to create a nested list

nested_list = [[2, 5, 1],[5, 9, 3],[9, 2, 0]]
print(nested_list[1])
[5, 9, 3]
#How to print any one element is nested list

print(nested_list[0][2])
1
#How to print any few list

print(nested_list[0:2])
[[2, 5, 1], [5, 9, 3]]
#Delete particular element in the list

num_list = [-1, 3, 4, 1, 5, 6, 9, 22]
del num_list[2]
print(num_list)
[-1, 3, 1, 5, 6, 9, 22]
#Tuple - They are immutable and usually contain a heterogeneous sequence of elements

sample_tuple = 23433, 84392, "Friends"
type(sample_tuple)
tuple
print(sample_tuple)
(23433, 84392, 'Friends')
#Reverse packing

x,y,z = smaple_tuple
print(x)
print(y)
print(z)
23433
84392
Friends
#Empty tuples are contructed by an empty pair of parentheses
#A tuple with one item is constructed by following a value with a comma
#It is not sufficient to enclose a single value in parenthese. Ugly, but effective

empty_tuple = ()
single_tuple = 'Dear',
print(single_tuple)
type(single_tuple)
('Dear',)
tuple
#Now let's see difference between list and tuple

list_1 = [12, 45, 55]
print(list_1)
print("\n")
tuple_1 = tuple(list_1)
print(tuple_1)
type(tuple_1)
[12, 45, 55]

(12, 45, 55)
tuple
#Tuple 

tuple_2 = ("Chetan","Nitin",23,"Ratan",44)
type(tuple_2)
tuple
print(tuple_2)
('Chetan', 'Nitin', 23, 'Ratan', 44)
print(tuple_2[0])
Chetan
#nested tuple

nested_tuples = tuple_1 , (1, 2, 5, 2)
print(nested_tuples)
((12, 45, 55), (1, 2, 5, 2))
#Nested tuples but with mutable objects

tuple_4 = ([1,2,3],[3,2,4])
print(tuple_4)
type(tuple_4)
([1, 2, 3], [3, 2, 4])
tuple
#Set is an unordered collection with no duplicate elements
#Curly braces or the set() function can be used to create sets
#How to create a set and find type

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana','watermelon'}
print(basket)
type(basket)
{'banana', 'orange', 'apple', 'pear', 'watermelon'}
set
'orange' in basket
True
#How to convert list into set

num_list_1 = [1,2,3,4,5,6]
num_set_1 = set(num_list_1)
print(num_set_1)
type(num_set_1)
{1, 2, 3, 4, 5, 6}
set
#Let's print list

num_list_2 = [3,4,2,1,4,8]
num_set_2 = set(num_list_2)
print(num_list_2)
type(num_set_2)
[3, 4, 2, 1, 4, 8]
set
num_set_1 - num_set_2
print(num_set_1)
{1, 2, 3, 4, 5, 6}
num_set_1 | num_set_2
{1, 2, 3, 4, 5, 6, 8}
#It give you common element

num_set_1 & num_set_2
{1, 2, 3, 4}
num_set_1 ^ num_set_2
{5, 6, 8}
print(num_set_1.intersection(num_set_2))
{1, 2, 3, 4}
print(num_set_1.union(num_set_2))
{1, 2, 3, 4, 5, 6, 8}
print(num_set_1)
print(num_set_2)
print(num_set_2.difference(num_set_1))
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 8}
{8}
#Dictionaries - A set of key:value pairs - with the requirement that the keys are unique (within one dictionary)
#Use {} to create dictionary
#Use 'dict()' to create dictionary
#dictionaries are indexed by keys, which can be immutable
empty_dictionary = {}
print(empty_dictionary)
type(empty_dictionary)
{}
dict
#Storing elements in dictionary

diction_data = {'Name':'Chetan', 'Age':21, 'Height':'5.9 ft', 'Hobby':'Blogging'}
print(diction_data)
type(diction_data)
{'Name': 'Chetan', 'Age': 21, 'Height': '5.9 ft', 'Hobby': 'Blogging'}
dict
#Print list data

list(diction_data)
['Name', 'Age', 'Height', 'Hobby']
diction_hobby = diction_data['Hobby']
print(diction_hobby)
Blogging
diction_age = diction_data['Age']
print(diction_age)
21
diction_data['Age'] = 19
print(diction_data)
{'Name': 'Chetan', 'Age': 19, 'Height': '5.9 ft', 'Hobby': 'Blogging'}
#Adding new key and its value

diction_data['Profession'] = 'Engineer'
print(diction_data)
{'Name': 'Chetan', 'Age': 19, 'Height': '5.9 ft', 'Hobby': 'Blogging', 'Profession': 'Engineer'}
#Lets check

'Profession' in diction_data
True
#How to get list of keys in one shot

print(list(diction_data.keys()))

#How to get list of values in one shot
print(list(diction_data.values()))
['Name', 'Age', 'Height', 'Hobby', 'Profession']
['Chetan', 19, '5.9 ft', 'Blogging', 'Engineer']
new_diction = dict([('Ratan',3000),('Nitin',3000),('Krishna',4000)])
new_diction
{'Ratan': 3000, 'Nitin': 3000, 'Krishna': 4000}
diction_data.update(new_diction)
print(diction_data)
{'Name': 'Chetan', 'Age': 19, 'Height': '5.9 ft', 'Hobby': 'Blogging', 'Profession': 'Engineer', 'Ratan': 3000, 'Nitin': 3000, 'Krishna': 4000}
#How to delete data

del diction_data['Krishna']
print(diction_data)
len(diction_data)
{'Name': 'Chetan', 'Age': 19, 'Height': '5.9 ft', 'Hobby': 'Blogging', 'Profession': 'Engineer', 'Ratan': 3000, 'Nitin': 3000}
7
#Lets do something more

students_data = dict([('Kiran',22),('Akshay',24),('Raj',35)])
print(students_data)
{'Kiran': 22, 'Akshay': 24, 'Raj': 35}
students_data[2] = "Krishna",23
print(students_data)
{'Kiran': 22, 'Akshay': 24, 'Raj': 35, 2: ('Krishna', 23)}
#Functions

def display():
    print("Hello World")
    
display()
Hello World
def square(num):
    out = num**2
    return(out)

sq_r = square(3)
print(sq_r)
9
#Find factorial

def factorial(n):
    if n>1:
        return n*factorial(n-1)
    else:
        return n
    
fact = factorial(6)
print(fact)
720
#Addition of argument

def addition(*args):
    print(args)
    return(sum(args))

print(addition(4,6,2,7,3,11))
print(addition(1,2))
(4, 6, 2, 7, 3, 11)
33
(1, 2)
3
#Conditional statements

score = int(input("Enter a number :"))

if score < 0:
    print("Enter positive number")
elif score == 0:
    print("You scored Zeo")
elif score == 1:
    print("You scored one")
else:
    print("It is above one")
Enter a number :2
It is above one
#Multiple if statement

score = int(input("Please enter your score :"))
cutoff = 75
pass_marks = 35

if score >= 75:
    print("You got distinction")
    
if score >= 35:
    print("You passed")
    
if score < 35:
    print("You failed")
Please enter your score :45
You passed
#If else

score = int(input("Enter you score"))

if score >=35:
    print("You passed")
else:
    print("You failed")
Enter you score12
You failed
#With strings

str1 = "Chetan works at CS Electrical And Electronics"
if "Electrical" in str1:
    print("It is there")
else:
    print("Not found")
    print("\n")
    
str2 = "World is very big and lovely"
if "verymuch" in str2:
    print("It is found in str2")
else:
    print("Not found") 
It is there
Not found
if False:
    print("True for this statement")
else:
    print("False for this statement")
False for this statement
if True:
    print("True for this statement")
else:
    print("False for this statement")
True for this statement
#Loops

fruits = ['Apple','Orange','Banana']
for i in fruits:
    print(i, len(i))
Apple 5
Orange 6
Banana 6
#Using range function, loop through...

for w in range(6, 15):
    print(w)
6
7
8
9
10
11
12
13
14
#Range function with increment

for i in range(10,20,2):
    print(i)
10
12
14
16
18
#Looping on a string

string = "Jai Shri Ram"

for alphabet in string:
    print(alphabet)
J
a
i
 
S
h
r
i
 
R
a
m
#Triangle

str1 = ' '
for i in range(0,9):
    if i<5:
        str1 += '* '
        print(str1)
    elif i>4:
        str1 = str1[:-2]
        print(str1)
 * 
 * * 
 * * * 
 * * * * 
 * * * * * 
 * * * * 
 * * * 
 * * 
 *
#Find count in statement

my_string = "Count the statement alpabets"

for n,alphabet in enumerate(my_string):
    print(alphabet, n)
C 0
o 1
u 2
n 3
t 4
  5
t 6
h 7
e 8
  9
s 10
t 11
a 12
t 13
e 14
m 15
e 16
n 17
t 18
  19
a 20
l 21
p 22
a 23
b 24
e 25
t 26
s 27
#Find the vowels

vowels =' '
for alphabet in my_string:
    if alphabet in 'aeiou':
        vowels += ' ' + alphabet
print(vowels)
  o u e a e e a a e
#Find the even and odd numbers

num = '0123456789'
even = ' '
odd = ' '
for number in num:
    if int(number)%2 == 0:
        even += number
    else:
        odd += number
        
print('All evens are : ' + even + ' & All odds are : '+odd)
All evens are :  02468 & All odds are :  13579
#Sentence check

str1 = "#Chetan #Shidling is a #Entrepreneur"

for word in str1.split():
    if word.startswith('#'):
        print(word[1:])
Chetan
Shidling
Entrepreneur
#Dictionary

students = {1:['Chetan',21], 2:['Nitin',19], 3:['Krish', 22]}
type(students)
dict
for key, val in students.items():
    print(key, val)
1 ['Chetan', 21]
2 ['Nitin', 19]
3 ['Krish', 22]
for key in students.keys():
    print(key)
1
2
3
for val in students.values():
    print(val)
['Chetan', 21]
['Nitin', 19]
['Krish', 22]
#While loop for fibonacci series

a, b = 0, 1
n = 9
while a < n:
    print(a, end=' ')
    a, b = b, a+b
0 1 1 2 3 5 8 
#NumPy - NUMerical PYthon

#It is used for scientific computing and data analysis

#Power of NumPy is N-dimensional array object which is in the form of rows and columns.

#Fast
#Less memory
#Convenient
#Vectorized code 
#How to import the numpy library

import numpy as np

#np is an alias, you may use any other alias, though np is standard in the industry
#Create 1-D array using list/tuple
#use np.array()

fromList_to_1dArray = np.array([10,20,30,40,50])
fromTuple_to_1dArray = np.array((1,2,3,4,5))

print(type(fromList_to_1dArray))
print(fromList_to_1dArray)
print("\n")
print(type(fromTuple_to_1dArray))
print(fromTuple_to_1dArray)
<class 'numpy.ndarray'>
[10 20 30 40 50]


<class 'numpy.ndarray'>
[1 2 3 4 5]
#2-D array creation using two lists

fromList_to_2dArray = np.array([[0,1,2,3,4],[5,6,7,8,9]])
print(fromList_to_2dArray)
[[0 1 2 3 4]
 [5 6 7 8 9]]
#Let us see how Numpy works when compare to standard python
#Sum of two list elements

num_list1 = [1,2,3,4,5,6,7,8,9]
num_list2 = [10,11,12,13,14,15,16,17,18]

sum_list = list(map(lambda x, y: x+y, num_list1, num_list2))
print(sum_list)
[11, 13, 15, 17, 19, 21, 23, 25, 27]
#Use numpy arrays
#1D Array
import numpy as np
np_list1 = np.array(num_list1)
np_list2 = np.array([10,11,12,13,14,15,16,17,18])

np_sumLst = np_list1+np_list2
print(np_sumLst)
print(type(np_sumLst))
[11 13 15 17 19 21 23 25 27]
<class 'numpy.ndarray'>
#Creation of arrays of fixed size

#from list
array_from_list = np.array([1,2,3,4,5])
array_from_list2 = np.array([1,2,3,4,5],dtype=float)
#from tuple
array_from_tuple = np.array((0,6,7,8,9))

print(array_from_list)
print(array_from_list2)
print(array_from_tuple)
[1 2 3 4 5]
[1. 2. 3. 4. 5.]
[0 6 7 8 9]
#Condition statement as an argument to np.array()

array_from_list3 = array_from_list >= 2
print(array_from_list3)
[False  True  True  True  True]
#How to fetch the actual elements

array_from_list[array_from_list3]
array([2, 3, 4, 5])
#2D Array

array1 = np.array([[0,1,2,3],[4,5,6,7]])
print(array1)

array2 = np.array([[10,11,12,13],[14,15,16,17]])
array2
[[0 1 2 3]
 [4 5 6 7]]
array([[10, 11, 12, 13],
       [14, 15, 16, 17]])
#Subtraction of two arrays

array2 - array1
array([[10, 10, 10, 10],
       [10, 10, 10, 10]])
###How to initialize np arrays when size is known

#Functions that are used to do so are:

#np.arange(): Array creation with defined increments
#np.zeros(): Array of 0s
#np.ones(): Array of 1s
#np.random.random(): Array of random numbers
#np.random.randint(): Random array of integers within a particular range
#np.linspace(): Array of fixed length
#np.full(): Constant array of any number 'n'
#np.eye(): Identity matrix array
#np.title(): Array is going to be created many times
#np.arange()
#range()

increment = np.arange(1,15,2)
increment
array([ 1,  3,  5,  7,  9, 11, 13])
decrement = np.arange(50,10,-10)
decrement
array([50, 40, 30, 20])
#Let us test them and see how they work
#Zeros initialization

np.zeros(5)
np.zeros((5,5))
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
#Ones initialization

np.ones(4)
np.ones((4,4))
np.ones((4,4), dtype = int)
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
#random numbers Array


np.random.random([3,3])
array([[0.97565383, 0.59839634, 0.81305042],
       [0.66541746, 0.99020817, 0.47314968],
       [0.08517457, 0.05256615, 0.10333111]])
#Create a 3 * 3 random array of integers ranging from 0 to 9

np.random.randint(1,10,(3,3))
array([[5, 6, 1],
       [7, 5, 2],
       [5, 4, 7]])
#np.full() - default data type int

np.full(2,5)
np.full((2,3),5)
array([[5, 5, 5],
       [5, 5, 5]])
#Identity matrix

i_matrix = np.eye(5, dtype = int)
i_matrix
array([[1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0],
       [0, 0, 0, 0, 1]])
#np.linspace()
#Lenght known, step size unknown

np.linspace(1,10,5)
array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ])
#Repeating array for number of times - default data type is int

array1 = ([5,4,3])
np.tile(array1,5)
np.tile(array1, (4,5))
array([[5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3],
       [5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3],
       [5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3],
       [5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3, 5, 4, 3]])
#Generate an array (5 * 5)
import numpy as np
array1 = np.ones((5,5))
array1
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
#Find dtype, shape, itemsize, and ndim of array1

print(array1.dtype)
print(array1.shape)
print(array1.itemsize)
print(array1.ndim)
float64
(5, 5)
8
2
#Creating a 3-d array - Difficult to print it
#reshape() simply reshapes a 1-D array

array_3d = np.arange(12).reshape(2,3,2)
print(array_3d)
[[[ 0  1]
  [ 2  3]
  [ 4  5]]

 [[ 6  7]
  [ 8  9]
  [10 11]]]
#Indexing, Subseting, Slicing and iterating through Arrays
#Indexing and slicing one dimensional arrays

array1d = np.arange(5)
print(array1d)
[0 1 2 3 4]
#Access thro indexing

array1d[2]
array1d[2:3]
array([2])
#Read all elements

array1d[[2,3,4]]
array([2, 3, 4])
#Subset with an increment of 2

print(array1d[0::2])
[0 2 4]
#Iterations are also similar to lists

for i in array1d:
    print(i*2)
0
2
4
6
8
#2 D array accessing

array2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(array2d)
print(array2d.ndim)
print(array2d[1,3])
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
2
8
print(array2d[2,:])
#Slicing all colums
print(type(array2d[2,:]))
#Slicng all rows
print(array2d[:,1])
#Slicing all rows and the first 2 colums
print(array2d[:,:2])
[ 9 10 11 12]
<class 'numpy.ndarray'>
[ 2  6 10]
[[ 1  2]
 [ 5  6]
 [ 9 10]]
#Iterating over 2D/3D arrays (row wise and columwise)

for colum in array2d:
    print(colum)
[1 2 3 4]
[5 6 7 8]
[ 9 10 11 12]
#Execution speed in NumPy and Standard Python Lists

NumPy is much faster!!!
#Import time library

import time
#create two large lists
list1 = [i for i in range(200000)]
list2 = [j**2 for j in range(200000)]
#Capture start time & end time
start_time = time.time()
perform_Ops = list(map(lambda x,y: x*y, list1, list2))
Ops_time = time.time()
diff_time = Ops_time - start_time
print(diff_time)

#numpy array
array1 = np.array([i for i in range(200000)])
array2 = np.array([j**2 for j in range(200000)])
np_startTime = time.time()
array3 = array1*array2
np_endTime = time.time()
numpy_time = np_endTime - np_startTime
print(numpy_time)

print("The ratio of the time taken {}".format(numpy_time/diff_time))
0.03191232681274414
0.000997304916381836
The ratio of the time taken 0.03125140082181547
#File is a named location on a disk to store related information.
#It is used to store the data permanently in a non volatile memory
#Variables will go off
#Mantra
#Open -> R or W -> Close
#Mode

#r-Read, w-Write, a-append, r+ - read and write
#file open

#open(path,mode)
open('G:\\Chetan\\work.txt',"w")
#close()
<_io.TextIOWrapper name='G:\\Chetan\\work.txt' mode='w' encoding='cp1252'>
open('G:\\Chetan\\work.txt',"r")
<_io.TextIOWrapper name='G:\\Chetan\\work.txt' mode='r' encoding='cp1252'>
#Reading file and assigning file object

fo = open('G:\\Chetan\\work.txt',"r")

readdata = fo.read()
print(readdata)
fo.close()
Hi i am chetan
Hi i am chetan
Hi i am chetan
Hi i am chetan
Hi i am chetan
Hi i am chetan
print(type(readdata))
<class 'str'>
print(type(fo))
<class '_io.TextIOWrapper'>
#Reading file and assigning file object

fo = open('G:\\Chetan\\work.txt',"r")

readdata = fo.read(6)
print(readdata)
fo.close()
Hi i a
#Readlines

fo = open('G:\\Chetan\\work.txt',"r")

readdata = fo.readlines()
print(readdata)
fo.close()
['Hi i am chetan\n', 'Hi i am chetan\n', 'Hi i am chetan\n', 'Hi i am chetan\n', 'Hi i am chetan\n', 'Hi i am chetan\n']
#One by one

fo = open('G:\\Chetan\\work.txt',"r")
linedata = fo.readlines()

for line in linedata:
    print(line)
    
fo.close()
Hi i am chetan

Hi i am chetan

Hi i am chetan

Hi i am chetan

Hi i am chetan

Hi i am chetan
#Writing to file

fo = open('G:\\Chetan\\work.txt',"w")

fo.write("I am in Banulure\n")
fo.write("I came here to start my own company\n")

fo.close()

#Open file you will find previous data will be vanished
#append

fo = open('G:\\Chetan\\work.txt',"a")
fo.write("\nHa ha, Deleted na?\n")

fo.close()
#Ask user

fo = open('G:\\Chetan\\work.txt',"a")
message = input("\nEnter your message :")
fo.write(message)

fo.close()
Enter your message :It nice
NumPy Arrays - Few Operations

Basic mathematical operations / linear algebra operations / functions

Playing with arrays using resize / reshape / stack creation
#Playing with arrays

#import the numpy library

import numpy as np
#If the arrays don't have the same dimensions
#use resize()

sample_array = np.arange(0,11)
print(sample_array)

print(sample_array.size)

print(np.resize(sample_array, (3,4)))

print(np.resize(sample_array, (6,4)))

print(sample_array.resize(3,5))
[ 0  1  2  3  4  5  6  7  8  9 10]
11
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10  0]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10  0]
 [ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10  0  1]]
None
#Give a new shape to an array without changing its data

sample_array = np.arange(0,9).reshape(3,3)
sample_array
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
#Dimensions are automatically calculated when you use -1

smaple_array = np.arange(0, 9)
print(sample_array)
x = sample_array.reshape(3,-1)
x
[[0 1 2]
 [3 4 5]
 [6 7 8]]
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
#Transpose of a matrix

x.T
array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])
#Adding elements at the end of the array

y = np.append(sample_array, [10,11])
y
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8, 10, 11])
#Insert elements to the array at index -8

z = np.insert(sample_array,8,9)
z
array([0, 1, 2, 3, 4, 5, 6, 7, 9, 8])
#Delete element at given index array

z = np.delete(sample_array,[8])
z
array([0, 1, 2, 3, 4, 5, 6, 7])
Split/Join/Stack Arrays
np.r_() - Arrange arrays row-wise
np.c_() - Arrange arrays column-wise
np.concatenate() - Concatenate two arrays
np.hstack() - stack arrays horizontally
np.vstack() - stack arrays vertically
#Create 2d arrays

import numpy as np
array1 = np.arange(9).reshape(3,3)
array2 = np.arange(12).reshape(3,4)

print(array1)
print(array2)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
#Concatenate

array3 = np.arange(9,18).reshape(3,3)
print(np.concatenate((array1,array3)))
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]
 [15 16 17]]
#Stack the array elements horizontally

np.hstack((array1, array3))
array([[ 0,  1,  2,  9, 10, 11],
       [ 3,  4,  5, 12, 13, 14],
       [ 6,  7,  8, 15, 16, 17]])
#Stack the array elements vertically

np.vstack((array1, array3))
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])
#Stack arrays row-wise

print(np.r_[array1,array3])
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]
 [15 16 17]]
#Stack arrays column-wise

print(np.c_[array1,array3])
[[ 0  1  2  9 10 11]
 [ 3  4  5 12 13 14]
 [ 6  7  8 15 16 17]]
Basic Mathematical Operations / Linear Algebra Operations / Functions
#Applying mathematical operations

sample_array = np.arange(10,20)

#exponential, logarithms, trigometry, sine and cosine

print('\nexponential values of sample array')
print(np.exp(sample_array))
print('\nlogarithmic values of sample array')
print(np.log(sample_array))
print('\nSine values of sample array')
print(np.sin(sample_array))
print('\nCosine values of sample array')
print(np.cos(sample_array))

exponential values of sample array [2.20264658e+04 5.98741417e+04 1.62754791e+05 4.42413392e+05 1.20260428e+06 3.26901737e+06 8.88611052e+06 2.41549528e+07 6.56599691e+07 1.78482301e+08] logarithmic values of sample array [2.30258509 2.39789527 2.48490665 2.56494936 2.63905733 2.7080502 2.77258872 2.83321334 2.89037176 2.94443898] Sine values of sample array [-0.54402111 -0.99999021 -0.53657292 0.42016704 0.99060736 0.65028784 -0.28790332 -0.96139749 -0.75098725 0.14987721] Cosine values of sample array [-0.83907153 0.0044257 0.84385396 0.90744678 0.13673722 -0.75968791 -0.95765948 -0.27516334 0.66031671 0.98870462]
sample_array
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
#Executing a function in non-numpy way

non_numpy_fn = [(x**2+2*x+3) for x in sample_array]
print(non_numpy_fn)
[123, 146, 171, 198, 227, 258, 291, 326, 363, 402]
#Vertorize the function and apply an numpy array

vectorised_fn = np.vectorize(lambda x: (x**2+2*x+3))

vectorised_fn (sample_array)
array([123, 146, 171, 198, 227, 258, 291, 326, 363, 402])
Linear Algebra Operations

help(np.linelg) - Provides linear algebra documentation

np.linalg - is a package

np.linalg.inv - inverse of a matrix
np.linalg.det - Determinant of a matrix
np.linalg.eig - Eigenvalues and eigenvectors of a matrix
np.dot(a,b) - dot product of two matrices
#Creating two arrays

array1 = np.arange(5,14).reshape(3,3)
array2 = np.arange(1,13).reshape(3,4)
print(array1)
print(array2)
[[ 5  6  7]
 [ 8  9 10]
 [11 12 13]]
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
#Determinant of 3*3 matrix vs 3*4 matrix

import numpy as np
print(np.linalg.det(array1))
#np.linalg.det(array2)
#array2 is 3*4 matrix, so it will throw error
0.0
#Eligenvalues

np.linalg.eig(array1)
(array([ 2.76509717e+01, -6.50971698e-01,  6.66880826e-16]),
 array([[-0.37636677, -0.75529416,  0.40824829],
        [-0.55798202, -0.05094566, -0.81649658],
        [-0.73959727,  0.65340284,  0.40824829]]))
#dotproduct of the matrix

np.dot(array1,array2)
array([[ 98, 116, 134, 152],
       [143, 170, 197, 224],
       [188, 224, 260, 296]])
Basics of Pandas library

Used for data analysis - Data manipulation, building ML models and data visualisation

Pandas data structures - Series & Dataframes
Pandas Series

01. import pandas library
02. To create series object, use pd.series()
03. Each column in a dataframe is a pandas series
#import pandas

import pandas as pd
#Creating a character series from tuple

char_series = pd.Series(('H','A','R','I'))
char_series
0    H
1    A
2    R
3    I
dtype: object
#Creating series from list

num_series = pd.Series([3,6,9,12,15,18,21])
print(num_series)
print(type(num_series))
0     3
1     6
2     9
3    12
4    15
5    18
6    21
dtype: int64
<class 'pandas.core.series.Series'>
#Creating a datetime series

date_series = pd.date_range(start = '01-04-2020', end = '19-04-2020')
print(date_series)
type(date_series)
DatetimeIndex(['2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07',
               '2020-01-08', '2020-01-09', '2020-01-10', '2020-01-11',
               '2020-01-12', '2020-01-13',
               ...
               '2020-04-10', '2020-04-11', '2020-04-12', '2020-04-13',
               '2020-04-14', '2020-04-15', '2020-04-16', '2020-04-17',
               '2020-04-18', '2020-04-19'],
              dtype='datetime64[ns]', length=107, freq='D')
pandas.core.indexes.datetimes.DatetimeIndex
#For more information try out this

help(pd.date_range(start = '01-04-2020', end = '19-04-2020'))
#Indexing

print(num_series[4])
print(num_series[1:4])
print(num_series[[1,4]])
15
1     6
2     9
3    12
dtype: int64
1     6
4    15
dtype: int64
#Setting up indexes explicity
#Use index argument while generating series

num_series = pd.Series([3,6,9,12,15], index = ['a','b','c','d','e'])
num_series
a     3
b     6
c     9
d    12
e    15
dtype: int64
#Another way of defining index

import numpy as np
series1 = pd.Series(np.array(range(0,5))+2, index = range(0,5))
series1
0    2
1    3
2    4
3    5
4    6
dtype: int32
The Pandas Dataframe

Dataframe - It is a table with rows and columns

Rows having an index and columns having meaningful names
Creating dataframes

Ways to create dataframes are:
    from dictionaries
    JSON objects
    Reading from CSV files, flat files or txt files, etc.
#Using dictionaries, create dataframe

basket_df = pd.DataFrame({'fruits':['Banana','Guava','Orange','Apple','Kiwi'], 'weight':[12,1,24,2,8], 'color':['Yellow','Green','Pale yellow','Red','Brown']})
basket_df
fruitsweightcolor
0Banana12Yellow
1Guava1Green
2Orange24Pale yellow
3Apple2Red
4Kiwi8Brown
#Reading a CSV file as a dataframe

basket_df = pd.read_csv("G:/Chetan/table.csv")
basket_df
Fruits Weight Color
0Banana\t12\tYellow
1Orange\t32\tGreen
2Apple\t5\tRed
#Reading a excel file as a dataframe

basket_df = pd.read_excel("G:/Chetan/excel.xlsx")
basket_df
NameSurnameUSN
0AkshayKumar786
1ChetanShidling420
2NithinKuman123
3AishuShidling344
4KrishnaShidling484
5Rathantata341
6JackHeri848
7MackLoin233
8JohnSignh154
9KiranKapoor432
Play with Dataframe
#Finding the information

basket_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 3 columns):
Name       10 non-null object
Surname    10 non-null object
USN        10 non-null int64
dtypes: int64(1), object(2)
memory usage: 320.0+ bytes
#Dimension of dataframe (rows * columns)

basket_df.shape
(10, 3)
#Glance at top and bottom records of dataframe

#It will display top rows and columns
basket_df.head()
NameSurnameUSN
0AkshayKumar786
1ChetanShidling420
2NithinKuman123
3AishuShidling344
4KrishnaShidling484
#It will display buttom 5 rows and columns

basket_df.tail()
NameSurnameUSN
5Rathantata341
6JackHeri848
7MackLoin233
8JohnSignh154
9KiranKapoor432
#Summary of all the numeric columns in the dataset

basket_df.describe()
USN
count10.000000
mean416.500000
std242.030416
min123.000000
25%260.000000
50%382.000000
75%471.000000
max848.000000
#Find columns names

basket_df.columns
Index(['Name', 'Surname', 'USN'], dtype='object')
#Extract the values of a dataframe as a numpy array using dataframe.values

basket_df.values
array([['Akshay', 'Kumar', 786],
       ['Chetan', 'Shidling', 420],
       ['Nithin', 'Kuman', 123],
       ['Aishu', 'Shidling', 344],
       ['Krishna', 'Shidling', 484],
       ['Rathan', 'tata', 341],
       ['Jack', 'Heri', 848],
       ['Mack', 'Loin', 233],
       ['John', 'Signh', 154],
       ['Kiran', 'Kapoor', 432]], dtype=object)
#Indexing with your own values
#Let us see how we can do it

basket_df.head()
NameSurnameUSN
0AkshayKumar786
1ChetanShidling420
2NithinKuman123
3AishuShidling344
4KrishnaShidling484
#Set color column as index

basket_df.set_index('Name', inplace = True)
basket_df.head()
SurnameUSN
Name
AkshayKumar786
ChetanShidling420
NithinKuman123
AishuShidling344
KrishnaShidling484

Still we will update.

I hope this article may help you all a lot. Thank you for reading.

Also, read:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking