DBDM-AI-DEV PDF
DBDM-AI-DEV PDF
:1
DATABASE DEVELOPMENT LIFE CYCLE-CASE STUDY
Date:
AIM
To learn about the phases involved in Database Development Lifecycle
The Database Life Cycle (DBLC) contains six phases, as shown in the following
Figure: database initial study, database design, implementation and loading, testing
and evaluation, operation, and maintenance and evolution.
1
The systems analysts or systems programmers are in charge of designing the
other system components. Their activities create the procedures that will help
transform the data within the database into useful information.
3. Implementation and Loading:
The output of the database design phase is a series of instructions detailing the
creation of tables, attributes, domains, views, indexes, security constraints, and
storage and performance guidelines.
4. Testing and Evaluation:
In the design phase, decisions were made to ensure integrity, security, performance,
and recoverability of the database. In testing and evaluation, the DBA tests and fine-
tunes the database to ensure that it performs as expected. This phase occurs in
conjunction with applications programming.
5. Operation
Once the database has passed the evaluation stage, it is considered to be operational.
At that point, the database, its management, its users, and its application programs
constitute a complete information system. The beginning of the operational phase
invariably starts the process of system evolution.
6. Maintenance and Evolution
The database administrator must be prepared to perform routine maintenance
activities within the database. Some of the required periodic maintenance activities
include:
Preventive maintenance (backup).
Corrective maintenance (recovery).
Adaptive maintenance (enhancing performance, adding entities and
attributes, and so on).
Assignment of access permissions and their maintenance for new and
old users.
RESULT
Thus various phases of Database Development cycle have been studied.
2
Ex.No.:2
DATABASE DESIGN USING ER MODELING-CASE STUDY
Date:
AIM
ER-EER MODELING
Entity Relationship Modeling (ER Modeling) is a graphical approach to database
design. It uses Entity/Relationship to represent real world objects.
Enhanced Entity Relationship (EER) Model is a high level data model which
provides extensions to original Entity Relationship (ER) model. EER Models
supports more details design. EER Modeling emerged as a solution for modeling
highly complex databases.
EER uses UML notation. UML is the acronym for Unified Modeling Language; it is
a general purpose modeling language used when designing object oriented systems.
Entities are represented as class diagrams. Relationships are represented as
associations between entities. The diagram shown below illustrates an ER diagram
using the UML notation.
It is deduced that the nature of the relationship between members and payments entities
is one-to-many. Now EER model is created using MySQL Workbench In the MySQL
workbench , Click - "+"Button
In this case study MyFlix Video Library is used to understand the concept of ER
diagrams. MyFlix is a business entity that rents out movies to its members. MyFlix has
been storing its records manually. The management now wants to move to a DBMS.
3
The steps to develop EER diagram for this database are
1. Identify the entities and determine the relationships that exist among them.
2. Each entity, attribute and relationship, should have appropriate names that
can be easilyunderstood by the non-technical people as well.
3. Relationships should not be connected directly to each other. Relationships
should connect entities.
4. Each attribute in a given entity should have a unique name.
From the above scenario, it is understood that the nature of the relationship is many-to-
many. Relational databases do not support many-to-many relationships. Hence a
junction entity is introduced. This is the role that the MovieRentals entity plays. It has
a one-to-many relationship with the members table and another one-to-many
relationship with movies table.
In the MySQL workbench, Double click on Add Diagram button to open the
workspace for ER diagrams.
4
Following window appears
The table object allows us to create entities and define the attributes
associated with the particular entity.
The relationship button allows us to define relationships between entities.
Membership number
Full names
Gender
Date of birth
Physical address
Postal address
5
Create the members table
2. Drop it in the workspace area. An entity named table 1 appears 3.Double click on it.
The properties window shown below appears
Next,
1. Change table 1 to Members
2. Edit the default idtable1 to membership_number
3. Click on the next line to add the next field
4. Do the same for all the attributes identified in members' entity.
6
Repeat the above steps for all the identified entities.
The diagram workspace should now look like the one shown below.
7
Repeat above steps for other relationships. The ER diagram should now look like this -
Summary
RESULT
Thus the database design using conceptual modeling (ER-EER) and map
it to relational database is performed successfully.
8
Ex.No.:3 A)
IMPLEMENT THE DATABASE USING DDL AND DML
COMMANDS IN POSTGRE SQL
Date:
AIM
To implement a database using DDL and DML commands.
POSTGRESQL
PostrgeSQL is an advanced relational database system.
It supports both relational (SQL) and non-relational (JSON) queries.
PostgreSQL is free and open-source.
DDL COMMANDS
DDL is an abbreviation for Data Definition Language. It is concerned with
database schemas and descriptions of how data should be stored in the database. DDL
statements are auto-committed, meaning the changes are immediately made to the
database and cannot be rolled back.
The DDL commands in SQL are divided into following major categories:
CREATE
ALTER
TRUNCATE
DROP
CREATE
The CREATE query is used to create a database or objects such as tables, views,
stored procedures, etc.
CREATE DATABASE
The CREATE DATABASE statement is used to create a new SQL database.
Syntax
CREATE DATABASE databasename;
Example
CREATE DATABASE testDB;
CREATE TABLE
The CREATE TABLE statement is used to create a table in database.
Syntax
CREATE TABLE tablename(attributename1 datatype1,attributename2
datatype2);
Example
create table student(rollno int,name char(10),dept char(10),marks int);
ALTER
The ALTER command in SQL DDL is used to modify the structure of an already
existing table.
ADDING A NEW COLUMN
The ALTER command is used to add a new column in an existing database.
Syntax
ALTER TABLE tablename ADD attibutename datatypename;
Example
alter table student add age int;
9
MODIFYING AN EXISTING COLUMN
The ALTER command is used to modify the datatype and its allocated size of an
existing column.
Syntax
ALTER TABLE tablename ALTER COLUMN columnane datatype;
Example
alter table student alter column name type char(20);
RENAME
RENAME is a DDL command which is used to change the name of the database
table.
Syntax
ALTER TABLE OldTableName RENAME TO NewTableName;
Example
alter table student rename to stud;
TRUNCATE
The TRUNCATE command is used to remove all the records from a table.
Syntax
TRUNCATE TABLE tablename;
Example
truncate table student;
DROP
The DROP command is used to delete an existing database or an object within a
database.
DROP DATABASE
The DROP DATABASE statement is used to drop an existing SQL database.
Syntax
DROP DATABASE databasename;
Example
DROP DATABASE testDB;
DROP TABLE
The DROP TABLE statement is used to delete a table from a database.
Syntax
DROP TABLE tablename;
Example
drop table student;
DML COMMANDS
The DML commands in Structured Query Language change the data present in the SQL
database. DML commands are used to access, store, modify, update and delete the
existing records from the database.
The DML commands in SQL are divided into following major categories:
SELECT
INSERT
UPDATE
DELETE
SELECT
SELECT is the most important data manipulation command in Structured
Query Language. The SELECT command shows the records of the specified table.
10
It also shows the particular record of a particular column by using the WHERE
clause.
Syntax
SELECT * FROM tablename;
Example
SELECT * FROM Student;
SELECT EmpId, EmpSalary FROM Employee;
INSERT
The INSERT command is used to insert data in database tables.
Syntax
INSERT into TABLENAME values (attributename1,attributename2….);
Example
INSERT into STUDENT values (102,'lenin','ai',95); (or)
INSERT into STUDENT (rollno,name,dept,marks)values(107,'vinai','ece',99);
UPDATE
The UPDATE command is used to update or modify the existing data in database
tables.
Syntax
UPDATE tablename SET columnname1= value1 WHERE condition;
Example
UPDATE Product SET ProductPrice = 80 WHERE ProductId = 'P102' ;
DELETE
DELETE is a DML command which allows SQL users to remove single or
multiple existing records from the database tables.
Syntax
DELETE FROM tableName WHERE condition;
Example
DELETE FROM Product WHERE ProductId = 'P202' ;
11
alter table student alter column name type char(25);
12
select * from student;
ERROR: relation "student" does not exist LINE 1: select * from student; ^ SQL state:
42P01 Character: 15
select * from stud;
RESULT
Thus the DDL and DML commands were implemented and the data was
retrieved from the database successfully.
13
Ex.No.:3 B)
IMPLEMENT THE DATABASE WITH CONSTRAINTS
Date:
AIM
To implement the database with constraints using PosgreSQL.
CONSTRAINTS
Constraints in DBMS (Database Management Systems) are rules or conditions that are
applied to the data within a database to ensure data integrity, consistency, and adherence to
business rules.
There are several types of constraints available in DBMS and they are:
Domain constraints
Entity Integrity constraints
Referential Integrity constraints
Key constraints
DOMAIN CONSTRAINTS
The domain refers to the allowed values (range of values) for a function. The domain
value of an attribute must be an atomic value .
Types of Domain Constraints
Not Null
The column value cannot be empty (i.e. cannot contain a null value)
Check
The CHECK constraint checks the condition that follows it, e.g. CHECK
(Age>21) ensures that each Age column value is greater than 21.
KEY CONSTRAINTS
Keys are the set of entities that are used to identify an entity within its entity set
uniquely. A primary key can only contain unique and not null values in the relational
database table.
14
QUERIES & OUTPUT
// UNIQUE CONSTRINT
alter table employee add unique(ename);
insert into employee values(107,24,'sara',85000,'Bangalore');
ERROR: duplicate key value violates unique constraint "employee_ename_key" DETAIL: Key
(ename)=(sara ) already exists. SQL state: 23505
//CHECK CONSTRAINT
alter table employee add check (age>25);
//REFERNTIAL INTEGRITY
insert into person values(22445566,102);
insert into person values(12332145,103);
insert into person values(42536415,104);
select * from person;
15
delete from person where aadhar=12332145;
create table persn(aadhar int primary key,empid int, foreign key(empid) references
employee(empid)on delete cascade on update cascade);
insert into persn values(22445566,102);
insert into persn values(12332145,103);
insert into persn values(42536415,104);
RESULT
Thus the types of constraints were studied and the commands were implemented in
database successfully.
16
Ex.No.:3 C)
IMPLEMENT THE DATABASE WITH VIEWS
Date:
AIM
To implement the concept of views in PostgreSQL.
VIEWS
Views allow the user to create a virtual table based on an SQL query referring to other tables
in the database. A view stores an SQL query that is executed whenever user refer to the view.
The view has primarily two purposes:
Simplify the complex SQL queries.
Provide restriction to users from accessing sensitive data.
CREATING VIEWS
Views can be created using CREATE VIEW statement. A View can be created from a single
table or multiple tables.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
DROPPING VIEWS
If the view is no longer needed it can be dropped using the DROP statement.
Syntax:
DROP VIEW view_name;
UPDATING VIEWS
The CREATE OR REPLACE VIEW statement is used to add or remove fields from a
view. There could be a situation to create a view or replace it if it already exists.
17
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1,column2,..
FROM table_name
WHERE condition;
OUTPUT
create table sdetails(regno int,name char(15),marks int,dept char(10));
18
create table pers(regno int,age int,address char(20));
RESULT
Thus the usage of views was implemented successfully.
19
Ex.No.:4 A)
IMPLEMENTATION OF SUB QUERIES
Date:
AIM
To implement various Sub queries in SQL.
SUB QUERY
In PostgreSQL, a subquery (also known as a nested query or inner query) is a query nested
inside another query. Subqueries are enclosed in parentheses and can be used in various parts
of a SQL statement, such as the SELECT, FROM, WHERE, and HAVING clauses.
The common types of subqueries in PostgreSQL are :
AS
An alias is created with the AS keyword.
SQL aliases are used to give a table, or a column in a table, a temporary name.
Syntax
SELECT column1, column2 FROM tablename AS aliasname;
Example
SELECT MAX(salary) AS maxsalary FROM employees
IN
The IN operator specify a list of possible values in the WHERE clause.
Syntax
WHERE IN (subquery)
Example
SELECT * FROM customers WHERE country IN ('Germany', 'France', 'UK');
ANY
The subquery must return exactly one column.
The ANY operator must be preceded by one of the following comparison
operator =, <=, >, <, > and <>.
The ANY operator is used to compare a value to any value in a set of values.
Syntax
expression operator ANY (subquery)
Example
SELECT * FROM products WHERE price > ANY (SELECT price FROM
discount_products);
ALL
The ALL operator is used to compare a value to all values in a set of values.
Syntax
expression operator ALL (subquery)
Example
SELECT * FROM products WHERE price > ALL (SELECT price FROM
expensive_products);
20
EXISTS
The EXISTS operator is used to check the existence of rows in a subquery result.
Syntax
pid price
1 200
2 100
3 300
4 50
pid price
1 200
3 300
SELECT * FROM product WHERE price < Any (SELECT price FROM discountprod);
pid price
2 100
4 50
21
SELECT * FROM product WHERE EXISTS (SELECT * FROM discountprod WHERE
product.pid = discountprod.pid);
pid price
1 200
2 100
4 50
SELECT * FROM product WHERE pid IN ( SELECT pid FROM discountprod WHERE
price = 80);
pid price
2 100
pid maxprice
1 300
2 300
3 300
4 300
max
300
RESULT
Thus the SQL sub query commands were implemented and the output was verified
successfully.
22
Ex.No.:4 B)
SELECT COMMANDS AND AGGREGATE FUNCTIONS IN SQL
Date:
AIM
To study and execute various select commands using SQL
TYPES OF SELECT COMMAND
GROUP BY
The SQL GROUP BY clause is used to arrange identical data into groups with the
help of some aggregate functions (COUNT, MAX, MIN, SUM, AVG) .
ORDERBY
The ORDER BY clause in SQL is used to sort the result set of a query based on one or more
columns. It can be used with both numerical and textual data, and sorted in ascending (asc)
or descending (desc) order.
HAVING
The HAVING clause in SQL is used in conjunction with the GROUP BY clause to filter the
results of a query based on aggregate conditions.
BETWEEN
The BETWEEN operator in SQL is used to filter the result set based on a specified range of
values. It is often used in the WHERE clause of a query.
SET OPERATORS
UNION
The UNION operator in SQL is used to combine the result sets of two or more
SELECT statements into a single result set.
It removes duplicate rows from the combined result set.
UNION ALL
The UNION ALL operator in SQL is similar to the UNION operator but includes
all rows in the result set, including duplicate rows.
INTERSEC
The INTERSECT operator in SQL is used to combine the result sets of two
SELECT statements and retrieve only the rows that are common to both result sets.
MINUS
The MINUS operator in SQL is used to combine the result sets of two SELECT
statements.
It performs set difference operation.
23
AGGREGATE FUNCTIONS
AVG
The AVG aggregate function in SQL is used to calculate the average value of a
numeric column in a result set.
COUNT
The COUNT aggregate function in SQL is used to count the number of rows in a
result set.
MAX
The MAX function is a useful aggregate function for obtaining the maximum value in
a set of data.
MIN
The MIN function is a useful aggregate function for obtaining the minimum value in
a set of data.
SUM
The SUM aggregate function in SQL is used to calculate the total sum of values in a
specified column.
OUTPUT
create table stud(rollno int,name char(20),dbms int,maths int,algorithm int,dept
char(10),address char(15));
insert into stud values(123,'priya',78,80,80,'cse','chennai');
insert into stud values(124,'uma',63,68,70,'cse','madurai');
insert into stud values(125,'ganesh',90,95,75,'ai','ooty');
insert into stud values(126,'varun',94,87,77,'ece','chennai');
dept
cse
ai
ece
24
select dept from stud;
dept
cse
cse
ai
ece
count dept
2 cse
1 ai
1 ece
select name from stud order by (name)desc;
name
varun
uma
priya
ganesh
name
priya
varun
count dept
2 cse
25
select * from stud union select * from person;
select name from stud where name in (select name from person where dept='ai');
name
ganesh
RESULT
Thus various select command and aggregate functions are executed in SQL
successfully.
26
Ex.No.:5 A)
PROGRAMS USING PROCEDURES
Date:
AIM
To Querying/Managing the database using SQL Programming -Procedures and
Functions
PL/pgSQL
PL/pgSQL is a procedural programming language for the PostgreSQL dbms system.
PL/pgSQL allows you to extend the functionality of the PostgreSQL dbms server by
creating server objects with complex logic.
PL/pgSQL was designed to :
Create user-defined functions, stored procedures, and triggers.
Extend standard SQL by adding control structures such as if, case,
and loop statements.
Inherit all user-defined functions, operators, and types.
PROCEDURES
PostgreSQL 11 introduced stored procedures that support transactions.
The following illustrates the basic syntax of the create procedure statement:
Output:
NOTICE: hai
DO
Query returned successfully in 37 msec.
27
PL/PGSQL TO SWAP TWO NUMBERS
DO $$
DECLARE
num1 int;
num2 int;
temp int;
BEGIN
num1 := 1000;
num2 := 2000;
RAISE NOTICE 'Before swapping';
RAISE NOTICE 'num1=%, num2=%', num1, num2;
temp := num1;
num1 := num2;
num2 := temp;
RAISE NOTICE 'After swapping';
RAISE NOTICE 'num1=%, num2=%', num1, num2;
END $$;
Output:
NOTICE: Before swapping
NOTICE: num1=1000, num2=2000
NOTICE: After swapping
NOTICE: num1=2000, num2=1000
DO
Call proc();
OUTPUT:
NOTICE: Hello from my stored procedure! CALL Query returned
successfully in 41 msec.
28
PROGRAM USING PROCEDURES TO PERFORM TRANSACTION
create table accounts ( id int, name varchar(100) not null, balance int not null, primary
key(id));
insert into accounts values(1,'Raju', 5000);
insert into accounts values(2,'Nikhil', 10000);
id name balance
1 Raju 5000
2 Nikhil 10000
id name balance
1 Raju 4000
2 Nikhil 11000
RESULT:
Thus the concept of procedures and functions are executed successfully.
29
Ex.No.:5 B)
PROGRAMS USING FUNCTIONS
Date:
AIM
To execute program using functions in postgreSQL.
FUNCTIONS
PostgreSQL uses the CREATE FUNCTION statement to develop user-defined functions.
Syntax:
CREATE [OR REPLACE] FUNCTION function_name (arguments)
RETURNS return_datatype
LANGUAGE plpgsql
AS $variable_name$
DECLARE
declaration;
[...] -- variable declaration
BEGIN
< function_body >
[...] -- logic
RETURN { variable_name | value }
END;
$$
Example:
select inc(20);
OUTPUT:
inc
21
30
insert into car values(2,70000);
select * from car;
OUTPUT:
OUTPUT:
31
PL/PGSQL FUNCTION TO CALCULATE THE FACTORIAL OF A NUMBER:
OUTPUT:
OUTPUT:
32
PL/PGSQL FUNCTION TO ACCEPT A NUMBER A PRINT THE SUM OF ITS DIGIT
RETURN sum;
END;
$$ LANGUAGE plpgsql;
// call the function
SELECT sum_of_digits(12345);
OUTPUT:
RETURN rev;
END;
$$ LANGUAGE plpgsql;
SELECT reverse_number(123);
OUTPUT:
RESULT
Thus the usage of functions was studied and the programs were executed successfully.
33
Ex.No.:6
CONSTRAINTS AND SECURITY USING TRIGGERS
Date:
AIM
To querying the database using SQL Programming – Constraints and security using
Trigger.
TRIGGERS
A trigger is a stored procedure in a database that automatically invokes whenever a special
event in the database occurs. For example, a trigger can be invoked when a row is inserted
into a specified table or when specific table columns are updated.
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Explanation of Syntax
1. Create trigger [trigger_name]: Creates or replaces an existing trigger with the
trigger_name.
2. [before | after]: This specifies when the trigger will be executed.
3. {insert | update | delete}: This specifies the DML operation.
4. On [table_name]: This specifies the name of the table associated with the trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger will be executed for
each affected row.
6. [trigger_body]: This provides the operation to be performed as the trigger is fired
PROGRAM
34
SELECT * FROM employee_audit;
RESULT
Thus the trigger was invoked and executed successfully.
35
Ex.No.:7
DATABASE DESIGN USING NORMALIZATION
Date:
AIM:
To create a database with Normal forms.
NORMALZATION
Normalization is a database design technique which organizes tables in a
manner that reduces redundancy and dependency of data.
It divides larger tables to smaller tables and links them using relationships.
The inventor of the relational model Edgar Codd proposed the theory of normalization
with the introduction of First Normal Form, and he continued to extend theory with
Second and Third Normal Form. Later he joined with Raymond F. Boyce to develop
the theory of Boyce-Codd Normal Form.
Theory of Data Normalization in SQL is still being developed further. For example,
there are discussions even on 6thNormal Form. However, in most practical
applications, normalization achieves its best in 3rd Normal Form. The evolution of
Normalization theories is illustrated below-
Assume a video library maintains a database of movies rented out. Without any
normalization, all information is stored in one table as shown below.
Table 1
36
1NF Example
What is a KEY?
A KEY is a value used to identify a record in a table uniquely. A KEY could be a
single column orcombination of multiple columns
Note: Columns in a table that are NOT used to identify a record uniquely are called
37
Let's move into second normal form 2NF
It is clear that we can't move forward to make our simple database in 2nd
Normalization form unlesswe partition the table above.
Table 1
Table 2
We have divided our 1NF table into two tables viz. Table 1 and Table2. Table 1
contains member information. Table 2 contains information on movies rented.
38
Why do we need a foreign key?
Suppose an idiot inserts a record in Table B such as
We will only be able to insert values into your foreign key that exist in the unique
key in the
parenttable. This helps in referential integrity.
Now, if somebody tries to insert a value in the membership id field that does not
exist in the parenttable, an error will be shown!
39
Let's move into 3NF
3NF (Third Normal Form) Rules
Rule 1- Be in 2NF
Rule 2- Has no transitive functional dependencies
To move our 2NF table into 3NF, we again need to again divide our table.
3NF Example
Table 1
Table 2
Table 3
We have again divided our tables and created a new table which stores Salutations.
There are no transitive functional dependencies, and hence our table is in 3NF
Now our little example is at a level that cannot further be decomposed to attain higher
forms of normalization. In fact, it is already in higher normalization forms. Separate
efforts for moving into next levels of normalizing data are normally needed in
40
complex databases. However, we will be discussing next levels of normalizations in
brief in the following.
If no database table instance contains two or more, independent and multivalued data
describing therelevant entity, then it is in 4th Normal Form.
RESULT
Thus the normalization concept was studied and the database was created using
normalization.
41
Ex.No.:8 DEVELOPING A DATABASE APPLICATIONS USING
Date: VISUALSTUDIO
AIM
To develop a database application for students using VisualStudio
PROCEDURE
1. In the start menu go to all apps and select Microsoft office->Ms Access
2. Select new document .create a table with name mydata . Choose design view and
create three fields name, address and age.
3. Enter the data for the created fields by double clicking the table mydata. Save the
document as mydata and close the document.
4. In the start menu go to Microsoft Visual studio 2022.click create a new project->WPF
framework(.Netframework)->next->create a new project.
5. Select and pin toolbox and datasource at the leftside of the window.From the toolbox
select 3 labels and 3 textboxes, place it in the form and change the label name from
its properties.
42
9. Choose the database objects and click finish.
10. Place the datagridview in the form and from choose data source->mydata
11. Select each textbox and from its properties choose data bindings->tag->select
appropriate field.
12. Write the code for each buttons.
13. Run the project by pressing CTRL+F5.
43
OUTPUT
RESULT
Thus the application to develop student Management System using VisualStudio is
successfully implemented.
44
Ex.No.:9 DATABASE DESIGN USING EER-TO-ODB MAPPING / UML CLASS
Date: DIAGRAMS
AIM
To design a database using EER-to-ODB mapping and create UML class diagrams for
the object-oriented database model.
ALGORITHM
1. Develop EER or UML diagrams representing system entities, attributes, and
relationships.
2. Extract entities and attributes for tables, considering each entity as a table and
attributes as columns.
3. Translate relationships into foreign keys in corresponding tables, considering
cardinalities and participation constraints.
4. Decide on a mapping strategy for inheritance (table-per-class or table-per-hierarchy)
for generalization/specialization.
5. Map aggregations to foreign keys or separate tables.
6. Assign appropriate data types to attributes based on requirements and the DBMS.
7. Apply normalization to eliminate redundancy and anomalies.
8. Establish primary keys, foreign keys, unique constraints, and other integrity
constraints.
9. Create a SQL script to implement the database schema, relationships, and constraints.
10. Execute the SQL script to create the database, test and optimize the schema for
performance and functionality, and document the structure
45
4. Student Attributes:
Class
5. AccountAttributes:
no_borrowed_books no_reserved_books no_returned_books
no_lost_books fine_amountOperations: Calculate_fine()
6. Book Attributes:
Title
Author
ISBN
publication Operations: Show_duedt()
Reservation_status()
Feedback()
Book_request()
Renew_info()
7. librarianAttributes:
Name
Id
Password
SearchString Operations: Verify_librarian()
Search()
8. Library database
Attributes:
List_of_books Operations:
Add()
Delete()
Update()
Display()
Search()
46
UML DIAGRAM
RESULT
Thus the implementation on database design using eer-to-odb mapping / uml class
diagrams is performed successfully.
47
Ex.No.:10 A)
PROGRAM USING UDTs AND INHERITANCE
Date:
AIM:
To implement the object features of SQL using UDTs and Inheritance.
ALGORITHM:
1. Create a type called as addresstype to hold street and city.
2. Create a table employ consisting of emp_id,name,salary and address.
3. Create a table manager which inherits employ table and insert values into this table.
4. Create a table developer which inherits employ and insert values into this table.
5. Display the above three tables, now the base table is inherited by the child tables and the
result will be shown.
PROGRAM
48
select * from developer;
RESULT
Thus the object features of SQL like UDT and inheritance was implemented
successfully.
49
Ex.No.:10 B)
PROGRAM USING UDTs AND SUBTYPES
Date:
AIM
To implement the object features of SQL like UDTs and Subtypes.
SUBTYPE IN POSTGRESQL
In PostgreSQL, subtypes are implemented using the CREATE TYPE statement. Subtypes
allow to create a new data type that is based on an existing data type and inherits its
properties.
ALGORITHM
RESULT
Thus the subtype concept under object features of SQL was implemented
successfully.
50
Ex.No.:10 C)
OBJECT FEATURES OF SQL-METHOD DEFINITION
Date:
AIM
To calculate the length of books title using method definition.
ALGORITHM
1. Create a table named books with columns book_id, title, author, and publication_year.
2. Define a method named calculate_title_length that takes a book title as an argument
and returns the length of the title using the LENGTH function.
3. Insert some sample data into the books table.
4. Use the calculate_title_length method in a query to find the title length for each book.
PROGRAM
-- Create a table
CREATE TABLE books (book_id SERIAL PRIMARY KEY, title VARCHAR, author
VARCHAR, publication_year INT);
-- Create a method to calculate the number of characters in the title of a book
CREATE OR REPLACE FUNCTION calculate_title_length(book_title VARCHAR)
RETURNS INT AS $$
BEGIN
RETURN LENGTH(book_title);
END;
$$ LANGUAGE plpgsql;
-- Use the method to calculate the title length for a specific book
RESULT
Thus the length of given books title was calculated using method definition and
displayed successfully.
51
Ex.No.:11 QUERYING THE OBJECT-RELATIONAL DATABASE USING
Date: OBJET QUERY LANGUAGE
AIM
To query the object relational database using object query language.
OBJECT-RELATIONAL DATABASE
Object-relational databases aim to bridge the gap between relational databases and
object-oriented databases, incorporating concepts from both paradigms.
They allow the users to define custom data types, store complex data structures, and
use functions to manipulate the data, providing more flexibility than traditional
relational databases.
Oracle Database: Oracle supports object-relational features and has a long history of
providing support for complex data types.
IBM Db2: Db2 is known for its support for complex data types and object-relational
features.
ALGORITHM & QUERIES
1. Table Creation:
The Authors table is created with columns AuthorID (primary key),
AuthorName (name of the author), and Books (an array of book titles).
CREATE TABLE Authors ( AuthorID SERIAL PRIMARY KEY, AuthorName
VARCHAR(100), Books VARCHAR(200)[ ]);
2. Data Insertion:
Sample data is inserted into the Authors table. Authors are associated with an
array of books they have written.
INSERT INTO Authors (AuthorName, Books) VALUES
('Jenith joel', ARRAY['Book1', 'Book2']),
('Reena dev', ARRAY['Book3', 'Book4', 'Book5']),
('Alice mary', ARRAY['Book6']);
52
SELECT a.AuthorID, a.AuthorName, CARDINALITY(Books) AS NumberofBooks
FROM Authors a;
RESULT
Thus the usage of arrays, creation of user-defined types and the inclusion of functions
which are features of object-relational databases are implemented and executed.
53
EX.No. 1 IMPLEMENT BASIC SEARCH STRATEGIES 8-PUZZLE PROBLEM
DATE:
AIM
ALGORITHM
1. The code starts by creating a Solution class and then defining the method solve.
2. The function takes in a board as an argument, which is a list of tuples representing the
positions on the board.
3. It iterates through each position in the list and creates a dictionary with that position's value
set to 0.
4. Then it iterates through all possible moves for that position and returns their number of
occurrences in dict.
5. After this, it loops over all nodes on the board until it finds one where there are no more
moves left to make (i.e., when len(current_nodes) == 0).
6. This node will be returned as -1 if found or else its index will be stored into pos_0 so that we
can find out what move was made at that point later on.
7. The next step is finding out what move was made at every node by looping over all possible
moves for each node using self's find_next function, which takes in a single node as an
argument and returns any other nodes connected to it via path-finding algorithms like DFS or
BFS (see below).
8. For example, if pos_0 = 1 then self would call: moves = { 0: [1], 1:
9. The code will print the number of paths in a solution.
PROGRAM
class Solution:
def solve(self, board):
state_dict = {} # Better name than 'dict' (which shadows the built-in dict type)
flatten = []
# Flatten the board into a 1D list and convert to tuple for immutability
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
while True:
# Get all the nodes at the current depth level (cnt)
current_nodes = [x for x in state_dict if state_dict[x] == cnt]
results = []
pos_0 = node.index(0) # Find the position of the blank (0)
# Generate possible next moves by swapping the blank with its valid neighbors
for move in moves[pos_0]:
new_node = list(node)
new_node[move], new_node[pos_0] = new_node[pos_0], new_node[move]
results.append(tuple(new_node)) # Convert back to tuple for immutability
return results
# Example usage:
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]
]
OUTPUT:
NO OF MOVES== 4
RESULT:
Thus the program to implement 8 puzzles search strategy is implemented and executed successfully
EX.No. 1.b Implement basic search strategies – 8-Queens Problem
DATE:
AIM
ALGORITHM
PROGRAM
def print_board(board):
"""Helper function to print the board."""
for row in board:
print(" ".join("Q" if col == 1 else "." for col in row))
print()
# Check diagonals
for k in range(N):
for l in range(N):
if (k + l == row + col) or (k - l == row - col):
if board[k][l] == 1:
return True
return False
# Backtrack
board[i][j] = 0
return False
if N_queens(board, N, N):
print_board(board)
else:
print("No solution exists.")
RESULT
Thus the program to implement 8 queens search strategy is implemented and executed successfully.
EX.No.3 Implement basic search strategies – Crypt arithmetic
DATE:
AIM
To implement basic search strategies – Crypt arithmetic.
ALGORITHM
# If there are more than 10 unique letters, it's not possible to assign digits
if len(letters) > 10:
print('0 Solutions!')
return
solutions = []
_solve(word1, word2, result, letters, {}, solutions)
if solutions:
print('\nSolutions:')
for soln in solutions:
print(f'{soln[0]}\t{soln[1]}')
else:
print('0 Solutions!')
if __name__ == '__main__':
print('CRYPTARITHMETIC PUZZLE SOLVER')
print('WORD1 + WORD2 = RESULT')
word1 = input('Enter WORD1: ').upper()
word2 = input('Enter WORD2: ').upper()
result = input('Enter RESULT: ').upper()
RESULT
Thus the program to implement crypt arithmetic search strategy is implemented and executed successfully
EX.No. 2 Implement A* Algorithm
DATE:
AIM
To Implement A* Algorithm.
ALGORITHM
PROGRAM
# Base Class
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[:] # Copy the parent's path
self.path.append(value) # Add the current value to the path
else:
self.path = [value]
self.start = start
self.goal = goal
def GetDistance(self):
pass
def CreateChildren(self):
pass
def GetDistance(self):
if self.value == self.goal:
return 0
dist = 0
for i in range(len(self.goal)):
letter = self.goal[i]
if letter in self.value:
dist += abs(i - self.value.index(letter))
else:
dist += len(self.goal) # If letter is not found, add maximum possible distance
return dist
def CreateChildren(self):
if not self.children:
for i in range(len(self.goal) - 1):
val = self.value
# Swap adjacent letters
val = val[:i] + val[i+1] + val[i] + val[i+2:]
child = State_String(val, self) # Create new child state
self.children.append(child)
# A* Solver Class
class A_Star_Solver:
def __init (self, start, goal):
self.path = []
self.visitedQueue = []
self.priorityQueue = PriorityQueue()
self.start = start
self.goal = goal
def Solve(self):
startState = State_String(self.start, None, self.start, self.goal)
count = 0
self.priorityQueue.put((0, count, startState)) # Start state with priority 0
if not self.path:
print("Goal is not possible: " + self.goal)
else:
print("Goal found! Path to solution:")
for i in range(len(self.path)):
print("{0}) {1}".format(i, self.path[i]))
Starting....
0)secure
1)secrue
2)sercue
3)srecue
4)rsecue
5)rescue
RESULT
AIM
ALGORITHM
# Returns optimal value for the current player (Initially called for root and maximizing player)
def minimax(depth, nodeIndex, maximizingPlayer, values, alpha, beta):
# Terminating condition. i.e, leaf node is reached
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
# Recur for left and right children (Maximizing player)
for i in range(2):
val = minimax(depth + 1, nodeIndex * 2 + i, False, values, alpha, beta)
best = max(best, val)
alpha = max(alpha, best)
# Alpha-Beta Pruning
if beta <= alpha:
break
return best
else:
best = MAX
# Recur for left and right children (Minimizing player)
for i in range(2):
val = minimax(depth + 1, nodeIndex * 2 + i, True, values, alpha, beta)
best = min(best, val)
beta = min(beta, best)
# Alpha-Beta Pruning
if beta <= alpha:
break
return best
# Driver Code
if name__ == " main ":
values = [3, 5, 6, 9, 1, 2, 0, -1] # Leaf node values
print("The optimal value is:", minimax(0, 0, True, values, MIN, MAX))
OUTPUT
The optimal value is : 5
RESULT
Thus the program to implement Minimax algorithm for game playing is implemented and executed
successfully
EX.No.4 Solve constraint satisfaction problems
DATE:
AIM
ALGORITHM
def backtrack(assignment):
# Check if assignment is complete
if len(assignment) == len(VARIABLES):
return assignment
def select_unassigned_variable(assignment):
# Select the next unassigned variable (can be optimized with MRV)
for var in VARIABLES:
if var not in assignment:
return var
return None
print("Solution:")
print(solution)
OUTPUT
{'csc': 'Monday', 'maths': 'Tuesday', 'phy': 'Tuesday', 'che': 'Monday', 'tam': 'MoMonday', 'eng': 'Wednesday',
'bio': 'Tuesday'}
RESULT
Thus the program to solve constraint satisfaction problem is implemented and executed successfully.
EX.No. 5 Propositional Model Checking Algorithms
DATE:
AIM
ALGORITHM
1. Class Literal, it has attributes name and sign to denote whether the literal is positive or negative in use.
2. The neg function returns a new literal with the same name but the opposite sign of its parent literal.
3. The repr function returns the string of the literal name,( or the string with a negative sign) each time the
instance of the literal is called.
4. The CNFConvert function converts the KiB from a list of sets to a list of list for easier computing
5. The VariableSet function finds all the used literals in the KB, and in order to assist with running the DPLL.
6. The Negativeofx function is for holding the negative form of the literal, for use in the DPLL algorithm
7. The pickX function picks a literal from the variable set and works with it as a node in the tree.
8. Now define the functions splitfalseliterals() and splitTrueLiteral().
9. Create the function dpll() that performs the dpll algorithm recursively.
10. Finally call the function to execute the code.
PROGRAM
import re
class Literal:
def __init (self, name, sign=True):
self.name = str(name)
self.sign = sign
def CNFconvert(KB):
# Converts the KB from a list of sets to a list of lists for easier computing
storage = []
for i in KB:
i = list(i)
i = [str(j) if isinstance(j, str) else j for j in i] # Ensure literals are properly handled
storage.append(i)
return storage
def VariableSet(KB):
# Finds all the used literals in the KB
KB = CNFconvert(KB)
storage = []
for obj in KB:
for item in obj:
if item[0] == '-' and item[1:] not in storage:
storage.append(str(item[1:]))
elif item not in storage and item[0] != '-':
storage.append(str(item))
return storage
def Negativeofx(x):
# Holds the negative form of the literal
return str(x[1:]) if x[0] == '-' else '-' + str(x)
def unitResolution(clauses):
literalholder = {}
i=0
while i < len(clauses):
newClauses = []
clause = clauses[i]
if len(clause) == 1:
literal = str(clause[0])
pattern = re.match("-", literal)
if pattern:
nx = literal[1:]
literalholder[nx] = False
else:
nx = "-" + literal
literalholder[literal] = True
for item in clauses:
if item != clauses[i]:
if nx in item:
item.remove(nx)
newClauses.append(item)
i=0
clauses = newClauses
return literalholder, clauses
def DPLL(KB):
KB = CNFconvert(KB)
varList = VariableSet(KB)
result = dpll(KB, varList)
if result == 'notsatisfiable':
return [False, {}]
else:
for i in varList:
if i in result and result[i] == True:
result[i] = 'true'
elif i in result and result[i] == False:
result[i] = 'false'
else:
result[i] = 'free'
return [True, result]
# Example usage
A = Literal('A')
B = Literal('B')
C = Literal('C')
D = Literal('D')
KB = [{A, B}, {A, -C}, {-A, B, D}]
print(DPLL(KB))
OUTPUT
RESULT
Thus the program to implement Propositional Model checking Algorithm is implemented and executed
successfully.
EX.No. 6 Implement Forward Chaining Algorithm
DATE:
AIM
ALGORITHM
PROGRAM
def main():
print("*-----Forward--Chaining ---- *", end='')
display()
x = int(input())
print(" \n", end='')
if x == 1 or x == 2:
print(" Chance Of Frog ", end='')
elif x == 3 or x == 4:
print(" Chance of Canary ", end='')
else:
print("\n-------In Valid Option Select ---------", end='')
if x >= 1 and x <= 4:
print("\n X is ", end='')
print(database[x-1], end='')
print("\n Color Is 1.Green 2.Yellow", end='')
print("\n Select Option ", end='')
k = int(input())
if k == 1 and (x == 1 or x == 2): # frog0 and green1
print(" yes it is ", end='')
print(knowbase[0], end='')
print(" And Color Is ", end='')
print(knowbase[2], end='')
elif k == 2 and (x == 3 or x == 4): # canary1 and yellow3
print(" yes it is ", end='')
print(knowbase[1], end='')
print(" And Color Is ", end='')
print(knowbase[3], end='')
else:
print("\n---InValid Knowledge Database", end='')
OUTPUT
*-----Forward--Chaining ---- *
X is
1.Croaks
2.Eat Flies
3.shrimps
4.Sings
Select One 1
Chance Of Frog
X is Croaks
Color Is
1.Green
2.Yellow
Select Option 1
yes it is Frog And Color Is Green
RESULT
Thus the program to implement Forward Chaining Algorithm is implemented and executed successful
EX.No. 7 Implement backward Chaining Algorithm
DATE:
AIM
ALGORITHM
1. The code starts with a function called display () which prints out the text "X is 1.frog 2.canary"
2. And then asks for input from the user, asking them to select one of two options: Chance of eating flies or
Chance of shrimping.
3. If they choose either option, it will print out what that option would be in green or yellow color respectively.
4. The next line is x = int (input ()) which takes the value entered by the user and assigns it to variable x.
5. The if statement checks whether x equals 1 or 2
6. So if they enter 1 as their answer, it will print out "Chance of eating flies"
7. Otherwise it will print "Chance of shrimping".
PROGRAM
def display():
"""Displays options for the user to choose from."""
print("\nX is:")
print("1. Croaks")
print("2. Eat Flies")
print("3. Shrimps")
print("4. Sings")
print("\nSelect One (1-4):", end=' ')
def main():
"""Main function to drive the forward chaining logic."""
print("*-----Forward-Chaining---- *")
display()
try:
x = int(input()) # User selects an option from the database
except ValueError:
print("\n---Invalid Input. Please enter a number (1-4 for X and 1-2 for Color).---")
*-----Backward--Chaining ---- *
X is
1.frog
2.canary
Select One 1
Chance Of eating flies
X is Frog
1.green
2.yellow
1
yes it is in Green colour and will Croak
RESULT
Thus the program to implement backward chaining algorithm is implemented and executed successfully.
EX.No. 8 Implement Naïve Bayes Models
DATE:
AIM
ALGORITHM
PROGRAM
# Split the dataset into training and testing sets (1/3 for testing)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33)
OUTPUT
Predicted Values:
[1 0 2 2 2 0 1 0 1 1 2 2 0 2 2 1 1 2 1 0 2 2 0 2 1 0 1 2 1 2 2 0 0 2 1 0 1
2 1 2 2 1 2 2 1 1 2 2 0 1]
Actual Values:
[1 0 2 2 2 0 1 0 1 1 2 2 0 2 2 1 1 2 1 0 2 2 0 2 2 0 1 1 1 1 2 0 0 2 1 0 1
2 1 2 2 1 2 2 2 1 2 2 0 1]
Confusion Matrix:
[[11 0 0]
[ 0 15 2]
[ 0 2 20]]
RESULT
Thus the program to implement Naïve Bayes Model is implemented and executed successf
EX.No. 9 Implement Bayesian Networks and perform inferences
DATE:
AIM:
To Implement Bayesian Networks and perform inferences.
ALGORITHM:
36
19. Next, it calculates the cross entropy loss between target and predicted values.
20. Then, it calculates the cost function which is then minimized using Adam optimizer.
21. Finally, it prints out the predicted value and total cost after every iteration of optimization process.
22. The code starts by defining a function called draw_graph that takes in a predicted value.
23. The code then creates two subplots on the same figure, one for each of the predictions.
24. The first subplot is created with fig_1 and has an index of 1, which means it's the second plot in this figure.
25. This plot will have two axes: x-axis and y-axis.
26. The x-axis represents time, while the y-axis represents accuracy percentage (0% to 100%).
27. The second subplot is created with fig_2 and has an index of 2, which means it's the third plot in this figure.
28. This plot will also have two axes: x-axis and y-axis but they represent different values than those on fig_1 .
29. The code is a function that takes in an input of predicted and returns the accuracy, cross entropy, and KL
values.
30. The first line of code calculates the size of the tensor using target_tensor.size(0) which will be equal to 1
because it is a one-dimensional tensor.
31. Next, we have the function draw_graph which draws a graph with two subplots; one for accuracy and one for
cross entropy.
32. The last line prints out some statistics on how accurate this prediction was.
PROGRAM
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchbnn as bnn
import matplotlib.pyplot as plt
from sklearn import datasets
# Convert to tensors
data_tensor = torch.from_numpy(data).float()
target_tensor = torch.from_numpy(target).long()
# Loss functions
cross_entropy_loss = nn.CrossEntropyLoss()
klloss = bnn.BKLLoss(reduction='mean', last_layer_only=False)
# Hyperparameters
klweight = 0.01
optimizer = optim.Adam(model.parameters(), lr=0.01)
# Forward pass
models = model(data_tensor)
38
# Total loss
total_cost = cross_entropy + klweight * kl
# Zero the gradients, perform the backward pass, and update the weights
optimizer.zero_grad()
total_cost.backward()
optimizer.step()
# Calculate accuracy
final = target_tensor.size(0)
correct = (predicted == target_tensor).sum().item()
accuracy = 100 * float(correct) / final
# Real data
fig_1 = fig.add_subplot(1, 2, 1)
z1_plot = fig_1.scatter(data[:, 0], data[:, 1], c=target, marker='v')
plt.colorbar(z1_plot, ax=fig_1)
fig_1.set_title("REAL")
# Predicted data
fig_2 = fig.add_subplot(1, 2, 2)
z2_plot = fig_2.scatter(data[:, 0], data[:, 1], c=predicted, marker='v')
plt.colorbar(z2_plot, ax=fig_2)
fig_2.set_title("PREDICT")
39
plt.show()
OUTPUT:
- Accuracy: 33.33%
- CE : 2.31, KL : 2.91
- Accuracy: 95.33%
- CE : 0.14, KL : 3.19
- Accuracy: 95.33%
- CE : 0.10, KL : 3.74
- Accuracy: 98.00%
- CE : 0.06, KL : 3.79
- Accuracy: 94.00%
- CE : 0.16, KL : 3.86
- Accuracy: 98.00%
- CE : 0.06, KL : 3.76
- Accuracy: 98.00%
- CE : 0.06, KL : 3.62
- Accuracy: 98.67%
- CE : 0.05, KL : 3.47
- Accuracy: 98.67%
- CE : 0.06, KL : 3.34
- Accuracy: 98.67%
- CE : 0.06, KL : 3.24
40
RESULT
Thus, the program to implement Bayesian Networks and perform inferences is implemented and executed
successfully.
41
VARUVN VADIVELAN INSTITUTE OF TECHNOLOGY
DHARMAPURI – 636701
AD3301
1. Install the data Analysis and Visualization tool: R/ Python /Tableau Public/ Power BI.
2. Perform exploratory data analysis (EDA) on with datasets like email data set. Export all your emails as a
dataset, import them inside a pandas data frame, visualize them and get different insights from the data.
3. Working with Numpy arrays, Pandas data frames , Basic plots using Matplotlib.
4. Explore various variable and row filters in R for cleaning data. Apply various plot features in R on sample
data sets and visualize.
5. Perform Time Series Analysis and apply the various visualization techniques.
6. Perform Data Analysis and representation on a Map using various Map data sets with Mouse Rollover
effect, user interaction, etc..
7. Build cartographic visualization for multiple datasets involving various countries of the world;
states and districts in India etc.
8. Perform EDA on Wine Quality Data Set.
9. Use a case study on a data set and apply the various EDA and visualization techniques and present an
analysis report.
LIST OF EXPERIMENTS
S.NO EXPERIMENS PAGE NO MARKS SIGNATURE
9
EX NO: 1
DATE: INSTALLING DATA ANALYSIS AND VISUALIZATION TOOL
AIM:
To write a steps to install data Analysis and Visualization tool: R/ Python /Tableau Public/ Power BI.
PROCEDURE:
R:
R is a programming language and software environment specifically designed for statistical
computing and graphics.
Windows:
Download R from the official website: https://cran.r-project.org/mirrors.html
Run the installer and follow the installation instructions.
macOS:
Download R for macOS from the official website: https://cran.r-project.org/mirrors.html
Open the downloaded file and follow the installation instructions.
Linux:
You can typically install R using your distribution's package manager. For example, on Ubuntu, you
can use the following command:
csharp
Copy code
sudo apt-get install r-base
Python:
Python is a versatile programming language widely used for data analysis. You can install Python
and data analysis libraries using a package manager like conda or pip.
Windows:
Download Python from the official website: https://www.python.org/downloads/windows/
Run the installer, and make sure to check the "Add Python to PATH" option during installation.
You can install data analysis libraries like NumPy, pandas, and matplotlib using pip.
macOS:
macOS typically comes with Python pre-installed. You can install additional packages using pip or
set up a virtual environment using Ana
conda.
Linux:
Python is often pre-installed on Linux. Use your distribution's package manager to install Python if
it's not already installed. You can also use conda or pip to manage Python packages.
Tableau Public:
Tableau Public is a free version of Tableau for creating and sharing interactive data visualizations.
Go to the Tableau Public website: https://public.tableau.com/s/gallery
Download and install Tableau Public by following the instructions on the website.
Power BI:
Power BI is a business analytics service by Microsoft for creating interactive reports and dashboards.
Go to the Power BI website: https://powerbi.microsoft.com/en-us/downloads/
Download and install Power BI Desktop, which is the tool for creating reports and dashboards.
Please note that the installation steps may change over time, so it's a good idea to check the official
websites for the most up-to-date instructions and download links. Additionally, system requirements
may vary, so make sure your computer meets the necessary specifications for these tools.
Ex no: 2
Date: Exploratory Data Analysis (EDA) on with Datasets
Aim:
To Perform exploratory data analysis (EDA) on with datasets like email data set.
Procedure:
Exploratory Data Analysis (EDA) on email datasets involves importing the data, cleaning it, visualizing
it, and extracting insights. Here's a step-by-step guide on how to perform EDA on an email dataset using
Python and Pandas
1. Import Necessary Libraries:
Import the required Python libraries for data analysis and visualization.
2. Load Email Data:
Assuming you have a folder containing email files (e.g., .eml files), you can use the email library to
parse and extract the email contents.
3. Data Cleaning:
Depending on your dataset, you may need to clean and preprocess the data. Common
cleaning steps include handling missing values, converting dates to datetime format, and removing
duplicates.
4. Data Exploration:
Now, you can start exploring the dataset using various techniques. Here are some common EDA
tasks:
Basic Statistics:
Get summary statistics of the dataset.
Distribution of Dates:
Visualize the distribution of email dates.
5. Word Cloud for Subject or Message:
Create a word cloud to visualize common words in email subjects or messages.
6. Top Senders and Recipients:
Find the top email senders and recipients.
Depending on your dataset, you can explore further, analyze sentiment, perform network analysis, or
any other relevant analysis to gain insights from your email data.
Program:
# Import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load the dataset
df = pd.read_csv('D:\ARCHANA\dxv\LAB\DXV\Emaildataset.csv')
# Display basic information about the dataset
print(df.info())
# Display the first few rows of the dataset
print(df.head())
# Descriptive statistics
print(df.describe())
# Check for missing values
print(df.isnull().sum())
# Visualize the distribution of numerical variables
sns.pairplot(df)
plt.show()
# Visualize the distribution of categorical variables
sns.countplot(x='label', data=df)
plt.show()
# Correlation matrix for numerical variables
correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.show()
# Word cloud for text data (if you have a column with text data)
from wordcloud import WordCloud
text_data = ' '.join(df['text_column'])
wordcloud = WordCloud(width=800, height=400, random_state=21,
max_font_size=110).generate(text_data)
plt.figure(figsize=(10, 7))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis('off')
plt.show()
OUT PUT:
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Unnamed: 0 5171 non-null int64
1 label 5171 non-null object
2 text 5171 non-null object
3 label_num 5171 non-null int64
dtypes: int64(2), object(2)
memory usage: 161.7+ KB
None
Unnamed: 0 label text label_num
0 605 ham Subject: enron methanol ; meter # : 988291\r\n... 0
1 2349 ham Subject: hpl nom for january 9 , 2001\r\n( see... 0
2 3624 ham Subject: neon retreat\r\nho ho ho , we ' re ar... 0
3 4685 spam Subject: photoshop , windows , office . cheap ... 1
4 2030 ham Subject: re : indian springs\r\nthis deal is t... 0
Unnamed: 0 label_num
count 5171.000000 5171.000000
mean 2585.000000 0.289886
std 1492.883452 0.453753
min 0.000000 0.000000
25% 1292.500000 0.000000
50% 2585.000000 0.000000
75% 3877.500000 1.000000
max 5170.000000 1.000000
Unnamed: 0 0
label 0
text 0
label_num 0
dtype: int64
Result:
The above Performing exploratory data analysis (EDA) on with datasets like email data set has been
performed successfully.
Ex no: 03
Date: Working with Numpy arrays, Pandas data frames , Basic plots using Matplotlib
Aim:
Write the steps for Working with Numpy arrays, Pandas data frames , Basic plots using Matplotlib
Procedure:
1. NumPy:
NumPy is a fundamental library for numerical computing in Python. It provides support for multi-
dimensional arrays and various mathematical functions. To get started, you'll first need to install NumPy if
you haven't already (you can use pip):
df = pd.DataFrame(data)
# Display the entire DataFrame
print("DataFrame:")
print(df)
# Accessing specific columns
print("\nAccessing 'Name' column:")
print(df['Name'])
# Adding a new column
df['Salary'] = [50000, 60000, 75000, 48000, 55000]
# Filtering data
print("\nPeople older than 30:")
print(df[df['Age'] > 30])
# Sorting by a column
print("\nSorting by 'Age' in descending order:")
print(df.sort_values(by='Age', ascending=False))
# Aggregating data
print("\nAverage age:")
print(df['Age'].mean())
# Grouping and aggregation
grouped_data = df.groupby('City')['Salary'].mean()
print("\nAverage salary by city:")
print(grouped_data)
# Applying a function to a column
df['Age_Squared'] = df['Age'].apply(lambda x: x ** 2)
# Removing a column
df = df.drop(columns=['Age_Squared'])
# Saving the DataFrame to a CSV file
df.to_csv('output.csv', index=False)
# Reading a CSV file into a DataFrame
new_df = pd.read_csv('output.csv')
print("\nDataFrame from CSV file:")
print(new_df)
OUTPUT:
3. Matplotlib:
Matplotlib is a popular library for creating static, animated, or interactive plots and graphs.
Install Matplotlib using pip:
pip install matplotlib
Here's a simple example of creating a basic plot:
import matplotlib.pyplot as plt
# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create a line plot
plt.figure(figsize=(8, 6))
plt.plot(x, y, label='Sine Wave')
plt.title('Sine Wave Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT:
RESULT:
Thus the above working with numpy, pandas, matplotlib has been completed successfully.
Ex no:4
Date: Exploring various variable and row filters in R for cleaning data
Aim:
Exploring various variable and row filters in R for cleaning data.
PROCEDURE:
Data Preparation and Cleaning
First, let's create a sample dataset and then explore various variable and row filters to clean the data
Variable Filters
1. Filtering by a Specific Value:
To filter rows based on a specific value in a variable (e.g., only show rows where Age is greater than
30):
filtered_data <- data[data$Age > 30, ]
RESULT:
Thus the above Exploring various variable and row filters in R for cleaning data.
EXNO: 5 PERFORM EDA ON WINE QUALITY DATA SET.
DATE
AIM:
To write a program to Perform EDA on Wine Quality Data Set.
PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Load the dataset
data = pd.read_csv("pathname")
# Display the first few rows of the dataset
print(data.head())
# Get information about the dataset
print(data.info())
# Summary statistics
print(data.describe())
# Distribution of wine quality
sns.countplot(data['quality'])
plt.title(" Wine Quality data set")
plt.show()
# Box plots for selected features by wine quality
features = ['alcohol', 'volatile acidity', 'citric acid', 'residual sugar']
for feature in features:
plt.figure(figsize=(8, 6))
sns.boxplot(x='quality', y=feature, data=data)
plt.title(f'{feature} by Wine Quality')
plt.show()
# Pair plot of selected features
sns.pairplot(data, vars=['alcohol', 'volatile acidity', 'citric acid', 'residual sugar'],
hue='quality', diag_kind='kde')
plt.suptitle("Pair Plot of Selected Features")
plt.show()
# Correlation heatmap
corr_matrix = data.corr()
plt.figure(figsize=(10, 8))
sns.heatmap(corr_matrix, annot=True, cmap="coolwarm", fmt=".2f")
plt.title("Correlation Heatmap")
plt.show()
# Histograms of selected features
features = ['alcohol', 'volatile acidity', 'citric acid', 'residual sugar']
for feature in features:
plt.figure(figsize=(6, 4))
sns.histplot(data[feature], kde=True, bins=20)
plt.title(f"Distribution of {feature}")
plt.show()
OUTPUT:
RESULT:
Thus the above program to to Perform EDA on Wine Quality Data Set.
EX NO:6
DATE: TIME SERIES ANALYSIS USING VARIOUS VISULAIZATION
TECHNIQUES
AIM:
To perform time series analysis and apply the various visualization techniques.
DOWNLOADING DATASET:
Step 1: Open google and type the following path in the address bar and download a dataset.
http://github.com/jbrownlee/Datasets.
Step 2: write the following code to get the details.
from pandas import read_csv
from matplotlib import pyplot
series=read_csv(‘pathname')
print(series.head())
series.plot()
pyplot.show()
OUTPUT:
Step 3: To get the time series line plot:
series.plot(style='-.')
pyplot.show()
Step 4:
To create a Histogram:
series.hist()
pyplot.show()
Step 5:
To create density plot:
series.plot(kind='kde')
pyplot.show()
Result:
Thus the above time analysis has been checked with Various visualization techniques.
EX NO: 7
DATE: DATA ANALYSIS AND REPRESENTATION ON A MAP
AIM:
Write a program to perform data analysis and representation on a map using various map data sets
with mouse rollover effect, user interaction.
PROCEDURE:
STEP 1:
Make sure to install the necessary libraries.
pip install geopandas folium bokeh
PROGRAM:
from bokeh.io import show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure
from bokeh.layouts import column
import pandas as pd
import folium
# Load your data
data = pd.read_csv('D:\ARCHANA\dxv\LAB\DXV\geographic.csv')
# Create a Bokeh figure
p = figure(width=800, height=400, tools='pan,wheel_zoom,reset')
# Create a ColumnDataSource to hold data
source = ColumnDataSource(data)
# Add circle markers to the figure
p.circle(x='Longitude', y='Latitude', size=10, source=source, color='orange')
# Create a hover tool for mouse rollover effect
hover = HoverTool()
hover.tooltips = [("Info", "@Info"), ("Latitude", "@Latitude"), ("Longitude",
"@Longitude")]
p.add_tools(hover)
# Display the Bokeh plot
layout = column(p)
show(layout)
# Create a map centered at a specific location
m = folium.Map(location=[latitude, longitude], zoom_start=10)
# Add markers for your data points
for index, row in data.iterrows():
folium.Marker(
location=[row['Latitude'], row['Longitude']],
popup=row['Info'], # Display additional info on mouse click
).add_to(m)
# Save the map to an HTML file
m.save('map.html')
OUPUT:
RESULT:
Data analysis and representation on a map using various map data sets with mouse rollover effect,
user interaction has been completed successfully.
EX NO: 8
DATE: BUILDING CARTOGRAPHIC VISUALIZATION
AIM:
Build cartographic visualization for multiple datasets involving various countries of the world;
states and districts in India etc
PROCEDURE:
STEP 1:
Collect Datasets
Gather the datasets containing geographical information for countries, states, or districts. Make sure these
datasets include the necessary attributes for mapping (e.g., country/state/district names, codes, and
relevant data).
STEP 2:
Install Required Libraries:
pip install geopandas matplotlib
STEP 3:
Load Geographic Data:
Use Geopandas to load the geographic data for countries, states, or districts. Make sure to match the
geographical data with your datasets based on the common attributes.
STEP 4:
Merge Datasets:
Merge your datasets with the geographic data based on common attributes. This step is crucial for linking
your data to the corresponding geographic regions.
STEP 5:
Create Cartographic Visualizations:
Use Matplotlib to create cartographic visualizations. You can create separate plots for different datasets
or overlay them on a single map.
STEP 6:
Customize and Enhance:
Customize your visualizations based on your needs. You can add legends, labels, titles, and other
elements to enhance the interpretability of your maps.
STEP 7:
Save and Share:
Save your visualizations as image files or interactive plots if needed. You can then share these
visualizations with others.
PROGRAM:
import pandas as pd
import geopandas as gpd
import shapely
# needs 'descartes'
import matplotlib.pyplot as plt
df = pd.DataFrame({'city': ['Berlin', 'Paris', 'Munich'],
'latitude': [52.518611111111, 48.856666666667, 48.137222222222],
'longitude': [13.408333333333, 2.3516666666667, 11.575555555556]})
gdf = gpd.GeoDataFrame(df.drop(['latitude', 'longitude'], axis=1),
crs={'init': 'epsg:4326'},
geometry=[shapely.geometry.Point(xy)
for xy in zip(df.longitude, df.latitude)])
print(gdf)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
base = world.plot(color='white', edgecolor='black')
gdf.plot(ax=base, marker='o', color='red', markersize=5)
plt.show()
OUTPUT:
city geometry
0 Berlin POINT (13.40833 52.51861)
1 Paris POINT (2.35167 48.85667)
2 Munich POINT (11.57556 48.13722)
RESULT:
Build cartographic visualization for multiple datasets involving various countries of the world;
has been visualized successfully.
EX NO :9
DATE: VISUALIZING VARIOUS EDA TECHNIQUES AS CASE STUDY FOR
IRIS DATASET
AIM:
Use a case study on a data set and apply the various EDA and visualization techniques and
present an analysis report.
PROCEDURE:
Import Libraries:
Start by importing the necessary libraries and loading the dataset.
Descriptive Statistics:
Compute and display descriptive statistics.
python
Check for Missing Values:
Verify if there are any missing values in the dataset.
Visualize Data Distributions:
Visualize the distribution of numerical variables.
python
Correlation Heatmap:
Examine the correlation between numerical variables.
Boxplots for Categorical Variables:
Use boxplots to visualize the distribution of features by species.
Violin Plots:
Combine box plots with kernel density estimation for better visualization.
Correlation between Features:
Visualize pair-wise feature correlations.
Conclusion and Summary:
Summarize key findings and insights from the analysis.
This case study provides a comprehensive analysis of the Iris dataset, including data exploration,
descriptive statistics, visualization of data distributions, correlation analysis, and feature-specific
visualizations.