CS4209- Data Science Laboratory Department of CSE Reg No:
1a. Program to check if the number is positive, negative or zero
Program:
# Input from the user
num = float(input("Enter a number: "))
# Check the condition if num> 0:
print("The number is positive.") elifnum< 0:
print("The number is negative.") else:
print("The number is zero.")
Output:
Enter a number: 5
The number is positive.
Enter a number: -3.2 The number
is negative.
Enter a number: 0 The number
is zero
[Link] n Natural Numbers
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
# Program to print first n natural numbers # Input from
the user
n = int(input("Enter the value of n: "))
# Loop to print natural numbers from 1 to n for i in
range(1, n + 1):
print(i, end=' ')
Output:
Enter the value of n: 5 1 2 3 4 5
2a. Max and Min element in ID
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
# Program to find the maximum and minimum elements in a 1D list
# Input: list of numbers from the user
nums = list(map(int, input("Enter numbers separated by space: ").split()))
# Find max and min
maximum = max(nums)
minimum = min(nums)
# Output
print("Maximum element:", maximum)
print("Minimum element:", minimum)
Output:
Enter numbers separated by space: 3 7 2 9 5
Maximum element: 9
Minimum element: 2
2b. Matrix Rotation
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
# Program to rotate a matrix 90 degrees clockwise # Sample 2D matrix
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Rotate 90° clockwise:
Transpose + Reverse each row rotated = [list(reversed(col)) for col in zip(*matrix)]
# Output
print("Original Matrix:") for row in matrix:
print(row)
print("\nRotated Matrix (90° Clockwise):") for row in rotated:
print(row)
Output:
Original Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Rotated Matrix (90° Clockwise): [7, 4, 1]
[8, 5, 2]
[9, 6, 3]
3a. Join 2 String
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
# Input: Two strings from the user
str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
# Join the strings
joined = str1 +“ ” + str2
# Output
print("Joined string:", joined)
Output:
Enter first string: Hello
Enter second string: World
Joined string: HelloWorld
3b. Sum of Digits using Recursive
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
# Recursive function to find the sum of digits
def sum_of_digits(n):
if n == 0:
return 0
else:
return n % 10 + sum_of_digits(n // 10)
# Input from user
num = int(input("Enter a number: "))
# Function call and output
print("Sum of digits:", sum_of_digits(num))
Output:
Enter a number: 1234 Sum of
digits: 10
4. List Programs
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
# Initial list
my_list = [5, 3, 8, 6, 3, 9]
print("Original list:", my_list)
# 1. Access elements by index
print("First element:", my_list[0])
print("Last element:", my_list[-1])
# 2. Append and insert
my_list.append(10)
print("After appending 10:", my_list)
my_list.insert(2, 15)
print("After inserting 15 at index 2:", my_list)
# 3. Delete elements
my_list.remove(3) # removes first occurrence
print("After removing first occurrence of 3:", my_list)
del my_list[1]
# delete by index
print("After deleting element at index 1:", my_list)
# 4. Length, Sum, Max, Min
print("Length of list:", len(my_list))
print("Sum of elements:",sum(my_list))
print("Maximum element:", max(my_list))
print("Minimum element:", min(my_list))
# 5. Sort and reverse
my_list.sort()
print("Sorted list:", my_list)
my_list.reverse()
print("Reversed list:", my_list)
# 6. Search for an element
element = 15
if element in my_list:
print(f"{element} found at index {my_list.index(element)}")
else:
print(f"{element} not found in the list")
# 7. Count occurrences
print("Count of 3:", my_list.count(3))
CS4209- Data Science Laboratory Department of CSE Reg No:
# 8. Concatenate two lists
list2 = [20, 25]
combined = my_list + list2
print("After concatenation with [20, 25]:", combined)
# 9. Copy list
copied_list = my_list.copy()
print("Copied list:", copied_list)
# 10. Remove duplicates using set
no_duplicates = list(set(my_list))
print("List after removing duplicates:", no_duplicates)
# 11. List comprehension: square of each element
squares = [x**2 for x in my_list]
print("Squares of elements:", squares)
# 12. Find even and odd numbers
evens = [x for x in my_list if x % 2 == 0]
odds = [x for x in my_list if x % 2 != 0]
print("Even numbers:", evens)
print("Odd numbers:", odds)
# 13. Common elements between two lists
list3 = [6, 9, 20, 100]
common = [x for x in my_list if x in list3]
print("Common elements with [6, 9, 20, 100]:", common)
# 14. Join list of strings into a single string
string_list = ['Python', 'is', 'fun']
joined_string = ' '.join(string_list)
print("Joined string:", joined_string)
# 15. Split string into list
split_list = joined_string.split()
print("Split string back into list:", split_list)
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
Original list: [5, 3, 8, 6, 3, 9]
First element: 5
Last element: 9
After appending 10: [5, 3, 8, 6, 3, 9, 10]
After inserting 15 at index 2: [5, 3, 15, 8, 6, 3, 9, 10]
After removing first occurrence of 3: [5, 15, 8, 6, 3, 9, 10]
After deleting element at index 1: [5, 8, 6, 3, 9, 10] Length of list: 6
Sum of elements: 41 Maximum
element: 10
Minimum element: 3
Sorted list: [3, 5, 6, 8, 9, 10]
Reversed list: [10, 9, 8, 6, 5, 3] 15 not found
in the list
Count of 3: 1
After concatenation with [20, 25]: [10, 9, 8, 6, 5, 3, 20, 25]
Copied list: [10, 9, 8, 6, 5, 3]
List after removing duplicates: [3, 5, 6, 8, 9, 10]
Squares of elements: [100, 81, 64, 36, 25, 9]
Even numbers: [10, 8, 6]
Odd numbers: [9, 5, 3]
Common elements with [6, 9, 20, 100]: [9, 6] Joined string:
Python is fun
Split string back into list: ['Python', 'is', 'fun']
5. Tuples and Dictionaries
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
# TUPLES
print("==== TUPLE OPERATIONS ====")
# Creating a tuple
my_tuple = (10, 20, 30, 40, 20, 50)
print("Original tuple:", my_tuple)
# Accessing elements
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])
# Slicing
print("Tuple slice [1:4]:", my_tuple[1:4])
# Length
print("Length of tuple:", len(my_tuple))
# Count and Index
print("Count of 20:", my_tuple.count(20))
print("Index of 30:", my_tuple.index(30))
# Looping through a tuple
print("Elements in tuple:")
for item in my_tuple:
print(item, end=' ')
print()
# Converting tuple to list for modification
temp_list = list(my_tuple)
temp_list.append(60)
my_tuple = tuple(temp_list)
print("Tuple after adding 60 (via list conversion):", my_tuple)
# Nested tuple
nested = (1, (2, 3), (4, 5))
print("Nested tuple:", nested)
print("Access 3 from nested tuple:", nested[1][1])
# ---------- DICTIONARIES ----------
print("\n==== DICTIONARY OPERATIONS ====")
CS4209- Data Science Laboratory Department of CSE Reg No:
# Creating a dictionary my_dict =
{
"name": "Alice", "age": 25,
"city": "New York"
}
print("Original dictionary:", my_dict)
# Accessing elements
print("Name:", my_dict["name"])
# Using get() to avoid error if key doesn't exist
print("Country (using get):", my_dict.get("country", "Not found"))
# Adding or updating values
my_dict["email"] = "alice@[Link]"
my_dict["age"] = 26
print("After adding email and updating age:", my_dict)
# Deleting a key del
my_dict["city"]
print("After deleting 'city':", my_dict)
# Dictionary keys, values, and items
print("Keys:", list(my_dict.keys()))
print("Values:", list(my_dict.values()))
print("Items:", list(my_dict.items()))
# Loop through dictionary
print("Dictionary content:")
for key, value in my_dict.items():
print(f"{key}: {value}")
# Check existence of a key
if "name" in my_dict:
print("'name' key exists in dictionary")
# Copy dictionary
copy_dict = my_dict.copy() print("Copied
dictionary:", copy_dict)
# Clear dictionary
copy_dict.clear()
print("Cleared copy dictionary:", copy_dict)
# Nested dictionary student = {
"name": "John", "marks": {
CS4209- Data Science Laboratory Department of CSE Reg No:
"math": 90,
"science": 85
}
}
print("Nested dictionary example:", student)
print("John's science mark:", student["marks"]["science"])
Output:
==== TUPLE OPERATIONS ====
Original tuple: (10, 20, 30, 40, 20, 50)
First element: 10
Last element: 50
Tuple slice [1:4]: (20, 30, 40)
Length of tuple: 6
Count of 20: 2
Index of 30: 2
Elements in tuple:
10 20 30 40 20 50
Tuple after adding 60 (via list conversion): (10, 20, 30, 40, 20, 50, 60)
Nested tuple: (1, (2, 3), (4, 5))
Access 3 from nested tuple: 3
==== DICTIONARY OPERATIONS ====
Original dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Name: Alice
Country (using get): Not found
After adding email and updating age: {'name': 'Alice', 'age': 26, 'city': 'New York', 'email':
'alice@[Link]'}
After deleting 'city': {'name': 'Alice', 'age': 26, 'email': 'alice@[Link]'}
Keys: ['name', 'age', 'email']
Values: ['Alice', 26, 'alice@[Link]']
Items: [('name', 'Alice'), ('age', 26), ('email', 'alice@[Link]')]
Dictionary content:
name: Alice
age: 26
email: alice@[Link] 'name' key
exists in dictionary
Copied dictionary: {'name': 'Alice', 'age': 26, 'email': 'alice@[Link]'}
Cleared copy dictionary: {}
Nested dictionary example: {'name': 'John', 'marks': {'math': 90, 'science': 85}}
John's science mark: 85
6. Packages for Data Science in Python.
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
Steps:
[Link] Anaconda
[Link] Anaconda
[Link] Anaconda
[Link] data science packages
1. Download Anaconda
This step downloads the Anaconda Python package for the Windows platform.
Anaconda is a free and easy-to-use environment for scientific Python.
1. Visit the Anaconda homepage.
2. Click “Anaconda” from the menu and click “Download” to go to the
download page.
3. Choose the download suitable for your platform (Windows, OSX, or Linux):
• Choose Python 3.5
• Choose the Graphical Installer
2. Install Anaconda
This step installs the Anaconda Python software on the system.
This step assumes that sufficient administrative privileges are contained to install software
on the system.
1. Double click the downloaded file.
2. Follow the installation wizard.
[Link] Anaconda
Anaconda comes with a suite of graphical tools called Anaconda Navigator. Start Anaconda
Navigator by opening it from the application launcher.
First, start with the Anaconda command line environment called conda.
Conda is fast, and simple, it’s hard for error messages to hide, and you
can quickly confirm your environment is installed and working correctly.
1. Open a terminal (command line window).
2. Confirm conda is installed correctly, by typing: conda -V
CS4209- Data Science Laboratory Department of CSE Reg No:
3. Confirm Python is installed correctly by typing: python –V
[Link] data science packages
With pip, a package is installed. To install the < package-name > generic package, run this
command:
$> pip install < package-name >
To install the < package-name > generic package, you just need to run the following
command:
$> conda install< package-name >
To install a particular version of the package
$> conda install< package-name > =1.11.0
To install multiple packages at once by listing all their names:
$> conda install < package-name-1 >< package-name-2 >
To update a package that you previously installed, you can keep on using conda:
$> conda update < package-name >
To update all the available packages simply by using the --all argument
$> conda update –all
To uninstall packages using conda:
$> conda remove < package-name >
a. NumPy
NumPy is the true analytical workhorse of the Python language. It provides the user with
multidimensional arrays, along with a large set of functions to operate a multiplicity of
mathematical operations on these arrays. Arrays are blocks of data arranged along multiple
dimensions, which implement mathematical vectors and matrices. Characterized by optimal
memory allocation, arrays are useful not just for storing data, but also for fast matrix
operations (vectorization), which are indispensable when solving ad hoc data science
problems.
$> conda install numpy
CS4209- Data Science Laboratory Department of CSE Reg No:
b. SciPy
SciPy completes NumPy's functionalities, offering a larger variety of scientific algorithms for
linear algebra, sparse matrices, signal and image processing, optimization, fast Fourier
transformation, and much more.
$> conda install scipy
c. Statsmodels
Statsmodels is a complement to SciPy's statistical functions. It features generalized linear
models, discrete choice models, time series analysis, and a series of descriptive statistics as
well as parametric and nonparametric tests.
[Link]
The pandas package deals with everything that NumPy and SciPy cannot do. Thanks to its
specific data structures, namely DataFrames and Series, pandas allow us to handle complex
tables of data of different types and time series. It enables the easy and smooth loading of
data from a variety of sources. The data can then be sliced, diced, handled with missing
elements, added, renamed, aggregated, reshaped, and finally visualized.
$> conda install pandas
d. Jupyter
A scientific approach requires the fast experimentation of different hypotheses in a
reproducible fashion. Initially named IPython and limited to working only with the Python
language, Jupyter was created to address the need for an interactive command shell for
several languages (based on the shell, web browser, and application interface), featuring
graphical integration, customizable commands, rich history (in the JSON format), and
computational parallelism for enhanced performance
$> conda install jupyter
7a. Basic Numpy Operations
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
(i). Creation of different types of Numpy arrays and displaying basic information
# Importing numpy import
numpy as np
# Defining 1D array
my1DArray = [Link]([1, 8, 27, 64])
print(my1DArray)
# Defining and printing 2D array
my2DArray = [Link]([[1, 2, 3, 4], [2, 4, 9, 16], [4, 8, 18, 32]])
print(my2DArray)
#Defining and printing 3D array
my3Darray = [Link]([[[ 1, 2 , 3 , 4],[ 5 , 6 , 7 ,8]], [[ 1, 2, 3, 4],[ 9, 10,
11, 12]]])
print(my3Darray)
# Print out memory address
print([Link])
# Print the shape of array
print([Link])
# Print out the data type of the array
print([Link])
# Print the stride of the array.
print([Link])
(ii).Creation of an array using built-in NumPy functions.
# Array of ones ones =
[Link]((3,4)) print(ones)
# Array of zeros
zeros = [Link]((2,3,4),dtype=np.int16) print(zeros)
# Array with random values
[Link]((2,2))
# Empty array
emptyArray = [Link]((3,2))
print(emptyArray)
CS4209- Data Science Laboratory Department of CSE Reg No:
# Full array
fullArray = [Link]((2,2),7) print(fullArray)
# Array of evenly-spaced values
evenSpacedArray2 = [Link](0,2,9)
print(evenSpacedArray2)
(iii). Performing file operations with NumPy arrays
import numpy as np #initialize
an array
arr = [Link]([[[11, 11, 9, 9], [11, 0, 2, 0]], [[10, 14, 9, 14], [0, 1, 11,
11]]])
# open a binary file in write mode file =
open("arr", "wb")
# save array to the file
[Link](file, arr)
# close the file [Link]
# open the file in read binary mode file =
open("arr", "rb")
#read the file to numpy array
arr1 = [Link](file)
#close the file print(arr1)
Output:
(i). Creation of different types of Numpy arrays and displaying basic information
[ 1 8 27 64]
[[ 1 2 3 4]
[ 2 4 9 16]
[ 4 8 18 32]]
[[[ 1 2 3 4]
[ 5 6 7 8]]
[[ 1 2 3 4]
[ 9 10 11 12]]]
CS4209- Data Science Laboratory Department of CSE Reg No:
(3, 4)
int32 (16, 4)
(ii). Creation of an array using built-in NumPy functions
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]]
[[0. 0.]
[0. 0.]
[0. 0.]]
[[7 7]
[7 7]]
[10 15 20]
[0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ]
(iii). Performing file operations with NumPy arrays
[[[11 11 9 9]
[11 0 2 0]]
[[10 14 9 14]
[ 0 1 11 11]]]
CS4209- Data Science Laboratory Department of CSE Reg No:
7b. Basic Arithmetic Operations with Numpy Arrays.
Program
import numpy as np
a = [Link](9, dtype = np.float_).reshape(3,3)
print ('First array:')
print (a)
print ('\n')
print ('Second array:')
b = [Link]([10,10,10])
print (b )
print ('\n')
print ('Add the two arrays:')
print ([Link](a,b))
print ('\n')
print ('Subtract the two arrays:')
print ([Link](a,b))
print ('\n')
print ('Multiply the two arrays:')
print ([Link](a,b))
print ('\n')
print ('Divide the two arrays:')
print ([Link](a,b))
Output:
First array:
[[ 0. 1.2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]
Second array:
[10 10 10]
Add the two arrays:
[[ 10. 11. 12.]
[ 13. 14. 15.]
[ 16. 17. 18.]]
Subtract the two arrays:
[[-10. -9. -8.]
[ -7. -6. -5.]
[ -4. -3. -2.]]
CS4209- Data Science Laboratory Department of CSE Reg No:
Multiply the two arrays:
[[ 0. 10. 20.]
[ 30. 40. 50.]
[ 60. 70. 80.]]
Divide the two arrays:
[[ 0. 0.1 0.2]
[ 0.3 0.4 0.5]
[ 0.6 0.7 0.8]]
CS4209- Data Science Laboratory Department of CSE Reg No:
[Link] with PANDAS Dataframes.
Program:
(i).Creation of a Dataframe from a Series:
import numpy as np
import pandas as pd
print("Pandas Version:", pd. version )
pd.set_option('display.max_columns', 500)
pd.set_option('display.max_rows', 500)
series = [Link]([2, 3, 7, 11, 13, 17, 19, 23])
print(series)
series_df = [Link]({
'A': range(1, 5),
'B': [Link]('20190526'),
'C': [Link](5, index=list(range(4)), dtype='float64'),
'D': [Link]([3] * 4, dtype='int64'),
'E': [Link](["Depression", "Social Anxiety", "Bipolar Disorder", "Eating Disorder"]),
'F': 'Mental health',
'G': 'is challenging'})
print(series_df)
(ii).Creation of a Dataframe from Dictionary
import numpy as np import
pandas as pd
dict_df = [{'A': 'Apple', 'B': 'Ball'},{'A': 'Aeroplane', 'B':'Bat', 'C': 'Cat'}] dict_df =
[Link](dict_df)
print(dict_df)
(iii).Creation of a Dataframe from N-Dimensional Arrays
import numpy as np
import pandas as pd
sdf = {'County':['Ostfold', 'Hordaland', 'Oslo', 'Hedmark', 'Oppland', 'Buskerud'],
'ISO-Code':[1,2,3,4,5,6],
'Area': [4180.69, 4917.94, 454.07, 27397.76, 25192.10, 14910.94],
'Administrative centre': ["Sarpsborg", "Oslo", "City of Oslo", "Hamar", "Lillehammer",
"Drammen"]}
sdf = [Link](sdf)
print(sdf)
CS4209- Data Science Laboratory Department of CSE Reg No:
(iv).Loading a Dataset from an external source into a PANDAS Data frame
import numpy as np import
pandas as pd
columns=['age', 'workclass', 'fnlwgt', 'education', 'education_num', 'marital_status',
'occupation', 'relationship', 'ethnicity', 'gender', 'capital_gain', 'capital_loss',
'hours_per_week', 'country_of_origin','income']
df=pd.read_csv('[Link]
databases/adult/[Link]',names=columns)
[Link](10)
Output:
(i).Creation of a dataframe from a series
Pandas Version: 1.3.4
02
13
27
3 11
4 13
5 17
6 19
7 23
dtype: int64
A B CD E F G
0 1 2019-05-26 5.0 3 Depression Mental health is challenging
1 2 2019-05-26 5.0 3 Social Anxiety Mental health is challenging
2 3 2019-05-26 5.0 3 Bipolar Disorder Mental health is challenging
3 4 2019-05-26 5.0 3 Eating Disorder Mental health is challenging
(ii).Creation of a dataframe from a dictionary
A B C
0 Apple Ball NaN
1 Aeroplane Bat Cat
(iii).Creation of a dataframe from n-dimensional array
County ISO-Code Area Administrative centre
0 Ostfold 1 4180.69 Sarpsborg
1 Hordaland 2 4917.94 Oslo
2 Oslo 3 454.07 City of Oslo
3 Hedmark 4 27397.76 Hamar
CS4209- Data Science Laboratory Department of CSE Reg No:
4 Oppland 5 25192.10 Lillehammer
5 Buskerud 6 14910.94 Drammen
CS4209- Data Science Laboratory Department of CSE Reg No:
9. Descriptive Analytics with Pandas on IRIS Data.
Program:
(i).Descriptive analytics on the Iris dataset by reading data from a specific location
in the computer or from web.
Code: Importing pandas to use in code as pd.
import pandas as pd
Code for reading data from CSV file
iris = pd.read_csv('[Link]', delimiter = ',')
Code for reading data from URL
a. Create csv_url and pass to it the URL where the data set is available
‘[Link] databases/iris/[Link]'. csv_url =
'[Link] learning-databases/iris/[Link]'
b. Create a list of column names “col_names” using the iris attribute information.
# using the attribute information as the column names
col_names=['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width', 'Class']
c. Create a panda’s DataFrame object called iris.
iris = pd.read_csv(csv_url, names = col_names)
Code: Display the top rows of the dataset with their columns
# Default value of head() function is 5, that is, it shows top 5 rows when no argument is
given [Link]()
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
Code: Display the specified number of rows randomly
[Link](10)
Code: Display the number of columns and names of the columns.
[Link]
Output:
Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_w idth',
'species'], dtype='object')
Code: Display the shape of the dataset
# Displays number of rows and columns.
[Link]
Output:
(150, 5)
Code: Display the whole dataset: iris
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
Code: Slicing the rows
# Prints the rows from 10 to 20
iris[10:21]
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
Code: Display the number of instances and attributes in the dataset
# Demonstrates a complete dataset - no null values
[Link]()
Output:
RangeIndex: 150 entries, 0 to 149 Data columns (total 5 columns):
# Column Non-Null Count Dtype
sepal_length 150 non-null float64
sepal_width 150 non-null float64
petal_length 150 non-null float64
petal_width 150 non-null float64
species 150 non-null object dtypes: float64(4), object(1) memory usage: 6.0+ KB
Code: Display the number of instances of each species
# Shows a balanced dataset - each type is equally represented
[Link]('species').size()
Output:
species setosa 50
versicolor 50
virginica 50 dtype:
int64
Code: Display the Datatypes of each of the attributes
The columns of the resulting DataFrame have different dtypes.
[Link]
Output:
Sepal_Length float64
Sepal_Width float64
Petal_Length float64
Petal_Width float64
Class object dtype: object
Code: Display basic statistical features of the Dataset
[Link]()
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
Code: Count the number of rows on the dataset
[Link]()
Output:
sepal_length 150
sepal_width 150
petal_length 150
petal_width 150
species 150
dtype: int64
Code: Number of counts of unique values using “value counts()”
iris[“species”].value counts()
Output:
setosa 50
versicolor 50
virginica 50
Name: species, dtype: int64
# Sample mean for every numeric column
[Link]()
Output:
sepal_length 5.843333
sepal_width 3.054000
petal_length 3.758667
petal_width 1.198667
dtype: float64
# Sample median for every numeric column
[Link]()
Output:
sepal_length 5.80
sepal_width 3.00
CS4209- Data Science Laboratory Department of CSE Reg No:
petal_length 4.35
petal_width 1.30
dtype: float64
# Sample variance for every numeric column
[Link]()
Output:
sepal_length 0.685694
sepal_width 0.188004
petal_length 3.113179
petal_width 0.582414
dtype: float64
# Sample standard deviance for every numeric column
[Link]()
Output:
sepal_length 0.828066
sepal_width 0.433594
petal_length 1.764420
petal_width 0.763161
dtype: float64
(ii).Descriptive analytics on the Iris dataset by reading data from the scikit-learn
datasets module
Code: Load iris dataset from scikit learn datasets module
from [Link] import load_iris
iris= load_iris()
Code: Store features matrix in X
X= [Link]
Code: Store target vector in y
y=[Link]
Code: Names of features/columns in iris dataset
iris.features_names
Output:
['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']
Code: Display names of the target/output in iris dataset
print(iris.target_names)
Output:
['setosa' 'versicolor' 'virginica']
Code: Examine the size of feature matrix
print([Link])
CS4209- Data Science Laboratory Department of CSE Reg No:
Output: (150, 4)
Code: Display the size of target vector
print([Link])
Output: (150,)
Code: Display the contents of the data
print([Link])
Output:
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
[5.4 3.7 1.5 0.2]
[4.8 3.4 1.6 0.2]
[4.8 3. 1.4 0.1]
[4.3 3. 1.1 0.1]
[5.8 4. 1.2 0.2]
[5.7 4.4 1.5 0.4]
[5.4 3.9 1.3 0.4]
[5.1 3.5 1.4 0.3]
[5.7 3.8 1.7 0.3]
[5.1 3.8 1.5 0.3]
[5.4 3.4 1.7 0.2]
[5.1 3.7 1.5 0.4]
[4.6 3.6 1. 0.2]
[5.1 3.3 1.7 0.5]
[4.8 3.4 1.9 0.2]
[5. 3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 3.1 5.5 1.8]
[6. 3. 4.8 1.8]
[6.9 3.1 5.4 2.1]
[6.7 3.1 5.6 2.4]
[6.9 3.1 5.1 2.3]
[5.8 2.7 5.1 1.9]
[6.8 3.2 5.9 2.3]
[6.7 3.3 5.7 2.5]
[6.7 3. 5.2 2.3]
[6.3 2.5 5. 1.9]
[6.5 3. 5.2 2. ]
[6.2 3.4 5.4 2.3]
[5.9 3. 5.1 1.8]]
Code: Display the target vector iris species : 0=setosa, 1=versicolor, 2=virginica
print([Link])
Output:
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
000000000001111111111111111111111111111
111111111111111111111122222222222222222
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
Code: Convert into dataframe
import pandas as pd
import numpy as np
df = [Link](data= np.c_[iris['data'], iris['target']], columns=
iris['feature_names'] + ['Species'])
CS4209- Data Science Laboratory Department of CSE Reg No:
# Distribution of each Iris species
df['Species'].value_counts()
Output:
0.0 50
1.0 50
2.0 50
Name: Species, dtype: int64
Code: Display basic statistical features of the Dataset
[Link]()
Output:
10a. Univariate Statistical Analysis on Diabetes Data
CS4209- Data Science Laboratory Department of CSE Reg No:
Program:
Code: Import the packages
import pandas as pd
import numpy as np
import statistics as st
Code: Load the pima_diabetes data
df=pd.read_csv("pima_diabetes.csv")
Code: Shape of the dataset
print([Link])
Output:
(768, 9)
Code: Display the number of instances and attributes in the dataset
print([Link]())
Output:
Code: Mean of the numerical variables in the data
[Link]()
Output:
Code: Calculate the mean of the variables 'Pregnancies' and 'Glucose'
CS4209- Data Science Laboratory Department of CSE Reg No:
print([Link][:,'Pregnancies'].mean())
print([Link][:,'Glucose'].mean())
Output:
3.8450520833333335
120.89453125
Code: Calculate the mean of the first five rows
[Link](axis = 1)[0:5]
Output:
Code: Median of the numerical variables in the data
[Link]()
Output:
Code: Calculate median of a particular column
print([Link][:,'Pregnancies'].median())
print([Link][:,'Glucose'].median())
Output:
3.0
117.0
Code: Calculate the median of the first five rows
CS4209- Data Science Laboratory Department of CSE Reg No:
[Link](axis = 1)[0:5]
Output:
Code: Compute the mode of all the variables in the data
[Link]()
Output:
Code: Compute standard deviation of all the numerical variables in the data
[Link]()
Output:
Code: Calculate the standard deviation of a particular variable
print([Link][:,' Pregnancies'].std())
print([Link][:,' Glucose '].std())
Output:
3.3695780626988623
31.97261819513622
Code: Calculate the standard deviation for the first five rows
[Link](axis = 1)[0:5]
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
# Print the variance of all the numerical variables in the dataset
[Link]()
Output:
Code: Calculate the skewness of the numerical variables using the skew() function
print([Link]())
Output:
Code: Calculate the kurtosis of the numerical variables using the kurtosis() function
print([Link]())
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
CS4209- Data Science Laboratory Department of CSE Reg No:
10b. Bivariate Analysis on Diabetes Data
(i). Bivariate Analysis using Linear Regression
Program:
import pandas as pd
import [Link] as sm
data = pd.read_csv(“pima_diabetes.csv") #create
correlation matrix
[Link]()
#Bivariate Analysis of Glucose-Insulin features
#define response variable 1
y1 = data['Glucose']
#define explanatory variable 1
x1 = data[['Insulin']]
#add constant to predictor variables
x1 = sm.add_constant(x1)
#fit linear regression model
model1 = [Link](y1, x1).fit()
#view model summary
print([Link]())
#Bivariate Analysis of Age-Pregnancies features
#define response variable 2
y2 = data['Age']
#define explanatory variable 2
x2 = data['Pregnancies']
#add constant to predictor variables
x2 = sm.add_constant(x2)
#fit linear regression model
model2 = [Link](y2, x2).fit()
#view model summary
print([Link]())
CS4209- Data Science Laboratory Department of CSE Reg No:
#Bivariate Analysis of SkinThickness-BMI features
#define response variable 3
y3 = data['SkinThickness']
#define explanatory variable 3
x3 = data[['BMI']]
#add constant to predictor variables
x3 = sm.add_constant(x3)
#fit linear regression model
Model3 = [Link](y3, x3).fit()
#view model summary
print([Link]())
Output:
[Link] Matrix
[Link] Analysis of Glucose-Insulin features
CS4209- Data Science Laboratory Department of CSE Reg No:
c. Bivariate Analysis of Age-Pregnancies features
d. Bivariate Analysis of SkinThickness-BMI features
CS4209- Data Science Laboratory Department of CSE Reg No:
(ii). Bivariate Analysis Using Logistic Regression
Program:
# importing libraries
import [Link] as sm
import pandas as pd
# loading the training dataset
data = pd.read_csv('pima_diabetes.csv', index_col = 0)
# defining the dependent and independent variables
Xtrain = data[['Glucose','SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction','Age']]
ytrain = data[['Outcome']]
# building the model and fitting the data
log_reg = [Link](ytrain, Xtrain).fit()
# printing the summary table
print(log_reg.summary()),
Output:
CS4209- Data Science Laboratory Department of CSE Reg No:
[Link] Regression Analysis on Diabetes Data
Program:
# importing modules and packages import
pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression import
[Link] as ssm
# importing data
df = pd.read_csv('pima_diabetes.csv')
# creating feature variables
X = [Link]('Outcome', axis=1)
Y = df['Outcome']
X=ssm.add_constant(X) #to add constant value in the model
model= [Link](Y,X).fit() #fitting the model
predictions= [Link]() #summary of the model predictions
Output: