0% found this document useful (0 votes)
3 views

Data A

Uploaded by

alishacalista238
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data A

Uploaded by

alishacalista238
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAM-1

Write a program to find max, min, sum, average, square root and round off of given list of
numbers.
import math

def calculatedData(data):

max_num = max(data)

min_num = min(data)

total_sum = sum(data)

average = total_sum / len(data)

square_roots = [math.sqrt(num) for num in data]

rounded_number = [round(num) for num in data]

return max_num, min_num, total_sum, average, square_roots, rounded_number

def main():

print("ADITYA PRATAP SINGH")

n = int(input("Enter the length of the list: "))

data = []

for i in range(n):

num = float(input(f"Enter number {i + 1}: "))

data.append(num)

maxElement, minElement, totalSum, avg, square_roots, rounded_number = calculatedData(data)

print(f"Max: {maxElement}")

print(f"Min: {minElement}")

print(f"Sum: {totalSum}")

print(f"Average: {avg}")

print("Square roots of each element:")

for num in square_roots:

print(num)

print("Rounded off elements:")

for num in rounded_number:

print(num)

if __name__ == "__main__":
main()

OUTPUT
ADITYA PRATAP SINGH

Enter the length of the list: 4

Enter number 1: 1

Enter number 2: 2

Enter number 3: 3

Enter number 4: 4

Max: 4.0

Min: 1.0

Sum: 10.0

Average: 2.5

Square roots of each element:

1.0

1.4142135623730951

1.7320508075688772

2.0

Rounded off elements:

4
PROGRAM-2
Write a program to input two matrices and perform addition, subtraction, multiplication,
division and transpose of matrices.
import numpy

x = numpy.array([[1, 2], [4, 5]])

y = numpy.array([[7, 8], [9, 10]])

print ("The element wise addition of matrix is : ")

print (numpy.add(x,y))

print ("The element wise subtraction of matrix is : ")

print (numpy.subtract(x,y))

print ("The element wise division of matrix is : ")

print (numpy.divide(x,y))

print ("The element wise multiplication of matrix is : ")

print (numpy.multiply(x,y))

print ("The product of matrices is : ")

print (numpy.dot(x,y))

print ("The transpose of given matrix is : ")

print (x.T)

OUTPUT
The element wise addition of matrix is :

[[ 8 10]

[13 15]]

The element wise subtraction of matrix is :

[[-6 -6]

[-5 -5]]

The element wise division of matrix is :

[[0.14285714 0.25 ]

[0.44444444 0.5 ]]

The element wise multiplication of matrix is :

[[ 7 16]
[36 50]]

The product of matrices is :

[[25 28]

[73 82]]

The transpose of given matrix is :

[[1 4]

[2 5]]
PROGRAM-3
Write a program to find mean, median, mode and standard deviation of given data.
import numpy as np

from scipy import stats

def calculate_statistics(data):

mean = np.mean(data)

median = np.median(data)

mode = stats.mode(data)[0][0]

std_dev = np.std(data)

return mean, median, mode, std_dev

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

mean, median, mode, std_dev = calculate_statistics(data)

print("Mean:", mean)

print("Median:", median)

print("Mode:", mode)

print("Standard Deviation:", std_dev)

OUTPUT
Mean: 5.5

Median: 5.5

Mode: 1

Standard Deviation: 2.8722813232690143


PROGRAM-4
Write a program to perform linear regression on the given array and find intercept and
coefficient of regression.
import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([[1], [2], [3], [4], [5]])


Y= np.array([2, 3.1, 4.2, 5.3, 6.2])
model = LinearRegression()
model.fit(X, Y)
X_new = np.array([[6]])

predicted_value = model.predict(X_new)
print("Predicted value for X_new:", predicted_value[0])
print("Intercept:", model.intercept_)
print("Coefficient:", model.coef_[0])

OUTPUT
Predicted value for X_new: 7.34
Intercept: 0.98
Coefficient: 1.06
PROGRAM-5
Write program to find principle components using PCA on the given dataset.
import pandas as pd

import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
def load_and_preprocess(data_path):
data = pd.read_csv(data_path)

numerical_features = data.select_dtypes(include=[np.number])
scaler = StandardScaler()
scaled_features =
scaler.fit_transform(numerical_features.fillna(numerical_features.mean()))
return scaled_features, numerical_features.columns
def apply_pca(scaled_features, n_components=10):
pca = PCA(n_components=n_components)

principal_components = pca.fit_transform(scaled_features)
return pca, principal_components
def save_loadings(pca, feature_names, output_path):
loadings = pd.DataFrame(pca.components_.T, columns=[f'PC{i+1}' for i in
range(pca.n_components_)], index=feature_names)
loadings.to_csv(output_path)

print(f"Loadings saved to {output_path}")


if _name_ == "_main_":
data_path = '/path/to/your/data.csv'
output_path = '/path/to/save/pca_loadings.csv'
scaled_features, feature_names = load_and_preprocess(data_path)

pca, principal_components = apply_pca(scaled_features)


save_loadings(pca, feature_names, output_path)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy