datamining lab

86
CHALAPATHI INSTITUTE OF TECHNOLOGY 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: outloo k temperat ure humidit y windy play overca st 85 85 FALSE no sunny 80 90 TRUE no overca st 83 86 FALSE yes rainy 70 96 FALSE yes rainy 68 80 FALSE yes rainy 65 70 TRUE no overca st 64 65 TRUE yes sunny 72 95 FALSE no sunny 69 70 FALSE yes rainy 75 80 FALSE yes sunny 75 80 FALSE yes overca st 72 90 TRUE yes overca 81 75 FALSE yes 1

Upload: santhipriyasunkara

Post on 18-Nov-2015

34 views

Category:

Documents


5 download

DESCRIPTION

lab for m.tech students

TRANSCRIPT

CHALAPATHI INSTITUTE OF TECHNOLOGY

Experiment - IAim:

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 humiditywindyplay

overcast8585FALSEno

sunny8090TRUEno

overcast8386FALSEyes

rainy7096FALSEyes

rainy6880FALSEyes

rainy6570TRUEno

overcast6465TRUEyes

sunny7295FALSEno

sunny6970FALSEyes

rainy7580FALSEyes

sunny7580FALSEyes

overcast7290TRUEyes

overcast8175FALSEyes

rainy7191TRUEno

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.

Select Explorer option. The Explorer screen will be shown below:

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.

Select Attribute:

Select most relevant aspects in the dataset.

Visualize:

View different 2-dimensional plots of the data & interact with them.

Classify:

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.

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.

J48 appears in the line beside the choose button as shown in the figure below:

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

=== Classifier model (full training set) ===

J48 pruned tree

------------------

outlook = sunny

|humidity 75: no (3.0)

outlook = overcast: yes (4.0)

outlook = rainy

|windy = TRUE: no (2.0)

|windy = FALSE: yes (3.0)

Number of Leaves : 5

Size of the tree : 8

Time taken to build model: 0.02 seconds

=== Stratified cross-validation ===

=== Summary ===

Correctly Classified Instances 9 64.2857 %

Incorrectly Classified Instances 5 35.7143 %

Kappa statistic 0.186

Mean absolute error 0.2857

Root mean squared error 0.4818

Relative absolute error 60 %

Root relative squared error 97.6586 %

Total Number of Instances 14

=== Detailed Accuracy By Class ===

TP Rate FP Rate Precision Recall F-Measure ROC Area Class

0.778 0.6 0.7 0.778 0.737 0.789 yes

0.4 0.222 0.5 0.4 0.444 0.789 no

=== Confusion Matrix ===

a b Item list=i2,i4 1 conf:(1)

5. Trans ID=t3 1 ==> Item list=i2,i3 1 conf:(1)

6. Item list=i1,i2,i4 1 ==> Trans ID=t4 1 conf:(1)

7. Trans ID=t4 1 ==> Item list=i1,i2,i4 1 conf:(1)

8. Trans ID=t5 1 ==> Item list=i1,i3 1 conf:(1)

9. Trans ID=t6 1 ==> Item list=i2,i3 1 conf:(1)10. Trans ID=t7 1 ==> Item list=i1,i3 1 conf:(1)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

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();

}

while(y 'Z')

c = c - 26;

}

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

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;iiph_ver = 4;

ip->iph_tos = 16;

ip->iph_len = sizeof(struct ipheader) + sizeof(struct

tcpheader);

ip->iph_ident = htons(54321);

ip->iph_offset = 0;

ip->iph_ttl = 64;

ip->iph_protocol = 6; // TCP

ip->iph_chksum = 0; // Done by kernel

// Source IP, modify as needed, spoofed, we accept through

command line argument

ip->iph_sourceip = inet_addr(argv[1]);

// Destination IP, modify as needed, but here we accept

through command line argument

ip->iph_destip = inet_addr(argv[3]);

// The TCP structure. The source port, spoofed, we accept

through the command line

tcp->tcph_srcport = htons(atoi(argv[2]));

// The destination port, we accept through command line

tcp->tcph_destport = htons(atoi(argv[4]));

tcp->tcph_seqnum = htonl(1);

tcp->tcph_acknum = 0;

tcp->tcph_offset = 5;

tcp->tcph_syn = 1;

tcp->tcph_ack = 0;

tcp->tcph_win = htons(32767);

tcp->tcph_chksum = 0; // Done by kernel

tcp->tcph_urgptr = 0;

// IP checksum calculation

ip->iph_chksum = csum((unsigned short *) buffer,

(sizeof(struct ipheader) + sizeof(struct tcpheader)));

// Inform the kernel do not fill up the headers' structure,

we fabricated our own

if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one))

< 0)

{

perror("setsockopt() error");

exit(-1);

}

else

printf("setsockopt() is OK\n");

printf("Using:::::Source IP: %s port: %u, Target IP: %s

port: %u.\n", argv[1], atoi(argv[2]), argv[3],

atoi(argv[4]));

// sendto() loop, send every 2 second for 50 counts

unsigned int count;

for(count = 0; count < 20; count++)

{

if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr

*)&sin, sizeof(sin)) < 0)

// Verify

{

perror("sendto() error");

exit(-1);

}

else

printf("Count #%u - sendto() is OK\n", count);

sleep(2);

}

close(sd);

return 0;

}RESULT :

Thus the Above programs using raw sockets TCP \IP (like packet capturing and

filtering) was executed and successfully.

EX No 3: SIMULATION OF SLIDING WINDOW PROTOCOL

AIM:

To write a C program to perform sliding window.

PROGRAM :

// SLIDING WINDOW PROTOCOL Client:

#include

#include

#include

#include

#include

#include

struct mymsgbuf

{

long mtype;

char mtext[25];

};

FILE *fp;

int main()

{

struct mymsgbuf buf;

int msgid;

int i=0,s;

int count=0,frmsz;

int a[100];

char d;

if((msgid=msgget(89,IPC_CREAT|0666))==-1)

{

printf("\n ERROR IN MSGGET");

exit(0);

}

printf("\n Enter the frame size:");

scanf("%d",&frmsz);

if((fp=fopen("check","r"))==NULL)

printf("\n FILE NOT OPENED");

else

printf("\n FILE OPENED");

while(!feof(fp))

{

d=getc(fp);

a[i]=d;

i++;

}

s=i;

for(i=0;i