Professional Documents
Culture Documents
Experiment - I
Aim:
To construct a Decision tree.
Description:
Suppose we have some data and we want to build a decision tree from the data.
First, we need to prepare the data then fire up explorer and load in the data. Next, we select a decision
tree construction method. Build a tree and interpret the output.
Input:
Prepare the data as follows:
outlook
temperature
humidity
windy
play
overcast
sunny
overcast
rainy
rainy
rainy
overcast
sunny
sunny
rainy
sunny
overcast
overcast
rainy
85
80
83
70
68
65
64
72
69
75
75
72
81
71
85
90
86
96
80
70
65
95
70
80
80
90
75
91
FALSE
TRUE
FALSE
FALSE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
TRUE
no
no
yes
yes
yes
no
yes
no
yes
yes
yes
yes
yes
no
Save this book.csv and open csv (cmd delimited).Load the data into Explorer
Lets load the data into Explorer and start analyzing. Pick up WEKA to get the panel shown below.
Click on Applications Menu.
In the Explorer screen 6 tabs along with the top are the basic operations that the explorer supports.
Right now, we are on the Preprocess tab.
Preprocess:
Choose the data set and modify it in various ways.
Classify:
Train learning schemes that perform classification, regression & evaluate them.
Cluster:
Learn clusters for the dataset.
Association:
Learn association rules for data and evaluate them.
This tells you about the datasets. It has 14 instances and 5 attributes.
The attributes are :
Outlook
Temperature
Humidity
Windy
Play
The first attribute outlook is selected by default. It has no missing values.
The actual values are sunny, overcast, rainy and they occur 5, 4, 5 times respectively.
A histogram at the lower right shows how often each of the 2 values of the class outlook which is
as shown in above figure.
If you click the line GUI, generic object editor is displayed. Having classify as the button.
Click on the start button.
Examining the output:
The figure below shows the output. At the beginning is a summary of the dataset and the
fact that 10-fold cross-validation is used to evaluate it. Then comes a pruned decision-tree in textual
form. The first split is on the outlook attribute and then second level. The splits are on humidity, windy
respectively. In the tree structure a colon introduced a class that has been assigned, designed a particular
leaf followed by the number of instances that reach the leaf.
Classifier Output:
By clicking the start button the output is as follows:
=== Run information ===
Scheme:
weka.classifiers.trees.J48 -C 0.25 -M 2
Relation: weather
Instances: 14
Attributes: 5
outlook
temperature
humidity
windy
play
Test mode: 10-fold cross-validation
9
5
64.2857 %
35.7143 %
0.186
0.2857
0.4818
60
%
97.6586 %
14
Tree View:
10
Experiment - II
Aim:
To construct a NaiveBayes Network
Description:
Suppose we have some data and we want to build a NaiveBayes from the data.
First, we need to prepare the data then fire up explorer and load in the data. Next, we select a
NaiveBayes construction method. Build a NaiveBayes Network and interpret the output.
Input:
Prepare the data as follows:
Outlook temperature humidity windy
play
overcast
Sunny
overcast
Rainy
Rainy
Rainy
overcast
Sunny
Sunny
Rainy
Sunny
overcast
overcast
Rainy
no
no
yes
yes
yes
no
yes
no
yes
yes
yes
yes
yes
no
85
80
83
70
68
65
64
72
69
75
75
72
81
71
85
90
86
96
80
70
65
95
70
80
80
90
75
91
FALSE
TRUE
FALSE
FALSE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
TRUE
Save this book.csv and open csv (cmd delimited).Load the data into Explorer
11
Lets load the data into Explorer and start analyzing. Pick up WEKA to get the panel shown below.
Click on Applications Menu.
In the Explorer screen 6 tabs along with the top are the basic operations that the explorer supports.
Right now, we are on the Preprocess tab.
First, select the classify tab in the explorer. Then choose button at the top left of the opening
tree section of the hierarchical menu shown below. The menu structure represents the organization of
WEKA code into modules.
Click on Open file, Select appropriate data file. The Explorer having the loaded file, the
screen will be as shown below.
Suppose the open file is weather.arff.
13
This tells you about the datasets. It has 14 instances and 5 attributes.
The attributes are :
Outlook
Temperature
Humidity
Windy
Play
The first attribute outlook is selected by default. It has no missing values.
The actual values are sunny, overcast, rainy and they occur 5, 4, 5 times respectively.
A histogram at the lower right shows how often each of the 2 values of the class outlook which is
as shown in above figure.
NaiveBayes appears in the line beside the choose button as shown in the figure below:
14
If you click the line GUI, generic object editor is displayed. Having classify as the button. Click
on the start button.
Examining the output:
The figure below shows the output. At the beginning is a summary of the dataset and the
fact that 10-fold cross-validation is used to evaluate it. Then comes a pruned decision-tree in textual
form. The first split is on the outlook attribute and then second level. The splits are on humidity, windy
respectively. In the tree structure a colon introduced a class that has been assigned, designed a particular
leaf followed by the number of instances that reach the leaf.
15
Classifier Output :
=== Run information ===
Scheme:
weka.classifiers.bayes.NaiveBayes
Relation: weather
Instances: 14
Attributes: 5
outlook
temperature
humidity
windy
play
Test mode: 10-fold cross-validation
=== Classifier model (full training set) ===
16
17
18
19
Experiment III
Aim:
To construct a Bayes Network
Description:
Suppose we have some data and we want to build a Bayes Network from the data.
First, we need to prepare the data then fire up explorer and load in the data. Next, we select a Bayes
Network construction method. Build a Bayes Network and interpret the output.
Input:
Prepare the data as follows:
outlook
play
overcast
sunny
overcast
rainy
rainy
rainy
overcast
sunny
sunny
rainy
sunny
overcast
overcast
rainy
85
80
83
70
68
65
64
72
69
75
75
72
81
71
no
no
yes
yes
yes
no
yes
no
yes
yes
yes
yes
yes
no
85
90
86
96
80
70
65
95
70
80
80
90
75
91
FALSE
TRUE
FALSE
FALSE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
TRUE
Save this book.csv and open csv (cmd delimited).Load the data into Explorer
20
Lets load the data into Explorer and start analyzing. Pick up WEKA to get the panel shown below.
Click on Applications Menu.
In the Explorer screen 6 tabs along with the top are the basic operations that the explorer supports.
Right now, we are on the Preprocess tab.
First, select the classify tab in the explorer. Then choose button at the top left of the opening
tree section of the hierarchical menu shown below. The menu structure represents the organization of
WEKA code into modules.
Click on Open file, Select appropriate data file. The Explorer having the loaded file, the
screen will be as shown below.
22
This tells you about the datasets. It has 14 instances and 5 attributes.
23
If you click the line GUI, generic object editor is displayed. Having classify as the button. Click
on the start button.
24
Classifier Output :
=== Run information ===
Scheme:
weka.classifiers.bayes.BayesNet -D -Q weka.classifiers.bayes.net.search.local.K2 -- -P 1
-S BAYES -E weka.classifiers.bayes.net.estimate.SimpleEstimator -- -A 0.5
Relation: weather
Instances: 14
Attributes: 5
outlook
temperature
humidity
windy
play
25
26
Visualize Graph :
27
28
29
Experiment IV
Aim:
To perform classification
Description:It is necessary to provide a clear classification of data mining systems which may help
users to distinguish between such systems and to identify them.
Data mining classification can be done in different ways:
1) Data mining can be classified according to the kinds of databases mined
2) Data mining can be Classified according to the kinds of knowledge mined which is done
based on the mining functionalities like characterization, discrimination etc
3) We can also classify the data mining systems according to the kinds of techniques utilized,
applications adapted.
Input:
customer ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
age
youth
youth
middle
senior
senior
senior
middle
youth
youth
senior
youth
middle
middle
senior
income
high
high
high
medium
low
low
low
medium
low
medium
medium
medium
high
Medium
student
no
no
no
no
yes
yes
yes
no
yes
yes
yes
no
yes
no
credit rating
fair
excellent
fair
fair
fair
excellent
excellent
fair
fair
fair
excellent
excellent
fair
excellent
class By computer
no
no
yes
yes
yes
no
yes
no
yes
yes
yes
yes
yes
no
\
FIGURE 1: INPUT FILE (CLASSIFY.CSV)
30
After loading the input file named classify.csv as shown in figure 2, choose the classify tab in the
WEKA explorer window.
Under the classify tab click on choose button and select the Multilayer Perceptron under
functions as shown in the following figure 3.
31
Now select the use training set under the Test Options located at the left of the WEKA
explorer window and click on start button.
The output is represented in the Classifier Output window in weka explorer window, which is
as shown in figure 4 below:
32
14
0
100
%
Kappa statistic
1
Mean absolute error
0.0242
Root mean squared error
0.0349
Relative absolute error
5.2211 %
Root relative squared
7.2691
error
%
Total Number of
14
Instances
=== Detailed Accuracy By Class ===
TP Rate FP Rate Precision Recall F-Measure ROC Area Class
1
0 1
1
1
1
no
1
0 1
1
1
1
yes
Weighted
1 0
1
1
1
1
Avg.
=== Confusion Matrix ===
a b <-- classified as
5 0 | a = no
0 9 | b = yes
35
Experiment V
Aim:
To perform clustering
Description:
Cl ustering is a task of assigning a set of objects into groups called as clusters.
Clustering is also referred as cluster analysis where the objects in the same cluster are more similar
to each other than to those objects in other clusters.
Clustering is the main task of Explorative Data mining and is a common technique for statistical data
analysis used in many fields like machine learning, pattern recognition, image analysis, bio informatics
etc
Cluster analysis is not an algorithm but is a general task to be solved.
Clustering is of different types like hierarchical clustering which creates a hierarchy of
clusters, partial clustering, and spectral clustering.
Input:
customer ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
age
youth
youth
middle
senior
senior
senior
middle
youth
youth
senior
youth
middle
middle
senior
income
high
high
high
medium
low
low
low
medium
low
medium
medium
medium
high
Medium
student
no
no
no
no
yes
yes
yes
no
yes
yes
yes
no
yes
no
credit rating
fair
excellent
fair
fair
fair
excellent
excellent
fair
fair
fair
excellent
excellent
fair
excellent
class By computer
no
no
yes
yes
yes
no
yes
no
yes
yes
yes
yes
yes
No
36
After loading the input file named cluster.csv as shown in figure 2, choose the cluster tab in the
WEKA explorer window.
Under the cluster tab click on choose button and select the SimpleKMeans under clusterers
as shown in the following figure 3.
37
Now select the use training set under the Test Options located at the left of the WEKA
explorer window and click on start button.
The output is represented in the Clusterer Output window in weka explorer window, which is
as shown in figure 4 below:
38
Cluster#
Attribute
Full Data
0
1
(14)
(7)
(7)
=================================================
customer ID
7.5
8.5714 6.428
youth
youth
6
age income
mediu
mediu middle
student
m no
m yes high
credit
fair
fair
rating
no
class By computer
yes
yes
Clustered Instances
0
1
7 ( 50%)
7 ( 50%)
40
Experiment VI
Aim:
To perform Association Rule.
It is a popular method for discovering interesting relations between variables in
large databases.
It is used to identify the most frequent item sets or frequent patterns that occur frequently in a
database.
To perform association rule mining we need two parameters like:
Support: - It is the total probability of the item sets that occurred in the transaction.
Confidence: - it is the ratio of probability of two item sets A,B and the probability of the item set A.
Association rule mining is used in different applications like basket analysis, catalog
design, clustering, classification etc
Input:
Trans ID
t1
t2
t3
t4
t5
t6
t7
t8
t9
Item list
i1,i2,i5
i2,i4
i2,i3
i1,i2,i4
i1,i3
i2,i3
i1,i3
i1,i2,i3,i5
i1,i2,i3
41
After loading the input file named association.csv as shown in figure 2, choose the associate
tab in the WEKA explorer window.
Under the associate tab click on choose button and select the Apriori Algorithm under
associations as shown in the following figure 3.
42
We can also view the output in a separate window by right clicking on the option in Result list
and clicking on view in separate window as shown in figure 5.
43
AIM:
Write a program in java, which performs a digital signature on a given text.
PROGRAM:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;
import sun.misc.BASE64Encoder;
public class DigSign {
public static void main(String[] args) throws Exception {
// TODO code application logic here
KeyPairGeneratorkpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPairkeyPair = kpg.genKeyPair();
byte[] data = "Sample Text".getBytes("UTF8");
Signature sig = Signature.getInstance("MD5WithRSA");
sig.initSign(keyPair.getPrivate());
sig.update(data);
byte[] signatureBytes = sig.sign();
System.out.println("Signature: \n" + new BASE64Encoder().encode(signatureBytes));
sig.initVerify(keyPair.getPublic());
sig.update(data);
System.out.println(sig.verify(signatureBytes));
}
}
OUTPUT:
Signature:
imwaKe99tkM6H6hiiP0rubmb/MrYJZLi
wLdRSjslF2KlA5B23az5M2LKftQFCB+NH
Ce5F5/YfN8OsNSNLtucrrZTah0SrdWSzdGCOfYLdUZmPQ72j1SkLhYspsTsUb/U
45
AIM:
Write a Java program to implement RSA Algoithm.
PROGRAM:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.Random;
import java.util.Scanner;
public class RSA {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Enter a Prime number: ");
BigInteger p = sc.nextBigInteger(); // Here's one prime number..
System.out.print("Enter an
other prime number: ");
BigInteger q = sc.nextBigInteger(); // ..and another.
BigInteger n = p.multiply(q);
BigInteger n2 = p.subtract(BigInteger.ON
E).multiply(q.subtract(BigInteger.ONE));
BigInteger e = generateE(n2);
BigInteger d = e.modInverse(n2); // Here's the multiplicative inverse
System.out.println("Encryption keys are: " + e + ", " + n);
System.out.println("Decryption keys are: " + d + ", " + n);
}
public static BigIntegergenerateE(BigIntegerfiofn) {
int y, intGCD;
BigInteger e;
BigInteger gcd;
Random x = new Random();
do {
y = x.nextInt(fiofn.intValue()-1);
String z = Integer.toString(y);
e = new BigInteger(z);
gcd = fiofn.gcd(e);
intGCD = gcd.intValue();
46
}
while(y <= 2 || intGCD != 1);
return e;
}
}
OUTPUT:
Enter a Prime number: 5
Enter another prime number: 11
Encryption keys are: 33, 55
Decryption keys are: 17, 55
47
AIM:
Write a Java program to implement the DES algorithm logic.
PROGRAM:
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.s
pec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DES {
private static final String UNICODE_FORMAT = "UTF8";
public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
privateKeySpecmyKeySpec;
privateSecretKeyFactorymySecretKeyFactory;
private Cipher cipher;
byte[] keyAsBytes;
private String myEncryptionKey;
private String myEncryptionScheme;
SecretKey key;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public DES() throws Exception {
// TODO code application logic here
myEncryptionKey = "ThisIsSecretEncryptionKey";
myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
myKeySpec = new DESedeKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFacto
ry.getInstance(myEncryptionScheme);
cipher = Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
}
48
OUTPUT:
Enter the string: Welcome
String To Encrypt: Welcome
Encrypted Value : BPQMwc0wKvg=
Decrypted Value : Welcome
50
AIM:
Write a Java program to perform encryption and decryption using the
following algorithms:
a)Ceaser Cipher
b)Substitution Cipher
c)Hill Cipher
PROGRAM:
a)Ceaser Cipher
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CeaserCipher {
static Scanner sc=new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.print("En
ter any String: ");
String str = br.readLine();
System.out.print("\nEnter the Key: ");
int key = sc.nextInt();
String encrypted = encrypt(str, key);
System.out.println("\nEncrypted String is: " +encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println("\nDecrypted String is: " +decrypted);
System.out.println("\n");
}
public static String encrypt(String str, int key) {
String encrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
51
}
else if (Character.isLowerCase(c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key) {
String decrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
}
else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}
Output:
Enter any String: Hello World
Enter the Key: 5
Encrypted String is: MjqqtBtwqi
Decrypted String is: Hello World
52
b)Substitution Cipher
PROGRAM:
import java.io.*;
import java.util.*;
public class SubstitutionCipher {
static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// TODO code application logic here
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
System.out.print("En
ter any string: ");
String str = br.readLine();
String decrypt = "";
char c;
for(int i=0;i<str.length();i++)
{
c = str.charAt(i);
int j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
}
System.out.println("The encr
ypted data is: " +decrypt);
}
}
Output:
Enter any string: aceho
The encrypted data is: zxvsl
53
C) Hill Cipher
PROGRAM:
import java.io.*;
import java.util.*;
import java.io.*;
public class HillCipher {
static float[][] decrypt = new float[3][1];
static float[][] a = new float[3][3];
static float[][] b = new float[3][3];
static float[][] mes = new float[3][1];
static float[][] res = new float[3][1];
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
// TODO code application logic here
getkeymes();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
res[i][j]=res[i][j]+a[i][k]*mes[k][j]; }
System.out.print("\nEncrypted string is : ");
for(int i=0;i<3;i++) {
System.out.print((char)(res[i][0]%26+97));
res[i][0]=res[i][0];
}
inverse();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]; }
System.out.print("\nDecr
ypted string is : ");
for(int i=0;i<3;i++){
System.out.print((char)(decrypt[i][0]%26+97));
}
System.out.print("\n");
}
54
}}
Output:
Enter a 3 letter string: hai
Encrypted string is :fdx
Inverse Matrix is :
0.083333336 0.41666666 -0.33333334
-0.41666666 -0.083333336 0.6666667
0.5833333 -0.083333336 -0.33333334
Decrypted string is :hai
56
AIM:
To write a C program to develop a DNS client server to resolve the given
hostname.
Program :
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc,char *argv[1])
{
struct hostent *hen;
if(argc!=2)
{
fprintf(stderr,"Enter the hostname \n");
exit(1);
}
hen=gethostbyname(argv[1]);
if(hen==NULL)
{
fprintf(stderr,"Host not found \n");
}
printf("Hostname is %s \n",hen->h_name);
printf("IP address is %s \n",inet_ntoa(*((struct in_addr *)hen->h_addr)));
}
RESULT:
Thus the above program udp performance using domain name server was
executed and successfully
EX NO 2 :
PROGRAMS USING RAW SOCKETS (LIKE PACKET CAPTURING AND
FILTERING)
57
AIM :
To implement programs using raw sockets (like packet capturing and filtering)
//---cat rawtcp.c--// Run as root or SUID 0, just datagram no data/payload
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
// Packet length
#define PCKT_LEN 8192
// May create separate header file (.h) for all
// headers' structures
// IP header's structure
struct ipheader {
unsigned char iph_ihl:5, /* Little-endian */
iph_ver:4;
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
unsigned char iph_flags;
unsigned short int iph_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};
/* Structure of a TCP header */
struct tcpheader {
unsigned short int tcph_srcport;
unsigned short int tcph_destport;
unsigned int tcph_seqnum;
unsigned int tcph_acknum;
unsigned char tcph_reserved:4, tcph_offset:4;
// unsigned char tcph_flags;
unsigned int
tcp_res1:4, /*little-endian*/
tcph_hlen:4, /*length of tcp header in 32-bit
words*/
tcph_fin:1, /*Finish flag "fin"*/
tcph_syn:1, /*Synchronize sequence numbers to
start a connection*/
tcph_rst:1, /*Reset flag */
58
61
64
Program :
#include <stdio.h>
#include<conio.h>
int main()
{
int n;
int i,j,k;
int a[10][10],b[10][10];
printf("\n Enter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter the distance between the host %d - %d:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
}
}
}
66
67
68
69