0% found this document useful (0 votes)
14 views15 pages

Batch1 Ds

Uploaded by

ece apce
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)
14 views15 pages

Batch1 Ds

Uploaded by

ece apce
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/ 15

Batch 1

1. Write a NumPy program to search the index of a given array in another given array.

Program:

import numpy as np

def find_index(main_array, sub_array):


main_array = np.array(main_array)
sub_array = np.array(sub_array)

result = np.where(np.all(main_array == sub_array, axis=1))

if result[0].size > 0:
return result[0][0]
else:
return -1

main_array = [[1, 2], [3, 4], [5, 6]]


sub_array = [3, 4]
index = find_index(main_array, sub_array)
print(f"Index of sub_array: {index}")

output

Index of sub_array: 1

2, Write a NumPy program to convert a Python dictionary to a NumPy ndarray.

Program:

import numpy as np

# Example Python dictionary


my_dict = {'a': 1, 'b': 2, 'c': 3}

# Convert both keys and values into a NumPy ndarray


np_array = np.array(list(my_dict.items()))

print("NumPy ndarray with keys and values:", np_array)

output:

NumPy ndarray with keys and values:

[['a' '1']
['b' '2']
['c' '3']]
3..Write a Pandas program to select all columns, except one given column
in a DataFrame.

Program:

import pandas as pd
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9],
}
df = pd.DataFrame(data)
column_to_exclude = 'B'
df_selected = df.drop(columns=[column_to_exclude])
print(df_selected)

output:

A C
0 1 7
1 2 8
2 3 9
4 . Write a Pandas program to get first n records of a DataFrame.

Program:

import pandas as pd
data = {
'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15],
}
df = pd.DataFrame(data)
n=3
first_n_records = df.head(n)
print(first_n_records)

output:

A B C
0 1 6 11
1 2 7 12
2 3 8 13

5. Apply and explore various plotting functions on UCI data set for performing the
following:
a) Correlation and scatter plots
b) Histograms
c) Three-dimensional plotting.
Program:

# Import necessary libraries


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.datasets import load_iris

# Load the Iris dataset


iris = load_iris()
data = pd.DataFrame(iris.data, columns=iris.feature_names)
data['species'] = iris.target

# Map target integers to species names


data['species'] = data['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})

# Display first few rows of the dataset


print(data.head())

# 1. Correlation and Scatter Plots


# Compute correlation matrix
corr_matrix = data.iloc[:, :-1].corr()
print("\nCorrelation Matrix:\n", corr_matrix)
# Heatmap of correlation matrix
plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt=".2f")
plt.title("Correlation Matrix Heatmap")
plt.show()

# Pairwise scatter plots


sns.pairplot(data, hue='species', diag_kind='hist')
plt.suptitle("Pairwise Scatter Plots", y=1.02)
plt.show()

# 2. Histograms
data.iloc[:, :-1].hist(bins=15, figsize=(10, 8), color='skyblue', edgecolor='black')
plt.suptitle("Histograms of Features")
plt.show()

# 3. Three-dimensional Plotting
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Scatter plot in 3D
scatter = ax.scatter(
data.iloc[:, 0], # First feature
data.iloc[:, 1], # Second feature
data.iloc[:, 2], # Third feature
c=data['species'].map({'setosa': 'r', 'versicolor': 'g', 'virginica': 'b'}),
label=data['species'],
alpha=0.7
)

# Labels and title


ax.set_xlabel('Sepal Length (cm)')
ax.set_ylabel('Sepal Width (cm)')
ax.set_zlabel('Petal Length (cm)')
plt.title("3D Scatter Plot of Iris Dataset")
plt.legend(['Setosa', 'Versicolor', 'Virginica'], loc='upper left')
plt.show()

output:
6. Apply and explore various plotting functions on UCI data set for performing the
following:
a) Normal values
b) Density and contour plots
c) Three-dimensional plotting

Program:
# Import necessary libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from mpl_toolkits.mplot3d import Axes3D
from scipy.stats import zscore

# Load the Iris dataset


iris = load_iris()
data = pd.DataFrame(iris.data, columns=iris.feature_names)
data['species'] = iris.target

# Map target integers to species names


