0% found this document useful (0 votes)
7 views4 pages

Python Programming Lab Assignments

This document contains a Python programming lab assignment with five tasks. The tasks include implementing PCA for dimensionality reduction, merging multiple dictionaries, listing directories and files in a path, performing a chi-square test, and concatenating multiple DataFrames. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

ravikumar955051
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)
7 views4 pages

Python Programming Lab Assignments

This document contains a Python programming lab assignment with five tasks. The tasks include implementing PCA for dimensionality reduction, merging multiple dictionaries, listing directories and files in a path, performing a chi-square test, and concatenating multiple DataFrames. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

ravikumar955051
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

Python Programming Lab

Assignment 2

[Each Q- 20 Marks]

1. Develop a Python program to perform PCA (Principal Component


Analysis) for dimensionality reduction.

import numpy as np
from [Link] import PCA
from [Link] import make_classification

# Generating a sample dataset


X, y = make_classification(n_samples=100, n_features=5, random_state=42)

# Applying PCA
pca =
PCA(n_components=2)
X_reduced =
pca.fit_transform(X)

# Displaying the transformed dataset


print("Transformed Dataset:")
print(X_reduced)

2. Implement a Python function to merge multiple dictionaries into one.


def merge_dictionaries(dicts):
merged_dict = {}
for dictionary in dicts:
merged_dict.update(dictionary)
return merged_dict

dict1 = {'a': 1, 'b': 2}


dict2 = {'b': 3, 'c':
4} dict3 = {'d': 5}

result = merge_dictionaries(dict1, dict2, dict3)


print(result)
3. Write a program to list all the directories, subdirectories, and files in a
given path.

import os

def list_directories_files(path):
for root, dirs, files in [Link](path):
level = [Link](path,
'').count([Link]) indent = ' ' * 4 *
(level)
print(f"{indent}{[Link](r
oot)}/") subindent = ' ' * 4 * (level +
1)
for f in files:
print(f"{subindent
}{f}")

list_directories_files('/your/pat
h/here')
print(list_directories_files)

4. Write a script to perform a chi-square test.

import [Link] as stats

# Define the data


data = [[30, 10], [20, 20]]

# Perform the Chi-Square test


chi2, p, dof, expected = stats.chi2_contingency(data)
# Print the results
print("Chi-Square value:",
chi2) print("P-value:", p)
print("Degrees of
freedom:", dof)
print("Expected frequencies:\n", expected)

5. Write a Python program to concatenate multiple DataFrames.

import pandas as pd
# Example DataFrames
df1 = [Link]({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = [Link]({'A': [7, 8, 9], 'B': [10, 11, 12]})
df3 = [Link]({'A': [13, 14, 15], 'B': [16, 17, 18]})

# Concatenate DataFrames
concatenated_df = [Link]([df1,
df2, df3])

print(concatenated_df)

You might also like