Experiment 12: Implement N Queens problem using Back Tracking. from math import * x={ } n=4 def place(k,i): if(i in x.values()): return False j=1 while j<k: a=x[j] if abs(a-i)==abs(j-k): return False j=j+1 return True def clearBlocks(k): for i in range(k,n+1): x[i]=None def Nqueens(k): for i in range(1,n+1): clearBlocks(k) if place(k,i): x[k]=i if k==n: for j in x: print("Place Queen at position :",x[j]) print(". ......................... ") else: Nqueens(k+1) # Driver code Nqueens(1)
Experiment 11: Consider the problem having weights and profits are: Weights: {3, 4, 6, 5} Profits: {2, 3, 1, 4} The weight of the knapsack is 8 kg. Find the optimal set of items to include in the knapsack using dynamic programming def knapsack(w,wt,val,n): k=[[0 for x in range(w+1)]for x in range(n+1)] for i in range(n+1): for w in range(w+1): if i==0 or w==0: k[i][w]=0 elif wt[i-1]<=w: k[i][w]=max(val[i-1]+k[i-1][w-wt[i1]],k[i- 1][w]) else: k[i][w]=k[i- 1][w] return k[n][w] # driver code val=[2,3,1,4] wt=[3,4,6,5] w=8 n=len(val) print("Optimal Solution: ",knapsack(w,wt,val,n))
Experiment 9: Consider the distance between Hassan and N different cities. Every city can be reached from Hassan directly or by using intermediate cities whichever costs less. Find the shortest distance from Hassan to other cities using Dijkstra’s algorithm. def near_vertex(d,vt): min=999 for i in range(len(d)): if i not in vt: if d[i]<min: min=d[i] v=i return v def path(v,pv,a): if pv[v]!=-1: a.append(pv[v]) path(pv[v],pv,a) def print_paths(d,pv): for i in range(len(d)): print print("path to vertex:",i) a=[] path(i,pv,a) a.reverse() a.append(i) print(a) def dijkstra(graph): n=len(graph) d=[] pv=[] vt=[] for i in range(n): d.append(999) pv.append(-1) s=0 d[s]=0 for i in range(n): v=near_vertex(d,vt) vt.append(v) adj_vertex=graph[v] for val in adj_vertex: if val[0] not in vt: if d[v]+val[1]<d[val[0]]: d[val[0]]=d[v]+val[1] pv[val[0]]=v print_paths(d,pv) # Driver code #//first write the example graph and then take input to the program from the same graph. #Take the input from graph graph={0:[[3,7]],1:[[2,4]],2:[[4,6]],3:[[1,2],[2,5]] , 4:[[3,4]]} dijkstra(graph)
Experiment 8: Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal’s algorithm. def find_set(all_set,key): for i in range(len(all_set)): if key in all_set[i]: return i def kruskal(edge,v): edge.sort(key=lambda x:x[2]) all_set=[ ] for k in vert: a=[] a.append(k) all_set.append(a) st_edge=[] i=0 ct=0 while ct<n-1: x=edge[i][0] y=edge[i][1] s1=find_set(all_set,x) s2=find_set(all_set,y) if s1!=s2: a=[] a.append([x,y,edge[i][2]]) st_edge.append(a) all_set[s1]=all_set[s1]+all_set[s2] del all_set[s2] ct=ct+1 i=i+1 return st_edge # driver code #//first write the example graph and then take input to the program from the same graph. edge=[['a','b',5],['b','d',6],['c','d',4],['a','c',7 ],['a','e',2],['b','e',3],['d','e',5],['c','e',4]] vert=['a','b','c','d','e'] n=len(vert) st_edge=kruskal(edge,vert) print (st_edge)
Experiment 7: There are n different routes from hostel to college. Each route incurs some cost. Find the minimum cost route to reach the college from hostel using Prim’s algorithm. def min_edge(edge,v,vt): min=999 for i in range(len(edge)): x=edge[i][0] y=edge[i][1] w=edge[i][2] if(x in v and y in vt)or(x in vt and y in v): if w<min: min=w pos=i return pos def prims(edge,v): vt=[] et=[] vert=v.pop(0) vt.append(vert) n=len(v) for i in range(n): pos=min_edge(edge,v,vt) x=edge[pos][0] y=edge[pos][1] b=edge[pos] del edge[pos] et.append(b) if x in vt: vt.append(y) v.remove(y) else: vt.append(x) v.remove(x) return et # driver code #//first write the example graph and then take input to the program from the same graph. edge=[['a','b',5],['b','d',6],['c','d',4],['a','c',7 ],['a','e',2],['b','e',3],['d','e',5],['c','e',4]] vert=['a','b','c','d','e'] st_edge=[ ] st_edge=prims(edge,vert) print (st_edge)
Experiment 6: Consider n cities. The shortest path between every pair of cities needs to be determined. Implement Floyd’s algorithm for the All-Pairs- Shortest- Paths problem. Also find transitive closure by implementing Warshall’s algorithm. def all_pair_sort(dist,n): for k in range(n): for i in range(n): for j in range(n): dist[i][j]=min(dist[i][j], (dist[i][k]+dist[k][j])) def transit_clos(reach,n): for k in range(n): for i in range(n): for j in range(n): reach[i][j]=(reach[i][j])or (reach[i][k]and reach[k][j]) # driver code #input for floyd's algorithm graph=[[0,100,3,100],[2,0,100,100],[100,7,0,1],[6,10 0,100,0]] n=4 all_pair_sort(graph,n) print("All-Pairs- Shortest-Paths") for i in range(n): print (graph[i]) #input for warshall's algorithm #graph=[[0,1,0,0],[0,0,0,1],[0,0,0,0],[1,0,1,0]] graph=[[1,1,0,1],[0,1,1,0],[0,0,1,1],[0,0,0,1]] n=4 transit_clos(graph,n) print(" transitive closure") for i in range(n): print (graph[i])
Experiment 5: Implement Horspool algorithm for String Matching. def horspool(text,pattern): m=len(text) n=len(pattern) if m<n: return -1 shift=[n]*250 for i in range(n-1): shift[ord(text[i])]=n-i-1 pos=0 while pos<=m-n: j=n-1 while j>=0 and pattern[j]==text[pos+j]: j=j-1 if j<0: return pos else: pos += max(1, j-shift[ord(text[pos+j])]) return -1 # Driver code text=input("Enter text string: ") pattern=input("Enter pattern string: ") pos=horspool(text,pattern) if(pos>=0): print("Pattern string found at position:",pos+1) else: print("Pattern string not found")
Experiment 4: Sort a given set of elements using the Heap sort method. import random def gen_data(a,n): for i in range(n): a.append(random.randint(0,100)) def heapify(arr, n, i): largest = i l = 2 * i + 1 r = 2 * i + 2 if l < n and arr[i] < arr[l]: largest = l if r < n and arr[largest] < arr[r]: largest = r if largest != i: arr[i],arr[largest] = arr[largest],arr[i] heapify(arr, n, largest) def heapSort(arr): n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify(arr, n, i) for i in range(n-1, 0, -1): arr[i], arr[0] = arr[0], arr[i] heapify(arr, i, 0) # Driver code to test above a=[ ] n=int(input('Enter size :')) gen_data(a,n) print('Given elements are:') print (a) heapSort(a) n = len(a) print ("Sorted array is") print (a)
Experiment 3: Print all the nodes reachable from a given starting node in a graph using DFS and BFS def dfs(graph,node,path): path.append(node) for v in graph[node]: if v not in path: dfs(graph,v,path) def bfs(graph,start,path): q=[start] path.append(start) while q: node=q.pop(0) for v in graph[node]: if not v in path: path.append(v) q.append(v) #-------driver code--------- #//first write the example graph and then take input to the program from the same graph. graph={'A':['B','C'],'B':['C'],'C':['D','E'],'D':['E'],'E':['A']} #graph={'A':['B','C'],'B':['E','G'],'C':['F'],'D':['A','B','C','E'], 'E':['F','D'],'F':[],'G':['E','F']} #graph={1:[2,3,4],2:[6,3,1],3:[1,2,6,5,4],4:[1,3,5],5:[3,4],6:[2,3]} #graph={40:[20,10],20:[10,30,50,60],50:[70],70:[10],10:[30],30:[60], 60:[70]} #graph={1:[2,3],2:[1],3:[1],4:[5],5:[4],6:[]} path=[] dfs(graph,'A',path) print (“dfs sequence:”,path) path=[ ] bfs(graph,'A',path) print(“bfs sequence:”, path)
Experiment 2: Students in a department need to be selected for a high jump competition based on their height (integer values only). Sort the heights of students using Quick sort and find the time required for the Sorting. import random import time def gen_data(a,n): for i in range(n): x=random.randint(0,100) a.append(x) def partition(a,left,right): key=a[left] i=left+1 j=right while True: while a[i]<key and i<right: i=i+1 while a[j]>key and j>left: j=j-1 if i<j: a[i],a[j]=a[j],a[i] i=i+1 j=j-1 else: break a[left],a[j]=a[j],a[left] return j def quick_sort(a,low,high): if low<high: k=partition(a,low,high) quick_sort(a,low,k-1) quick_sort(a,k+1,high) # driver code a=[ ] print("enter size: ") n=int(input()) gen_data(a,n) print("the numbers are:") print (a) start=time.time() quick_sort(a,0,n-1) end=time.time() print("the sorted numbers are:") print (a) print("Time required to sort :",(end-start))
Experiment 1: Employees in an organization need to be grouped for a tournament based on their ages. Sort the ages using Merge sort and find the time required to perform the sorting. import random def gen_data(a, n): for i in range(n): x=random.randint(0,100) a.append(x) def merge_sort(a,low,high): if high-low<1: return mid=int( (low+high)/2 ) merge_sort(a,low,mid) merge_sort(a,mid+1,high) merge(a,low,mid,high) def merge(a,low,mid,high): left=a[low:mid+1] right=a[mid+1:high+1] k=low i=0 j=0 while(i<len(left) and j<len(right)): if left[i]<right[j]: a[k]=left[i] i=i+1 else: a[k]=right[j] j=j+1 k=k+1 if i<len(left): while i<len(left): a[k]=left[i] i=i+1 k=k+1 else: while j<len(right): a[k]=right[j] j=j+1 k=k+1 # driver code a=[ ] print("enter size: ") n=int(input()) gen_data(a,n) print(" the numbers are :") print (a) merge_sort(a,0,n) print(" the sorted numbers are:") print (a)
9. Develop an android application to list all the engineering branches of MCE and displays a brief information of any department which the user clicks on in a separate page. package com.example.nine; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends AppCompatActivity { String[] str={"Select Branch","CSE","ME"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner sp=(Spinner) findViewById(R.id.spinner); ArrayAdapter<String> adp=new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,str); sp.setAdapter(adp); sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View v, int i, long l) { int index = adapterView.getSelectedItemPosition(); if(index==1) { Intent in1=new Intent(getApplicationContext(),MainActivity2.class); startActivity(in1); } if (index==2) { Intent ME=new Intent(getApplicationContext(),MainActivity3.class); startActivity(ME); } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } } MainActivity2: package com.example.nine; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity2 extends AppCompatActivity { Button b1; TextView t1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Button b1=(Button) findViewById(R.id.back); TextView t1=(TextView) findViewById(R.id.tview); t1.setText("The department of Computer Science and Engineering was established in the year 1983. It offers the 4 years (8 Semesters) B.E. course with an intake of 180 students."); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } } MainActivity3 package com.example.nine; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity3 extends AppCompatActivity { Button b1; TextView t1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); Button b1=(Button) findViewById(R.id.back3); TextView t1=(TextView) findViewById(R.id.tview3); t1.setText("The department of Mechanical Engineering (NBAAccredited) made its beginning with the establishment of the college in 1960. It offers a 4 years (8 semesters) B.E. course in Mechanical Engineering (Autonomous) with an intake of 120 students."); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); }}
7. Design an app to display menu options on clicking a button “FILE”. The menu options are: New, Open, Save, Save as, And Print. Clicking on any option should display the relevant information. package com.example.seven; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; import androidx.appcompat.widget.Toolbar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case 0: Toast.makeText(getApplicationContext(),"Opens a New File",Toast.LENGTH_LONG).show(); return true; case 1: Toast.makeText(getApplicationContext(),"Opens the existing file",Toast.LENGTH_LONG).show(); return true; case 2: Toast.makeText(getApplicationContext(),"saves as a existing file",Toast.LENGTH_LONG).show(); return true; case 3: Toast.makeText(getApplicationContext(),"Saves with the name",Toast.LENGTH_LONG).show(); return true; case 4: Toast.makeText(getApplicationContext(),"Prints the existing file",Toast.LENGTH_LONG).show(); return true; } return false; } }
6. Assume you need to accept order online for fast food items. Design an app such that it accepts the order for multiple items and displays the total amount to be paid on placing the order. package com.example.sixth; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { CheckBox c1, c2, c3; Button b1, b2; TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); c1 = (CheckBox)findViewById(R.id.cb1); c2 = (CheckBox)findViewById(R.id.cb2); c3 = (CheckBox)findViewById(R.id.cb3); b1 = (Button)findViewById(R.id.bill); b2 = (Button)findViewById(R.id.pay); tv1 = (TextView)findViewById(R.id.amount); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int total =0; if(c1.isChecked()){ total = total + 30; } if(c2.isChecked()){ total = total + 50; } if(c3.isChecked()){ total = total + 45; } tv1.setText("Total :"+String.valueOf(total)); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Redirecting to Payment", Toast.LENGTH_SHORT).show(); } }); } }
4. Design an app that displays the names of all planets in our universe. Clicking on “Solar System” in first activity should display all the planet’s names in second activity and it should return the total number of planets to first activity. MainActiviy1: package com.example.second; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button) findViewById(R.id.solar_page); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent conn = new Intent(getApplicationContext(),MainActivity2.class); startActivity(conn); startActivityForResult(conn,1); }}); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); TextView t1=(TextView)findViewById(R.id.textDisplay); if (requestCode==1) if (resultCode==RESULT_OK){ String res = data.getStringExtra("Planets"); t1.setText(String.valueOf(res)); }}} MainActivity2: package com.example.second; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity2 extends AppCompatActivity { Button b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); java.lang.String total="8"; b2=(Button) findViewById(R.id.back); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent in=getIntent(); in.putExtra("Planets",total); setResult(RESULT_OK,in); finish(); } });
10. Implement non-parametric Locally Weighted Regression algorithm in order to fit data point Select the appropriate data set for your experiment and draw graph. import pandas as pd import numpy as np import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt def local_regression(x0, X, Y, tau): x0 = [1, x0] X = [[1, i] for i in X] X = np.asarray(X) xw = (X.T) * np.exp(np.sum((X - x0) ** 2, axis=1) / (-2 * tau)) beta = np.linalg.pinv(xw @ X) @ xw @ Y @ x0 return beta def draw(tau): prediction = [local_regression(x0, X, Y, tau) for x0 in domain] plt.plot(X, Y, 'o', color='black') plt.plot(domain, prediction, color='red') plt.show() X = np.linspace(-3, 3, num=1000) domain = X Y = np.log(np.abs(X ** 2 - 1) + .5) draw(10) draw(0.1) draw(0.01) draw(0.001)
9. Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set. Print both correct and wrong predictions. from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import confusion_matrix,classification_report from sklearn import datasets iris=datasets.load_iris() iris_data=iris.data iris_labels=iris.target print(iris_data) x_train,X_test,Y_train,Y_test=train_test_split(iris_data,iris_labels,t est_size=0.20) classifier=KNeighborsClassifier(n_neighbors=5) classifier.fit(x_train,Y_train) y_prd=classifier.predict(X_test) print(confusion_matrix(Y_test,y_prd)) print(classification_report(Y_test,y_prd))
8. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for clustering using k-Means algorithm. Compare the results of these two algorithms and comment on the quality of clustering. import matplotlib.pyplot as plt from sklearn import datasets from sklearn.cluster import KMeans import pandas as pd import numpy as np # import some data to play with iris = datasets.load_iris() X = pd.DataFrame(iris.data) X.columns = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width'] y = pd.DataFrame(iris.target) y.columns = ['Targets'] # Build the K Means Model model = KMeans(n_clusters=3) model.fit(X) # model.labels_ : Gives cluster no for which samples belongs to # # Visualise the clustering results plt.figure(figsize=(14,14)) colormap = np.array(['red', 'lime', 'black']) # Plot the Original Classifications using Petal features plt.subplot(2, 2, 1) plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets], s=40) plt.title('Real Clusters') plt.xlabel('Petal Length') plt.ylabel('Petal Width') # Plot the Models Classifications plt.subplot(2, 2, 2) plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[model.labels_], s=40) plt.title('K-Means Clustering') plt.xlabel('Petal Length') plt.ylabel('Petal Width') # General EM for GMM from sklearn import preprocessing # transform your data such that its distribution will have a # mean value 0 and standard deviation of 1. scaler = preprocessing.StandardScaler() scaler.fit(X) xsa = scaler.transform(X) xs = pd.DataFrame(xsa, columns = X.columns) from sklearn.mixture import GaussianMixture plt.figure(figsize=(14,14)) colormap = np.array(['red', 'lime', 'black']) gmm = GaussianMixture(n_components=3) gmm.fit(xs) gmm_y = gmm.predict(xs) plt.subplot(2, 2, 3) plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[gmm_y], s=40) plt.title('GMM Clustering') plt.xlabel('Petal Length') plt.ylabel('Petal Width') print('Observation: The GMM using EM algorithm based clustering matched the true labels more closely than the Kmeans.')
7. Write a program to construct a Bayesian network considering medical data. Use this model to demonstrate the diagnosis of lungs patients using standard lungs Disease Data Set. import numpy as np import pandas as pd import csv from pgmpy.estimators import MaximumLikelihoodEstimator from pgmpy.models import BayesianModel from pgmpy.inference import VariableElimination heartDisease = pd.read_csv('heart.csv') heartDisease = heartDisease.replace('?',np.nan) print('Sample instances from the dataset are given below') print(heartDisease.head()) print(' Attributes and datatypes') print(heartDisease.dtypes) model= BayesianModel([('age','heartdisease'),('sex','heartdisease'),('exang','heartdisease'),('cp','heart disease'),('heartdisease','restecg'),('heartdisease','chol')]) print(' Learning CPD using Maximum likelihood estimators') model.fit(heartDisease,estimator=MaximumLikelihoodEstimator) print(' Inferencing with Bayesian Network:') HeartDiseasetest_infer = VariableElimination(model) print(' 1. Probability of HeartDisease given evidence= restecg') q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'age':1}) print(q1) print(' 2. Probability of HeartDisease given evidence= cp ') q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2}) print(q2)
6. Assuming a set of documents that need to be classified, use the naïve Bayesian Classifier model to perform this task. Calculate the accuracy, precision, and recall for your data set. import pandas as pd from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import CountVectorizer from sklearn.naive_bayes import MultinomialNB from sklearn import metrics msg=pd.read_csv('naive.csv',header=None,names=['message','label']) print("The dimensions of the dataset",msg.shape) msg['labelnum']=msg.label.map({'pos':1,'neg':0}) x=msg.message y=msg.labelnum xtrain,xtest,ytrain,ytest=train_test_split(x,y,random_state=1) count_vect=CountVectorizer() xtrain_dtm=count_vect.fit_transform(xtrain) xtest_dtm=count_vect.transform(xtest) clf=MultinomialNB().fit(xtrain_dtm,ytrain) predicted=clf.predict(xtest_dtm) print("Accuracy metrics:") print("Accuracy of the classifier is",metrics.accuracy_score(ytest,predicted)) print("Confusion matrix:") print(metrics.confusion_matrix(ytest,predicted)) print("Recall and Precision:") print(metrics.recall_score(ytest,predicted)) print(metrics.precision_score(ytest,predicted))
5. Write a program to implement the naïve Bayesian classifier for a sample training data set stored as a .CSV file. Compute the accuracy of the classifier, considering few test datasets. import pandas as pd from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB from sklearn import metrics df = pd.read_csv("pima_indian.csv") feature_col_names = ['num_preg', 'glucose_conc', 'diastolic_bp', 'thickness', 'insulin', 'bmi', 'diab_pred', 'age'] predicted_class_names = ['diabetes'] X = df[feature_col_names].values # these are factors for the prediction y = df[predicted_class_names].values # this is what we want to predict #splitting the dataset into train and test data xtrain,xtest,ytrain,ytest=train_test_split(X,y,test_size=0.33) print (' the total number of Training Data :',ytrain.shape) print (' the total number of Test Data :',ytest.shape) # Training Naive Bayes (NB) classifier on training data. clf = GaussianNB().fit(xtrain,ytrain.ravel()) predicted = clf.predict(xtest) predictTestData= clf.predict([[6,148,72,35,0,33.6,0.627,50]]) #printing Confusion matrix, accuracy, Precision and Recall print(' Confusion matrix') print(metrics.confusion_matrix(ytest,predicted)) print(' Accuracy of the classifier is',metrics.accuracy_score(ytest,predicted)) print(' The value of Precision', metrics.precision_score(ytest,predicted)) print(' The value of Recall', metrics.recall_score(ytest,predicted)) print("Predicted Value for individual Test Data:", predictTestData)
4. Build an Artificial Neural Network by implementing the Backpropagation algorithm and test the same using appropriate data sets. import numpy as np import matplotlib as m X=np.array(([2,9],[1,5],[3,6]),dtype=float) y=np.array(([92],[86],[89]),dtype=float) X=X/np.amax(X,axis=0) y=y/100 def sigmoid(x): return 1/(1+np.exp(-x)) def derivatives_sigmoid(x): return x*(1-x) epoch=7000 lr=0.1 inputlayer_neurons=2 hiddenlayer_neurons=3 output_neurons=1 wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons)) bh=np.random.uniform(size=(1,hiddenlayer_neurons)) wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons)) bout=np.random.uniform(size=(1,output_neurons)) for i in range(epoch): hinp1=np.dot(X,wh) hinp=hinp1+bh hlayer_act=sigmoid(hinp) outinp1=np.dot(hlayer_act,wout) outinp=outinp1+bout output=sigmoid(outinp) EO=y-output outgrad=derivatives_sigmoid(output) d_output=EO*outgrad EH=d_output.dot(wout.T) hiddengrad=derivatives_sigmoid(hlayer_act) d_hiddenlayer=EH*hiddengrad wout+=hlayer_act.T.dot(d_output)*lr wh+=X.T.dot(d_hiddenlayer)*lr print("Input: "+str(X)) print("Actual Output: "+str(y)) print("Predicted Output: ",output)
3.Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an appropriate data set for building the decision tree and apply this knowledge to classify a new sample. #Implementing Decision Tree #numpy and pandas initialization import numpy as np import pandas as pd #Loading the PlayTennis data PlayTennis = pd.read_csv("PlayTennis.csv") PlayTennis #It is easy to implement Decision Tree with numerical values. #We can convert all the non numerical values into numerical values using LabelEncoder from sklearn.preprocessing import LabelEncoder Le = LabelEncoder() PlayTennis['outlook'] = Le.fit_transform(PlayTennis['outlook']) PlayTennis['temp'] = Le.fit_transform(PlayTennis['temp']) PlayTennis['humidity'] = Le.fit_transform(PlayTennis['humidity']) PlayTennis['windy'] = Le.fit_transform(PlayTennis['windy']) PlayTennis['play'] = Le.fit_transform(PlayTennis['play']) PlayTennis #Lets split the training data and its coresponding prediction values. #y - holds all the decisions. #X - holds the training data. y = PlayTennis['play'] X = PlayTennis.drop(['play'],axis=1) # Fitting the model from sklearn import tree clf = tree.DecisionTreeClassifier(criterion = 'entropy') clf = clf.fit(X, y) # We can visualize the tree using tree.plot_tree tree.plot_tree(clf) #In Graph #X[0] -> Outlook #X[1] -> Temperature #X[2] -> Humidity #X[3] -> Wind # The predictions are stored in X_pred X_pred = clf.predict(X) # verifying if the model has predicted it all right. X_pred == y
2. For a given set of training data examples stored in a .CSV file, implement and demonstrate the Candidate-Elimination algorithm to output a description of the set of all hypotheses consistent with the training examples. import numpy as np import pandas as pd # Loading Data from a CSV File data = pd.DataFrame(data=pd.read_csv('2b.csv')) # Separating concept features from Target concepts = np.array(data.iloc[:,0:-1]) # Isolating target into a separate DataFrame #copying last column to target array target = np.array(data.iloc[:,-1]) def learn(concepts, target): #learn() function implements the learning method of the Candidate elimination algorithm. #Arguments: #concepts - a data frame with all the features #target - a data frame with corresponding output values # Initialise S0 with the first instance from concepts # .copy() makes sure a new list is created instead of just pointing to the same memory location specific_h = concepts[0].copy() print("initialization of specific_h and general_h") print(specific_h) general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_h))] print(general_h) # The learning iterations for i, h in enumerate(concepts): # Checking if the hypothesis has a positive target if target[i] == "Yes": for x in range(len(specific_h)): # Change values in S & G only if values change if h[x] != specific_h[x]: specific_h[x] = '?' general_h[x][x] = '?' # Checking if the hypothesis has a positive target if target[i] == "No": for x in range(len(specific_h)): # For negative hyposthesis change values only in G if h[x] != specific_h[x]: general_h[x][x] = specific_h[x] else: general_h[x][x] = '?' print(" steps of Candidate Elimination Algorithm",i+1) print("Specific_h ",i+1," ") print(specific_h) print("general_h ", i+1, " ") print(general_h) # find indices where we have empty rows, meaning those that are unchanged indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']] for i in indices: # remove those rows from general_h general_h.remove(['?', '?', '?', '?', '?', '?']) # Return final values return specific_h, general_h s_final, g_final = learn(concepts, target) print("Final Specific_h:", s_final, sep=" ") print("Final General_h:", g_final, sep=" ")
1. Implement and demonstrate the FIND-S algorithm for finding the most specific hypothesis based on a given set of training data samples. Read the training data from av.CSV file. import csv with open('lab1a.csv', 'r') as f: reader = csv.reader(f) your_list = list(reader) h = [['0', '0', '0', '0', '0', '0']] for i in your_list: print(i) if i[-1] == "True": j = 0 for x in i: if x != "True": if x != h[0][j] and h[0][j] == '0': h[0][j] = x elif x != h[0][j] and h[0][j] != '0': h[0][j] = '?' else: pass j = j + 1 print("Most specific hypothesis is") print(h)