data['species'] = data['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})

# Display the first few rows of the dataset


print(data.head())

# (a) Normalize Values


# Normalize the numerical features using z-score normalization
normalized_data = data.iloc[:, :-1].apply(zscore)
normalized_data['species'] = data['species']

# Plot normalized values


plt.figure(figsize=(10, 6))
for col in normalized_data.columns[:-1]:
plt.plot(normalized_data[col], label=col)

plt.title("Normalized Values of Features")


plt.xlabel("Sample Index")
plt.ylabel("Z-score")
plt.legend()
plt.show()

# (b) Density and Contour Plots


# Kernel density estimation (KDE) plot for two features
plt.figure(figsize=(8, 6))
sns.kdeplot(
x=data.iloc[:, 0],
y=data.iloc[:, 2],
hue=data['species'],
fill=True,
cmap="viridis",
alpha=0.7
)
plt.title("Density and Contour Plot")
plt.xlabel("Sepal Length (cm)")
plt.ylabel("Petal Length (cm)")
plt.show()

# (c) Three-dimensional Plotting


fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# 3D scatter plot with different species


scatter = ax.scatter(
data.iloc[:, 0], # Sepal Length
data.iloc[:, 1], # Sepal Width
data.iloc[:, 2], # Petal Length
c=data['species'].map({'setosa': 'r', 'versicolor': 'g', 'virginica': 'b'}),
alpha=0.7
)
# Labels and title
ax.set_xlabel('Sepal Length (cm)')
ax.set_ylabel('Sepal Width (cm)')
ax.set_zlabel('Petal Length (cm)')
plt.title("3D Scatter Plot of Iris Dataset")
plt.legend(['Setosa', 'Versicolor', 'Virginica'], loc='upper left')
plt.show()

output:
7. Use the diabetes data set from UCI data set for performing the following:
Apply Bivariate analysis:
• Multiple Regression analysis

8. Use the diabetes data set from UCI data set for performing the following:
Apply Bivariate analysis:
• Linear and logistic regression modeling

9. Use the diabetes data set from UCI data set for performing the following:
Apply Univariate analysis:
• Frequency
• Mean,
• Median,
• Mode,
• Variance
• Standard Deviation
• Skewness and Kurtosis

10. Reading data from text files, Excel and the web and exploring various
commands for doing descriptive analytics on the Iris data set

11. Write a Pandas program to get the first 3 rows of a given


DataFrame.

# Import the pandas library


import pandas as pd

# Define a dictionary with sample data


data = {
"Name": ["Alice", "Bob", "Charlie", "David", "Eva"],
"Score": [85, 78, 90, 65, 88],
"Attempts": [1, 3, 2, 4, 3],
"Grade": ["A", "B", "A", "C", "B"]
}

# Create a DataFrame from the dictionary


df = pd.DataFrame(data)

# Get the first 3 rows of the DataFrame


first_three_rows = df.head(3)

# Display the first 3 rows


print("First 3 rows of the DataFrame:")
print(first_three_rows)

output:

First 3 rows of the DataFrame:


Name Score Attempts Grade
0 Alice 85 1 A
1 Bob 78 3 B
2 Charlie 90 2 A

12. Write a Pandas program to select the rows where the number of attempts in
the examination is
greater than 2.

Program:

# Import the pandas library


import pandas as pd

# Define a dictionary with sample data


data = {
"Name": ["Alice", "Bob", "Charlie", "David", "Eva"],
"Score": [85, 78, 90, 65, 88],
"Attempts": [1, 3, 2, 4, 3],
"Grade": ["A", "B", "A", "C", "B"]
}

# Create a DataFrame from the dictionary


df = pd.DataFrame(data)

# Select rows where 'Attempts' > 2


filtered_df = df[df["Attempts"] > 2]

# Display the filtered DataFrame


print("Rows where the number of attempts is greater than 2:")
print(filtered_df)

output:
Rows where the number of attempts is greater than 2:
Name Score Attempts Grade
1 Bob 78 3 B
3 David 65 4 C
4 Eva 88 3 B

13. Write a Pandas program to create and display a DataFrame from a


specified dictionary data which
has the index labels.

Program:
# Import the pandas library
import pandas as pd

# Define a dictionary with data


data = {
"Name": ["Alice", "Bob", "Charlie", "David"],
"Age": [25, 30, 35, 40],
"City": ["New York", "Los Angeles", "Chicago", "Houston"],
"Score": [85, 90, 88, 92]
}

# Define custom index labels


index_labels = ["A", "B", "C", "D"]

# Create a DataFrame from the dictionary with the specified index labels
df = pd.DataFrame(data, index=index_labels)

# Display the DataFrame


print("DataFrame:")
print(df)

output:

DataFrame:
Name Age City Score
A Alice 25 New York 85
B Bob 30 Los Angeles 90
C Charlie 35 Chicago 88
D David 40 Houston 92

14.

a. Write a NumPy program to convert an array to a float type


Program
import numpy as np

# Create an integer array


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

# Convert the array to float type


arr_float = arr.astype(float)

# Display the result


print("Original array:", arr)
print("Array converted to float type:", arr_float)

output:
Original array: [1 2 3 4 5]
Array converted to float type: [1. 2. 3. 4. 5.]
[ ]:
1

b. Write a NumPy program to create an empty and a full array


Program;
import numpy as np

# Create an empty array (values are random and uninitialized)


empty_arr = np.empty((3, 3))

# Create a full array with all elements set to a specific value


full_arr = np.full((3, 3), 7)

# Display the arrays


print("Empty array:")
print(empty_arr)
print("\nFull array (filled with 7):")
print(full_arr)

output:
Empty array:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

Full array (filled with 7):


[[7 7 7]
[7 7 7]
[7 7 7]]
c. Write a NumPy program to convert a list and tuple into arrays
Program:
import numpy as np

# List and tuple


my_list = [1, 2, 3, 4, 5]
my_tuple = (6, 7, 8, 9, 10)

# Convert list and tuple to numpy arrays


array_from_list = np.array(my_list)
array_from_tuple = np.array(my_tuple)

# Display the results


print("Array from List:", array_from_list)
print("Array from Tuple:", array_from_tuple)

output:
Array from List: [1 2 3 4 5]
Array from Tuple: [ 6 7 8 9 10]

d. Write a NumPy program to find the real and imaginary parts of an array of complex
numbers
Program:

import numpy as np

# Create an array of complex numbers


complex_array = np.array([3+4j, 1+2j, 5-6j, -2+3j])

# Extract real and imaginary parts


real_part = np.real(complex_array)
imaginary_part = np.imag(complex_array)

# Display the results


print("Original Complex Array:", complex_array)
print("Real Part:", real_part)
print("Imaginary Part:", imaginary_part)

output:
Original Complex Array: [ 3.+4.j 1.+2.j 5.-6.j -2.+3.j]
Real Part: [ 3. 1. 5. -2.]
Imaginary Part: [ 4. 2. -6. 3.]
15..

a. Write a NumPy program to convert an array to a float type


b. Write a NumPy program to add a border (filled with 0's) around an existing array
c. Write a NumPy program to convert a list and tuple into arrays
d. Write a NumPy program to append values to the end of an array
a. Convert an array to a float type
python
Copy code
import numpy as np

# Create an array of integers


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

# Convert the array to float type


float_array = int_array.astype(float)

# Display the result


print("Original Array:", int_array)

print("Array as Float:", float_array)

b. Add a border (filled with 0's) around an existing array


python
Copy code
import numpy as np

# Create a 2D array
original_array = np.array([[1, 2, 3], [4, 5, 6]])

# Add a border filled with 0's


array_with_border = np.pad(original_array, pad_width=1,
mode='constant', constant_values=0)

# Display the result


print("Original Array:")
print(original_array)
print("Array with Border:")
print(array_with_border)

c. Convert a list and tuple into arrays


python
Copy code
import numpy as np

# List and tuple


my_list = [1, 2, 3, 4, 5]
my_tuple = (6, 7, 8, 9, 10)

# Convert list and tuple to numpy arrays


array_from_list = np.array(my_list)
array_from_tuple = np.array(my_tuple)

# Display the results


print("Array from List:", array_from_list)
print("Array from Tuple:", array_from_tuple)

d. Append values to the end of an array


python
Copy code
import numpy as np

# Create an array
original_array = np.array([1, 2, 3, 4])
# Values to append
values_to_append = [5, 6, 7]

# Append values to the end of the array


new_array = np.append(original_array, values_to_append)

# Display the result


print("Original Array:", original_array)
print("Array after appending:", new_array)

output for all:


a . Convert an array to a float type
python
Copy code
Original Array: [1 2 3 4 5]
Array as Float: [1. 2. 3. 4. 5.]
b. Add a border (filled with 0's) around an existing array
python
Copy code
Original Array:
[[1 2 3]
[4 5 6]]
Array with Border:
[[0 0 0 0]
[0 1 2 3]
[0 4 5 6]
[0 0 0 0]]
c. Convert a list and tuple into arrays
python
Copy code
Array from List: [1 2 3 4 5]
Array from Tuple: [ 6 7 8 9 10]
d. Append values to the end of an array
python
Copy code
Original Array: [1 2 3 4]
Array after appending: [1 2 3 4 5 6 7]

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