AI Lab2
AI Lab2
Lists:
Lists are what they seem - a list of values. Each one of them is numbered, starting from zero.
You can remove values from the list, and add new values to the end. Example: Your many cats'
names. Compound data types, used to group together other values. The most versatile is the list,
which can be written as a list of comma-separated values (items) between square brackets. List
items need not all have the same type.
print (cats[2])
cats.append(‘Oscar’)
Compound datatype:
>>> a = ['spam', 'eggs', 100, 1234]
Remove some:
>>> a[0:2] = []
>>> a
[123, 1234]
Length of list:
>>> a = ['a', 'b', 'c', 'd']
>>> len(a)
4
Nested lists:
>>> q = [2, 3]
>>> p = [1, q, 4]
>>> len(p)
3
>>> p[1]
[2, 3]
Functions of lists:
list.append(x): Add an item to the end of the list; equivalent to a[len(a):] = [x].
list.extend(L): Extend the list by appending all the items in the given list; equivalent to a[len(a):]
= L.
list.insert(i, x): Insert an item at a given position. The first argument is the index of the element
before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is
equivalent to a.append(x).
list.remove(x): Remove the first item from the list whose value is x. It is an error if there is no
such item.
list.pop(i): Remove the item at the given position in the list, and return it. If no index is
specified, a.pop() removes and returns the last item in the list.
Tuples:
Tuples are just like lists, but you can't change their values. Again, each value is numbered
starting from zero, for easy reference. Example: the names of the months of the year.
months = ('January' , 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December')
Index Value
0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December
We can have easy membership tests in Tuples using the keyword in.
>>> 'December' in months # fast membership testing
True
nums = (1, 2, 3, 4, 5)
print(nums[0]) # prints 1
print(nums[1]) # prints 2
print(nums[4]) # prints 5
Sets:
A set is an unordered collection with no duplicate elements. Basic uses include membership
testing and eliminating duplicate entries. Set objects also support mathematical operations like
union, intersection, difference, and symmetric difference.
Curly braces or the set() function can be used to create sets. Note: to create an empty set you
have to use set(), not {}; the latter creates an empty dictionary.
Example 1:
>>> fruit
True
False
Example 2:
>>> a = set('abracadabra')
>>> b = set('alacazam')
{'a', 'c'}
>>> a
{'r', 'd'}
Dictionaries:
Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an
'index' of words, and for each of them a definition.
In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't
numbered - they aren't in any specific order, either - the key does the same thing.
You can add, remove, and modify the values in dictionaries. Example: telephone book.
The main operations on a dictionary are storing a value with some key and extracting the value
given the key. It is also possible to delete a key:value pair with del. If you store using a key that
is already in use, the old value associated with that key is forgotten. It is an error to extract a
value using a non-existent key.
Performing list(d.keys()) on a dictionary returns a list of all the keys used in the dictionary, in
arbitrary order (if you want it sorted, just use sorted(d.keys()) instead). To check whether a single
key is in the dictionary, use the in keyword.
At one time, only one value may be stored against a particular key. Storing a new value for an
existing key overwrites its old value. If you need to store more than one value for a particular
key, it can be done by storing a list as the value for a key.
print("New phonebook")
print(phonebook)
list(phonebook.keys())
sorted(phonebook.keys())
def fib(n):
a = 1
b = 1
result = [a, b]
for i in range(2, n):
next_num = a + b
result.append(next_num)
a = b
b = next_num
print(result)
num = int(input("Enter length of fibonacci sequence: "))
fib(num)
2. Write a program that lets the user enter some English text, then converts the text to Pig-
Latin. To review, Pig-Latin takes the first letter of a word, puts it at the end, and appends
“ay”. The only exception is if the first letter is a vowel, in which case we keep it as it is
and append “hay” to the end. For example: “hello” -> “ellohay”, and “image” ->
“imagehay”
It will be useful to define a list or tuple at the top called VOWELS. This way, you can
check if a letter x is a vowel with the expression x in VOWELS.
It’s tricky for us to deal with punctuation and numbers with what we know so far, so
instead, ask the user to enter only words and spaces. You can convert their input from a
string to a list of strings by calling split on the string:
“My name is John Smith”.split(“ ”) -> [“My”, “name”, “is”, “John”, “Smith”]
def pig_latin(text):
VOWELS = ("a", "e", "i", "o", "u")
words = text.split(" ")
pig_latin_words = []
Pandas:
Before loading the data, lets understand the 2 key data structures in Pandas – Series and
DataFrames
o Series can be understood as a 1 dimensional labelled / indexed array. You can access
individual elements of this series through these labels.
o A dataframe is similar to Excel workbook – you have column names referring to columns
and you have rows, which can be accessed with use of row numbers. The essential
difference being that column names and row numbers are known as column and row
index, in case of dataframes.
Datatypes in Pandas:
Dataframe Datatypes:
Dataframe Attributes:
Dataframe Methods:
Dataframe Group by Method:
Dataframe: Filtering
Dataframe: Slicing
Dataframe: Method loc
Dataframe: Method iloc
Dataframe: sorting
Missing Values:
Aggregation Function in Pandas: computing a summary statistic about each group, i.e.
Common aggregation functions: min, max, count, sum, prod, mean, median, mode, mad, std, var
Creating a Histogram Using Pandas:
print(data.describe())
o display first and last column
print(first_and_last_columns)
Data Visualization is very much important task in machine learning. For data visualization in
python we use matplotlib and sklearn modules. With the usage of these modules we can have
clear view of how data points are rendered in 2D space. Below are some exercises which would
help you to plot graphs in an easier way.
Example-1: Line Plot in Python.
import numpy as np
plt.plot(x, x, label='linear')
# Add a legend
plt.legend()
plt.show()
plt.show()
fig = plt.figure()
# Set up Axes
sub1 = fig.add_subplot(221)
sub2 = fig.add_subplot(222)
sub3 = fig.add_subplot(223)
sub4 = fig.add_subplot(224)
plt.show()
iris = datasets.load_iris()
plt.scatter(iris.data[:,0],iris.data[:,1],
c=iris.target)
plt.colorbar(ticks=[0, 1, 2])
plt.show()
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
iris = datasets.load_iris()
ax.scatter(iris.data[:,0],
iris.data[:,1],iris.data[:,2],
c=iris.target)
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
Feature 1 Feature 2
12 4
11 5
8 1
6 4
9 3
6 6
10 2