0% found this document useful (0 votes)
10 views9 pages

Statistical Exp 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Statistical Exp 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Harsh 11232640 3A1

Practical no.9
AIM: Implementation of database connectivity in python and perform CRUD
(Create, Retrieve, Update and Delete) operations on database.

SOURCECODE:
import sqlite3
conn = sqlite3.connect('test.db')
print("Opened database successfully")

# Step 1
query = """CREATE TABLE COMPANY_6 (ID INT PRIMARY KEY NOT NULL, NAME
TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL)"""
conn.execute(query)
print("Table created successfully")
# Step 2
query = """ INSERT INTO COMPANY_6(ID,NAME,AGE,ADDRESS,SALARY) VALUES
(1, 'Paul', 32, 'California', 21040.00 ) """
conn.execute(query)
conn.commit()
print("Records created successfully")
# Step 3
cursor = conn.execute("SELECT id, name, address, salary from COMPANY_6")
for row in cursor:
print("ID = ", row[0])
print("NAME = ", row[1])
print("ADDRESS = ", row[2])
print("SALARY = ", row[3])
print("Operation done successfully")
conn.close()

26
MMEC,MMDU
Harsh 11232640 3A1

# Step 4
cursor = conn.execute("DELETE FROM COMPANY_6 WHERE ID = 1")
print("Record delete successfully")
conn.close()

OUTPUT:

27
MMEC,MMDU
Harsh 11232640 3A1

Project no.1
Aim: To calculate Measure of Central Tendency and Dispersion.

SOURCE CODE:
def calculate_mean(data):
return sum(data) / len(data)

def calculate_median(data):
data.sort()
n = len(data)
if n % 2 == 1:
return data[n // 2]
else:
mid1 = data[n // 2 - 1]
mid2 = data[n // 2]
return (mid1 + mid2) / 2

def calculate_mode(data):
frequency = {}
for value in data:
if value in frequency:
frequency[value] += 1
else:
frequency[value] = 1
max_frequency = max(frequency.values())
modes = [value for value, freq in frequency.items() if freq == max_frequency]
if len(modes) == 1:

28
MMEC,MMDU
Harsh 11232640 3A1

return modes[0]
else:
return modes

def calculate_range(data):
return max(data) - min(data)

def calculate_variance(data):
mean = calculate_mean(data)
squared_diffs = [(x - mean) ** 2 for x in data]
return sum(squared_diffs) / len(data)

def calculate_std_dev(data):
variance = calculate_variance(data)
return variance ** 0.5

def main():
# Get user input
data = input("Enter a list of numbers separated by commas: ")
data = [float(x) for x in data.split(",")]

# Calculate measures of central tendency and dispersion


mean = calculate_mean(data)
median = calculate_median(data)
mode = calculate_mode(data)
range_ = calculate_range(data)
variance = calculate_variance(data)

29
MMEC,MMDU
Harsh 11232640 3A1

std_dev = calculate_std_dev(data)

# Print results
print("Central Tendency:")
print(f"Mean: {mean}")
print(f"Median: {median}")
if isinstance(mode, list):
print(f"Mode: {mode} (multiple modes)")
else:
print(f"Mode: {mode}")

print("\nDispersion:")
print(f"Range: {range_}")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_dev}")

if __name__ == "__main__":
main ()

30
MMEC,MMDU
Harsh 11232640 3A1

OUTPUT/RESULT:

31
MMEC,MMDU
Harsh 11232640 3A1

Project no.2
Aim: To perform Correlation Analysis.

SOURCE CODE:
def calculate_mean(data):
return sum(data) / len(data)

def calculate_deviation(data, mean):


return [i - mean for i in data]

def calculate_covariance(dev_x, dev_y):


return sum([i*j for i, j in zip(dev_x, dev_y)]) / len(dev_x)

def calculate_variance(deviation):
return sum([i**2 for i in deviation]) / len(deviation)

def calculate_correlation_coefficient(covariance, variance_x, variance_y):


return covariance / (variance_x ** 0.5 * variance_y ** 0.5)

# Get user input


x = input("Enter the values of x (separated by space): ").split()
x = [float(i) for i in x]

y = input("Enter the values of y (separated by space): ").split()


y = [float(i) for i in y]

# Calculate the mean of x and y


mean_x = calculate_mean(x)
mean_y = calculate_mean(y)

32
MMEC,MMDU
Harsh 11232640 3A1

# Calculate the deviation of x and y from the mean


dev_x = calculate_deviation(x, mean_x)
dev_y = calculate_deviation(y, mean_y)

# Calculate the covariance of x and y


covariance = calculate_covariance(dev_x, dev_y)

# Calculate the variance of x and y


variance_x = calculate_variance(dev_x)
variance_y = calculate_variance(dev_y)

# Calculate the correlation coefficient


correlation_coefficient = calculate_correlation_coefficient(covariance, variance_x,
variance_y)

# Print the results


print("Results:")
print("--------")
print("Mean of x:", mean_x)
print("Mean of y:", mean_y)
print("Deviation of x from mean:", dev_x)
print("Deviation of y from mean:", dev_y)
print("Covariance of x and y:", covariance)
print("Variance of x:", variance_x)
print("Variance of y:", variance_y)
print("Correlation Coefficient:", correlation_coefficient)

33
MMEC,MMDU
Harsh 11232640 3A1

OUTPUT/RESULT:

34
MMEC,MMDU

You might also like