Data A
Data A
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)
def main():
data = []
for i in range(n):
data.append(num)
print(f"Max: {maxElement}")
print(f"Min: {minElement}")
print(f"Sum: {totalSum}")
print(f"Average: {avg}")
print(num)
print(num)
if __name__ == "__main__":
main()
OUTPUT
ADITYA PRATAP SINGH
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
1.0
1.4142135623730951
1.7320508075688772
2.0
4
PROGRAM-2
Write a program to input two matrices and perform addition, subtraction, multiplication,
division and transpose of matrices.
import numpy
print (numpy.add(x,y))
print (numpy.subtract(x,y))
print (numpy.divide(x,y))
print (numpy.multiply(x,y))
print (numpy.dot(x,y))
print (x.T)
OUTPUT
The element wise addition of matrix is :
[[ 8 10]
[13 15]]
[[-6 -6]
[-5 -5]]
[[0.14285714 0.25 ]
[0.44444444 0.5 ]]
[[ 7 16]
[36 50]]
[[25 28]
[73 82]]
[[1 4]
[2 5]]
PROGRAM-3
Write a program to find mean, median, mode and standard deviation of given data.
import numpy as np
def calculate_statistics(data):
mean = np.mean(data)
median = np.median(data)
mode = stats.mode(data)[0][0]
std_dev = np.std(data)
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
OUTPUT
Mean: 5.5
Median: 5.5
Mode: 1
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)