<jupyter_start><jupyter_text>*Artificial Intelligence (AI) PRACTICALS :-* <NAME> --- **Practical 1 -** BFS, DFS & IDDFS> - Implement Breadth first Search algorithm > - Implement Depth First Search Algorithm> - Implement Iterative Deepening DFS Search---  `A. Breadth First Search`  Description* Uses FIFO, expands root node first, expands all nodes at level d before expanding level d+1.* A breadth first search (BFS) explores nodes nearest the root before exploring nodes further away.* After searching A, then B, then C it proceeds towards D, E, F and so on.![](https://drive.google.com/uc?export=view&id=1kadHTK2zyTVki-5vM90BoT371d3Ynvvf)  `◘ BFS Graph`![](https://drive.google.com/uc?id=1-fYSGyuhfLDMKczlmr3-oQ-LgFzbjVt-)<jupyter_code>#bfs
graph_shibu = {
  'A' : ['B','C'],
  'B' : ['D', 'E'],
  'C' : ['F'],
  'D' : [],
  'E' : ['F'],
  'F' : []
}
visited = [] 
queue = []     

def bfs(visited, graph_shibu, node):
  visited.append(node)
  queue.append(node)
  while queue:
    s = queue.pop(0) 
    print (s, end = " ") 
    for neighbour in graph_shibu[s]:
      if neighbour not in visited:
        visited.append(neighbour)
        queue.append(neighbour)
bfs(visited, graph_shibu, 'A')

print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>A B C D E F 
----------
[1m6022 SHIBU<jupyter_text>`B. Depth First Search`  Description* A depth first search (DFS) explores a path all the way to a leaf before backtracing & exploring another path.* Always expands the deepest nodes in the current fringe. The search proceeds immediately to deepest level of search tree, where nodes have no successors.* Uses LIFO queqe (stack)![](https://drive.google.com/uc?export=view&id=1pcc-p0GzLNaEWWCItlsyZGxV9eqoOLp3)  `◘ DFS Graph`![](https://drive.google.com/uc?id=15eVDBqs733rJKzKWy2sv75fwX7lXffQA)<jupyter_code>#dfs

graph_shibu = {
    'A' : ['B','C'],
    'B' : ['D', 'E'],
    'C' : ['D'],
    'D' : ['E'],
    'E' : [],
}
visited = set()            
def dfs(visited, graph_shibu, node):
    if node not in visited:
        print (node)
        visited.add(node)
        for neighbour in graph_shibu[node]:
            dfs(visited, graph_shibu, neighbour)
dfs(visited, graph_shibu, 'A')

print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>A
B
D
E
C

----------
[1m6022 SHIBU<jupyter_text>`C. Iterative Deepening DFS Search`  Description:* IDDFS combines depth-first search’s space-efficiency and breadth-first search’s fast search (for nodes closer to root)* Algorithm-`1. First, perform a depth-first search to depth one.` `2. Then, discarding the nodes generated in the first search, start over and do a depth-first search to level two.``3. Next, start over again and do a depth-first search to depth three, etc., continuing this process until a goal state is reached.`![](https://drive.google.com/uc?export=view&id=1ZQtQoVHPtn0Ic1hmstOXS8K7BgywBWSY)  `◘ IDDFS Graph`![](https://drive.google.com/uc?id=12TZxv_ZS3_PjsrAN-sKOVi3-WwbNDPQ-)<jupyter_code>#Iterative Deepening Search DFS

graph_shibu = {
    '0': ['1'],
    '1': ['0', '2'],
    '2': ['3', '5', '7', '8'],
    '3': ['9', '10'],
    '5':['2', '6', '7', '8'],
    '6':['5'],
    '7':['2','5', '8'],
    '8':['2','5', '7'],
    '9':['3'],
    '10':['3'],  
}
def DLS(source,depth):
    start=graph_shibu[source]
    print(source, end=' ')
    if start[0] is None:
        return
    if depth==0:
        return
    else:
        for location in start:
            listt = location
            for place in listt:
                DLS(place,depth-1)

def iddfs(source,max_height):
    for i in range(max_height+1):
        print('depth: '+str(i)+': ',end=' ')
        DLS(source,i)
        print()

iddfs('0',4)

print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>depth: 0:  0 
depth: 1:  0 1 
depth: 2:  0 1 0 2 
depth: 3:  0 1 0 1 2 3 5 7 8 
depth: 4:  0 1 0 1 0 2 2 3 9 1 0 5 2 6 7 8 7 2 5 8 8 2 5 7 

----------
[1m6022 SHIBU<jupyter_text>--- **Practical 2 -** A* search & RBFS> - Implement A* Heuristic search algorithm > - Implement Recurssive Best First Search---  `A* Hueristic Search`  Description:* A* Search algorithm is one of the best and popular technique used in path-finding and graph traversals.*  A* working:1. Node (also called State) — All potential position or stops with a unique identification2. Transition — The act of moving between states or nodes.Starting Node — Whereto start searching3. Goal Node — The target to stop searching.Search Space — A collection of nodes, like all board positions of a board game.4. Cost — Numerical value (say distance, time, or financial expense) for the path from a node to another node.5. g(n) — this represents the exact cost of the path from the starting node to any node n6. h(n) — this represents the heuristic estimated cost from node n to the goal node.7. f(n) — lowest cost in the neighboring node n8. These values we calculate using the following formula: **f(n) = g(n) + h(n)**![](https://drive.google.com/uc?export=view&id=1MDBpinYHa1diBkylWvpdpakHUOM0plfO)  `◘ Coding part`<jupyter_code>from queue import PriorityQueue

class State(object):
    def __init__(self, value, parent, start = 0, goal = 0):
        self.children = []
        self.parent = parent
        self.value = value
        self.dist = 0
        if parent:
            self.start = parent.start
            self.goal = parent.goal
            self.path = parent.path[:]
            self.path.append(value)

        else:
            self.path = [value]
            self.start = start
            self.goal = goal

    def GetDistance(self):
        pass
    def CreateChildren(self):
        pass

class State_String(State):
    def __init__(self, value, parent, start = 0, goal = 0 ):
        super(State_String, self).__init__(value, parent, start, goal)
        self.dist = self.GetDistance()

    def GetDistance(self):
            if self.value == self.goal:
                return 0
            dist = 0
            for i in range(len(self.goal)):
                letter = self.goal[i]
                dist += abs(i - self.value.index(letter))
            return dist

    def CreateChildren(self):
            if not self.children:
                for i in range(len(self.goal)-1):
                    val = self.value
                    val = val[:i] + val[i+1] + val[i] + val[i+2:]
                    child = State_String(val, self)
                    self.children.append(child)

class a_star:
    def __init__(self, start, goal):
        self.path = []
        self.vistedQueue =[]
        self.priorityQueue = PriorityQueue()
        self.start = start
        self.goal = goal

    def Solve(self):
        startState = State_String(self.start,0,self.start,self.goal)
        count = 0
        self.priorityQueue.put((0,count, startState))
        while(not self.path and self.priorityQueue.qsize()):
               closesetChild = self.priorityQueue.get()[2]
               closesetChild.CreateChildren()
               self.vistedQueue.append(closesetChild.value)
               for child in closesetChild.children:
                   if child.value not in self.vistedQueue:
                    count += 1
                    if not child.dist:
                       self.path = child.path
                       break
                    self.priorityQueue.put((child.dist,count,child))
        if not self.path:
            print("Goal Of  is not possible !" + self.goal )
        return self.path

if __name__ == "__main__":
    start1 = "hbuis"
    goal1 = "shibu"
    #print("Starting....")
    a = a_star(start1,goal1)
    a.Solve()
    for i in range(len(a.path)):
        print("->",i,a.path[i])

print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>-> 0 hbuis
-> 1 hbius
-> 2 hbisu
-> 3 hibsu
-> 4 hisbu
-> 5 hsibu
-> 6 shibu

----------
[1m6022 SHIBU<jupyter_text>`B. Recursive best-first search algorithm`  Description:* Recursive Best-First Search or RBFS, is an AI algo that belongs to heuristic search algorithm.* RBFS is similar to a recursive implementation of depth-first search, with the difference that it uses a special condition for backtracking that ensures that nodes are expanded in best-first order. * It works by maintaing on the recursion stack the complete path to the current node being expanded, as well as all immediate siblings of nodes on that path, along with cost of the best node in the subtree explored below each sibling.* Whenever the cost of the current node exceeds that of some other node in the previously expanded portion of the tree, the algorithm backs up to their deepest common ancestor, and continues the search down the new path.**Example can be given using Romanian map:**![](https://drive.google.com/uc?export=view&id=1SNQsAzA2uwWWQ1hwW-6q8ui46hBZWOy_)![](https://drive.google.com/uc?export=view&id=13Ro6qHx8wA8t5m-vX15WYzaqYTdI3CLa)![](https://drive.google.com/uc?export=view&id=1CKLJ8QUyXRl2-LVrlLrEObqvtqf2Ynne)<jupyter_code>'''def RBFS( Root, Goal,  limit):
     if(Root==Goal):
          goalSucc=true
          return Root.fCost
     else:
         successors=ExpandNode(Root)
         successors.SortNodes()   
         if(successors[firstNode].fCost>limit):
              return successors[firstNode].fCost
         else:
              closeList.Insert(Root)
              if s in successors:
                   if(s!=closeList[item]):
                        openList.Insert(s)
                        openList.Sort()
     bestNode=openList.RemoveFirstNode();
     alternativeNode=openList.RemoveFirstNode();

     while(goalSucc==false):
          bestNode=RBFS(bestNode,Goal,Math.Min(limit, alternativeNode.fCost))
          openList.Insert(bestNode)
          list.Sort( )
          bestNode=openList.RemoveFirstNode()
          alternativeNode=openList.RemoveFirstNode()

RBFS(2,5,3)'''

print("Shibu")<jupyter_output>Shibu<jupyter_text>--- **Practical 3 -** IRIS Dataset> - Implementation of Neural Network on IRIS dataset---  Description:* Iris Data Set, this small dataset often used for testing out ML algo & visualizations (eg: Scatter Plot). * Each row of the table represents an iris flower, including its species and dimensions of its botanical parts, sepal and petal, in centimeters.* The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. * One class is linearly separable from the other 2; the latter are NOT linearly separable from each other.1. Class:   > Iris Setosa   > Iris Versicolour   > Iris Virginica![](https://drive.google.com/uc?export=view&id=1NLFhihRkVg09OJbxwsop4i0H8humaAf0)  `◘ Packages`<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>`◘ Uploading the dataset`<jupyter_code>df=pd.read_csv("/content/drive/My Drive/Colab Notebooks/iris.csv")
df.head()<jupyter_output><empty_output><jupyter_text>`◘ Finding the missing values`<jupyter_code>df.isnull().sum()
df["species"].unique()

'''df.species[df.species=="setosa"]=0
df.species[df.species=="versicolor"]=1
df.species[df.species=="virginica"]=2'''
df
df=pd.get_dummies(df,columns=["species"])
df.head()

df.shape
attri=df.drop(["species_setosa","species_versicolor","species_virginica"],axis=1)
clas=df.drop(["sepal_length","sepal_width","petal_length","petal_width"],axis=1)
from sklearn import preprocessing
normalized_attri = preprocessing.normalize(attri)
from sklearn.model_selection import train_test_split
attri_train,attri_val,clas_train,clas_val=train_test_split(normalized_attri,clas,test_size=0.15,random_state=42)
attri_train,attri_test,clas_train,clas_test=train_test_split(attri_train,clas_train,test_size=0.15,random_state=42)
from keras.models import Sequential 
from keras.layers import Dense,Activation,Dropout 
#from keras.layers.normalization import BatchNormalization 
#from keras.utils import np_utils<jupyter_output><empty_output><jupyter_text>`◘ Applying multi-layer neural netwrok model`<jupyter_code>def Neural_Network():
    model=Sequential()
    #input layer
    model.add(Dense(8,input_dim=4))
    model.add(Activation('relu'))
    
    #1st hidden layer
    model.add(Dense(16))
    model.add(Activation('relu'))
    
    #2nd hidden layer
    model.add(Dense(32))
    model.add(Activation('relu'))
    
    #Output layer
    #model.add(Dropout(0.5))
    model.add(Dense(3))
    model.add(Activation('sigmoid'))
    return model

model=Neural_Network()
model.compile(loss='categorical_crossentropy',optimizer="adam",metrics=['accuracy'])
model.summary()
from keras import optimizers
#opt = optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, decay=0.0001/100, amsgrad=False, clipnorm = 1.)
model.fit(attri_train,clas_train,validation_data=(attri_val,clas_val),batch_size=32,epochs=100)
loss,accuracy=model.evaluate(x=attri_test,y=clas_test,batch_size=32)
print(accuracy*100)
loss
print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>----------
[1m6022 SHIBU<jupyter_text>--- **Practical 4 -** Diabetes Dataset> - Implementation of basic neural network model with 4 activation functions on Pima Indians onset of diabetes dataset.---  Description:* This dataset is originally from the National Institute of Diabetes and Digestive and Kidney Diseases. * The objective of the dataset is to diagnostically predict whether or not a patient has diabetes, based on certain diagnostic measurements included in the dataset. Several constraints were placed on the selection of these instances from a larger database. * In particular, all patients here are females at least 21 years old of Pima Indian heritage.* The datasets consists of several medical predictor variables and one target variable, Outcome. Predictor variables includes the number of pregnancies the patient has had, their BMI, insulin level, age, and so on.*   The steps to use keras are as follows:1.   Load Data.2.   Define Keras Model.3.   Compile Keras Model.4.   Fit Keras Model.5.   Evaluate Keras Model.6.   Tie It All Together.7.   Make Predictions  `◘ Uploading csv file`<jupyter_code>from google.colab import files
files.upload()<jupyter_output><empty_output><jupyter_text>`◘ Actual algorithm`<jupyter_code>from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense

# loading the dataset
dataset = loadtxt('/content/drive/My Drive/Colab Notebooks/pima-indians-diabetes.csv', delimiter=',')

# split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]

# defining the keras model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# fit the keras model on the dataset
model.fit(X, y, epochs=170, batch_size=10)

# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))
print('Accuracy: %.2f' % (accuracy*100))

print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>Accuracy: 70.31

----------
[1m6022 SHIBU<jupyter_text>--- **Practical 5 -** AND & OR in NN> - Performing AND & OR Operations in the Neural Network.--- --- **Perceptron:**1.   A Perceptron is an algorithm used for supervised learning of binary classifiers. 2.   In other words, perceptron is a single-layer neural network. 3. They consist of four main parts including input values, weights and bias, net sum, and an activation function.--- **Working:-**1.   The process begins by taking all the input values and multiplying them by their weights. 2. Then, all of these multiplied values are added together to create the weighted sum. The weighted sum is then applied to the activation function, producing the perceptron's output. 3. The activation function plays the integral role of ensuring the output is mapped between required values such as (0,1) or (-1,1). 4. It is important to note that the weight of an input is indicative of the strength of a node. 5. Similarly, an input's bias value gives the ability to shift the activation function curve up or down. **Diagram:**![](https://drive.google.com/uc?id=1Ynm9XYWMH-Vy6kuWuhV-rO3fmKq9gO6W)---  `◘ Packages & Implementation`<jupyter_code>import numpy as np

#Defining unit step function
#Heavyside Step function. v must be a scalar
def unitStep(v):
  if v >= 0:
    return 1
  else:
    return 0

#Function implemented by a perceptron with weight vector w and bias b
#Designing Perceptron Model
def perceptronModel(x, w, b):
  v = np.dot(w, x) + b
  y = unitStep(v)
  return y

#Testing th perceptron Model
test1 = np.array([0, 0])
test2 = np.array([0, 1])
test3 = np.array([1, 0])
test4 = np.array([1, 1])

#OR Logic Function
def OR_logicFunction(x):
  w = np.array([1, 1])           #w1 = 1, w2 = 1, b = -0.5
  b = -0.5
  return perceptronModel(x, w, b)

#AND Logic Function
def AND_logicFunction(x):           #ŷ = ϴ(w1x1 + w2x2 + b)
  w = np.array([1, 1])              #w1 = 1, w2 = 1, b = -1.5
  b = -1.5
  return perceptronModel(x, w, b)       

#NOT Logic Function
def NOT_logicFunction(x):                   #ŷ = ϴ(wx + b)
	return perceptronModel(x, w=-1, b=0.5)    #w = -1 and b = 0.5.

#XOR Logic Function         
def XOR_logicFunction(x):                 #2 NOT Gates, 2 AND Gates, 1 OR Gate       
    g1 = AND_logicFunction(x)             #XOR(X1, X2) = AND(NOT(AND(x1, x2)), OR(x1, x2))
    g2 = NOT_logicFunction(g1)
    g3 = OR_logicFunction(x)
    new_x = np.array([g2, g3])
    output = AND_logicFunction(new_x)
    return output

print("OR({}, {}) = {}".format(0, 0, OR_logicFunction(test1)))
print("OR({}, {}) = {}".format(0, 1, OR_logicFunction(test2)))
print("OR({}, {}) = {}".format(1, 0, OR_logicFunction(test3)))
print("OR({}, {}) = {}".format(1, 1, OR_logicFunction(test4)))

print("--------")

print("AND({}, {}) = {}".format(0, 0, AND_logicFunction(test1)))
print("AND({}, {}) = {}".format(0, 1, AND_logicFunction(test2)))
print("AND({}, {}) = {}".format(1, 0, AND_logicFunction(test3)))
print("AND({}, {}) = {}".format(1, 1, AND_logicFunction(test4)))

print("--------")

print("NOT(0) = {}".format(NOT_logicFunction(0)))
print("NOT(1) = {}".format(NOT_logicFunction(1)))

print("----------")

print("XOR({}, {}) = {}".format(0, 0, XOR_logicFunction(test1)))
print("XOR({}, {}) = {}".format(0, 1, XOR_logicFunction(test2)))
print("XOR({}, {}) = {}".format(1, 0, XOR_logicFunction(test3)))
print("XOR({}, {}) = {}".format(1, 1, XOR_logicFunction(test4)))

print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>OR(0, 0) = 0
OR(0, 1) = 1
OR(1, 0) = 1
OR(1, 1) = 1
--------
AND(0, 0) = 0
AND(0, 1) = 0
AND(1, 0) = 0
AND(1, 1) = 1
--------
NOT(0) = 1
NOT(1) = 0
----------
XOR(0, 0) = 0
XOR(0, 1) = 1
XOR(1, 0) = 1
XOR(1, 1) = 0

----------
[1m6022 SHIBU<jupyter_text>--- **Practical 6** - Cat vs NonCat> - Prediction Algorithm - Use of different packages on dataset of Cat and Non-Cat images.---  Description:* Data will be loaded from the test_catvnoncat.h5 and train_catvnoncat.h5 files  The load_dataset function below is responsebile for loading the above mentioned data files.lr_utils file includes the function load_dataset()* Loading the data (cat/non-cat)train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset() We added "_orig" at the end of image datasets (train and test) because we are going to preprocess them.   After preprocessing, we will end up with train_set_x and test_set_x (the labels train_set_y and test_set_y don't need any preprocessing).  Each line of your train_set_x_orig and test_set_x_orig is an array representing an image. You can visualize an example by running the following code.  * Feel free also to change the `index` value and re-run to see other images.   Example of a picture change the index value below to check if the image at that particular index is cat or non cat**Note: For the first image(flower) index = 30 & for the second image (Cat image) index = 24**  `◘ Coding and implementation`<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
%matplotlib inline

def load_dataset():
    train_dataset = h5py.File('/content/drive/My Drive/Colab Notebooks/datasets/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File('/content/drive/My Drive/Colab Notebooks/datasets/test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

# Loading the data (non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
index = 30
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a " + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  " picture")
# Loading the data (cat)
train_set_y_orig, train_set_x, test_set_y_orig, test_set_x, classes = load_dataset()
index = 24
plt.imshow(train_set_y_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a " + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  " picture")
print("----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>----------
[1m6022 SHIBU<jupyter_text>--- **Practical 8 -** Simple Linear Regression---  Description:* Linear Regression is a machine learning algorithm based on supervised learning. * It performs a regression task. Regression models a target prediction value based on independent variables. * It is mostly used for finding out the relationship between variables and forecasting. * Different regression models differ based on – the kind of relationship between dependent and independent variables, they are considering and the number of independent variables being used.![](https://drive.google.com/uc?export=view&id=1e85UJ4UxaVLQfl-LLfABEU0Mghb-6RGI)  `◘ Packages`<jupyter_code>import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
x_and_y = [(0,1),(1,3),(2,2),(3,5),(4,7),(5,8),(6,8),(7,9),(8,10),(9,12)]
data = pd.DataFrame(x_and_y, columns=['X','y'])
data<jupyter_output><empty_output><jupyter_text>`◘ Plotting`<jupyter_code>X = data['X']
y = data['y']
b = 0
w = 1.5

y_predicted = b + w * X
error = y - y_predicted
print(np.mean(error**2))
plt.scatter(X,y)
plt.plot(X, y_predicted)
print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>----------
[1m6022 SHIBU<jupyter_text>--- **Practical 9 -** Support Vector Algorithm>- Implement Support Vector Algorithm---  Description:* Itis a supervised machine learning algorithm which can be used for both classification or regression challenges. * However,  it is mostly used in classification problems. In the SVM algorithm, we plot each data item as a point in n-dimensional space (where n is number of features you have) with the value of each feature being the value of a particular coordinate. * Then, we perform classification by finding the hyper-plane that differentiates the two classes very well.![](https://drive.google.com/uc?id=1qsbhpArP9qBIsAa-JEJImF1eU0uWrKUp)  `◘ Coding and implementation`<jupyter_code># Support Vector Algorithm

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn import svm

x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]
plt.scatter(x,y)
plt.show()
X = np.array([[1, 2],
             [5, 8],
             [1.5, 1.8],
             [8,8],
             [1, 0.6],
             [9, 11]])
y = np.array([0, 1, 0, 1, 0, 1])

clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,y)
w = clf.coef_[0]

a = -w[0] / w[1]

xx = np.linspace(0,12)
yy = a * xx -clf.intercept_[0] / w[1]

h0 = plt.plot(xx, yy, 'k-', label="non weighted div")

plt.scatter(X[:, 0], X[:, 1], c = y)
plt.legend()
plt.show()

print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output><empty_output><jupyter_text>--- **Practical 10 -** Decision Tree Learning>- Implement Decision Tree Learning Algorithm---  Description:* Decision Tree Analysis is a general, predictive modelling tool that has applications spanning a number of different areas. In general, decision trees are constructed via an algorithmic approach that identifies ways to split a data set based on different conditions.* The terminologies are:1. Instances: Refer to the vector of features or attributes that define the input space2. Attribute: A quantity describing an instance3. Concept: The function that maps input to output4. Target Concept: The function that we are trying to find, i.e., the actual answer5. Hypothesis Class: Set of all the possible functions6. Sample: A set of inputs paired with a label, which is the correct output (also known as the Training Set)7. Candidate Concept: A concept which we think is the target concept8. Testing Set: Similar to the training set and is used to test the candidate concept and determine its performance* A decision tree is a tree-like graph with nodes representing the place where we pick an attribute and ask a question; edges represent the answers the to the question; and the leaves represent the actual output or class label. They are used in non-linear decision making with simple linear decision surface.* Decision trees classify the examples by sorting them down the tree from the root to some leaf node, with the leaf node providing the classification to the example. Each node in the tree acts as a test case for some attribute, and each edge descending from that node corresponds to one of the possible answers to the test case. This process is recursive in nature and is repeated for every subtree rooted at the new nodes.![](https://drive.google.com/uc?id=1UNRf6ZgnheoyfK0PhU1oQ32ppynDBPSp)    Fig 1. A decision tree for the concept Play Badminton![](https://drive.google.com/uc?id=1iJ64QAYkjcYKmPgKhcYGPBbJDdmzR8nl)    Fig 2. A decision tree for the concept Play Badminton (when attributes are continuous)  `◘ Coding part`<jupyter_code># Here we have taken a decision tree for Iris Dataset

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
from IPython.display import Image
from pydot import graph_from_dot_data
import pandas as pd
import numpy as np

iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
print("Following are the columns of iris dataset\n")
print(X)
print("\n\n")
y = pd.Categorical.from_codes(iris.target, iris.target_names)

X.head()
y = pd.get_dummies(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)

dot_data = StringIO()
export_graphviz(dt, out_file=dot_data, feature_names=iris.feature_names)
(graph, ) = graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
print("\n----------")
print('\033[1m' + '6022 SHIBU')<jupyter_output>----------
[1m6022 SHIBU<jupyter_text>**Miscellaneous*** Basically a second code for cats vs non-cats dataset<jupyter_code>!wget --no-check-certificate \
  https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \
  -O /tmp/cats_and_dogs_filtered.zip
import os
import zipfile

local_zip = '/tmp/cats_and_dogs_filtered.zip'

zip_ref = zipfile.ZipFile(local_zip, 'r')

zip_ref.extractall('/tmp')
zip_ref.close()
base_dir = '/tmp/cats_and_dogs_filtered'

train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')

# Directory with our training cat/dog pictures
train_cats_dir = os.path.join(train_dir, 'cats')
train_dogs_dir = os.path.join(train_dir, 'dogs')

# Directory with our validation cat/dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')
validation_dogs_dir = os.path.join(validation_dir, 'dogs')

print('total training cat images :', len(os.listdir(      train_cats_dir ) ))
print('total training dog images :', len(os.listdir(      train_dogs_dir ) ))

print('total validation cat images :', len(os.listdir( validation_cats_dir ) ))
print('total validation dog images :', len(os.listdir( validation_dogs_dir ) ))
import tensorflow as tf
model = tf.keras.models.Sequential([
                                    
    # Note the input shape is the desired size of the image 150x150 with 3 bytes color
    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2), 
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'), 
    tf.keras.layers.MaxPooling2D(2,2),

    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(), 

    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'), 
    
    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('cats') and 1 for the other ('dogs')
    tf.keras.layers.Dense(1, activation='sigmoid')  
])
model.summary()
from tensorflow.keras.optimizers import RMSprop

model.compile(optimizer=RMSprop(lr=0.001),
              loss='binary_crossentropy',
              metrics = ['accuracy'])
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# All images will be rescaled by 1./255.
train_datagen = ImageDataGenerator( rescale = 1.0/255. )
test_datagen  = ImageDataGenerator( rescale = 1.0/255. )


# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(train_dir,
                                                    batch_size=20,
                                                    class_mode='binary',
                                                    target_size=(150, 150))     

# Flow validation images in batches of 20 using test_datagen generator
validation_generator =  test_datagen.flow_from_directory(validation_dir,
                                                         batch_size=20,
                                                         class_mode  = 'binary',
                                                         target_size = (150, 150))

history = model.fit(train_generator,
                              validation_data=validation_generator,
                              steps_per_epoch=100,
                              epochs=15,
                              validation_steps=50,
                              verbose=2)
import numpy as np

from google.colab import files
from keras.preprocessing import image

uploaded=files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path='/content/' + fn
  img=image.load_img(path, target_size=(150, 150))
  
  x=image.img_to_array(img)
  x=np.expand_dims(x, axis=0)
  images = np.vstack([x])
  
  classes = model.predict(images, batch_size=10)
  
  print(classes[0])
  
  if classes[0]>0:
    print(fn + " is a non cat")
    
  else:
    print(fn + " is a cat")
 
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()


plt.show()
loss<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12937270990573424, 'fraction_numerical': 0.026087501174407318, 'mean_word_length': 3.5780645161290323, 'pattern_counts': {'":': 1, '<': 63, '<?xml version=': 0, '>': 88, 'https://': 17, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '598513', 'license': 'MIT', 'n_tokens_mistral': 11150, 'n_tokens_neox': 10445, 'n_words': 3130, 'path': "Shibu's_AI_Basics.ipynb", 'repo_name': 'MohapatraShibu/Artificial-Intelligence', 'size': '31940'}