Unit 5
Unit 5
Example Code:
import numpy as np
import numpy as np
print(arr[0])
ACCESS 2-D ARRAYS
To access elements from 2-D arrays we can use comma separated integers representing
the dimension and the index of the element.
Think of 2-D arrays like a table with rows and columns, where the dimension represents the
row and the index represents the column.
Example Code:
import numpy as np
To access elements from 3-D arrays we can use comma separated integers representing
the dimensions and the index of the element.
Example code:
Access the third element of the second array of the first array:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
NUMPY ARRAY SLICING
Slicing arrays
Slicing in python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start:end].
We can also define the step, like this: [start:end:step].
If we don't pass start its considered 0
If we don't pass end its considered length of array in that dimension
If we don't pass step its considered 1
EXAMPLE
import numpy as np
print(arr[1:5])
CHECKING THE DATA TYPE OF AN ARRAY
The NumPy array object has a property called dtype that returns the data type of the array:
Example code:
Get the data type of an array object:
import numpy as np
print(arr.dtype)
SHAPE OF AN ARRAY
import numpy as np
print(arr.shape)
PANDAS
Example code:-
import pandas as pd
mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
myvar = pandas.DataFrame(mydataset)
print(myvar)
WHAT IS A SERIES?
Example code:-
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
READ CSV FILES
A simple way to store big data sets is to use CSV files (comma separated files).
CSV files contains plain text and is a well know format that can be read by everyone
including Pandas.
Example code:-
import pandas as pd
df = pd.read_csv('data.csv')
print(df)
VIEWING DATA
Example code:-
Example code:-
plt.plot(xpoints, ypoints)
plt.show()
MARKERS
You can use the keyword argument marker to emphasize each point with a specified
marker:
Example code:
import matplotlib.pyplot as plt
import numpy as np
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' X (filled)
'+' Plus
'P' Plus (filled)
's' Square
'D' Diamond
FORMAT STRINGS FMT
You can also use the shortcut string notation parameter to specify the marker.
This parameter is also called fmt, and is written with this syntax:
Example code:
import matplotlib.pyplot as plt
import numpy as np
plt.plot(ypoints, 'o:r')
plt.show()
TYPES OF CHARTS
Line Chart:-
Example code:-
import matplotlib.pyplot as plt
import numpy as np
Example code:-
plt.bar(x,y)
plt.show()
HORIZONTAL BARS
Example code:-
import matplotlib.pyplot as plt
import numpy as np
plt.barh(x, y)
plt.show()
BAR COLOR
The bar() and barh() take the keyword argument color to set the color of the bars:
Example code:-
import matplotlib.pyplot as plt
import numpy as np
The bar() takes the keyword argument width to set the width of the bars:
Example code:-
Draw 4 very thin bars:
import matplotlib.pyplot as plt
import numpy as np
The barh() takes the keyword argument height to set the height of the bars:
Example code:-
import matplotlib.pyplot as plt
import numpy as np
Example code:-
import numpy as np
print(x)
PIE CHART
Example code:-
plt.pie(y)
plt.show()
SCATTER PLOT
Example code:-
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
WHAT IS TKINTER?
• Tkinter is a Python wrapper around Tcl/Tk, a GUI toolkit developed in the 1990s.
• It allows Python developers to create windows, dialogs, buttons, menus, textboxes, and
other interface elements.
• It’s built into Python, so you don’t need to install anything extra (tkinter module is included
in standard Python distribution).
KEY FEATURES
Widget Description
Label Displays text or images
Button A clickable button
Entry A single-line text box
Text A multi-line text area
Frame A container for organizing widgets
Checkbutton, Radiobutton Selection controls
Canvas Drawing shapes, images, etc.
Menu Drop-down menus
BUTTON
import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds')
button = tk.Button(r, text='Stop',
width=25, command=r.destroy)
button.pack()
r.mainloop()
ENTRY
master = Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Last Name').grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop()
CHECKBUTTON
master = Tk()
var1 = IntVar()
Checkbutton(master, text='male',
variable=var1).grid(row=0, sticky=W)
var2 = IntVar()
Checkbutton(master, text='female',
variable=var2).grid(row=1, sticky=W)
mainloop()
RADIOBUTTON
root = Tk()
v = IntVar()
Radiobutton(root, text='GfG', variable=v,
value=1).pack(anchor=W)
Radiobutton(root, text='MIT', variable=v,
value=2).pack(anchor=W)
mainloop()