ML IU48prac1,2
ML IU48prac1,2
AIM: Study of Python Basic Libraries like a Numpy. Perform the following task
using Numpy library.
Code 1 : Creating blank array, with predefined data, with pattern specific data
import numpy as np
a = np.array([])
b = np.array([1, 2, 3, 4])
c = np.arange(0, 11, 2)
print("Blank Array: ", a)
print("Predefined Array: ", b)
print("Array containing even numbers: ", c)
Output 1:
Blank Array: []
Predefined Array: [1 2 3 4]
Array containing even numbers: [ 0 2 4 6 8 10]
Output 2:
Original Array: [1 2 3 4 5]
Spliced Array: [1 2 3 4]
Updated Array: [0 0 3 4 5]
IU21412300048 7CSE-A1 1
Machine Learning(CS0701)
d = np.transpose(b)
print("Transpose: ", d)
Output 3:
Original Array: [1 2 3 4]
Reshaped Array: [[1 2]
[3 4]]
Flattened Array: [1 2 3 4]
Transpose: [[1 3]
[2 4]]
Output 4 :
1
2
3
4
Output 5 :
[1. 2. 3. 4. 5. 6.]
IU21412300048 7CSE-A1 2
Practical - 2
AIM: Study of Python Libraries for ML applications such as Pandas and Matplotlib
Perform the following task using Pandas & Matplotlib library.
Pandas
Code 1 : Creating data frame
import pandas as pd
def create_dataframe():
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [24, 27, 22, 32, 29],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
}
df = pd.DataFrame(data)
print("DataFrame:\n", df)
return df
df = create_dataframe()
Output 1:
DataFrame:
Name Age City
0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago
3 David 32 Houston
4 Eve 29 Phoenix
def read_file(file_path):
df = pd.read_csv(file_path)
print("\nDataFrame from file:\n", df)
file_path = '/content/industry.csv'
read_file(file_path)
Output 2:
IU21412300048 7CSE-A1 3
Industry
0 Accounting/Finance
1 Advertising/Public Relations
2 Aerospace/Aviation
3 Arts/Entertainment/Publishing
4 Automotive
5 Banking/Mortgage
6 Business Development
7 Business Opportunity
8 Clerical/Administrative
9 Construction/Facilities
10 Consumer Goods
11 Customer Service
12 Education/Training
13 Energy/Utilities
14 Engineering
15 Government/Military
16 Green
17 Healthcare
18 Hospitality/Travel
19 Human Resources
df = pd.DataFrame(data)
sliced_df = df.iloc[0:3]
print("\nSliced DataFrame (rows 0 to 2):\n", sliced_df)
Output 3:
Sliced DataFrame (rows 0 to 2):
Name Age City
0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago
IU21412300048 7CSE-A1 4
Machine Learning(CS0701)
read_and_print_csv(file_path)
Output 4 :
DataFrame from file:
Name Age City
0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago
3 David 32 Houston
4 Eve 29 Phoenix
Output 5 :
DataFrame after manipulations:
Name Age City Age in 5 Years
0 Alice 24 NEW YORK 29.0
1 Bob 27 LOS ANGELES 32.0
2 Charlie 22 CHICAGO 27.0
3 David 32 HOUSTON 37.0
4 Eve 29 PHOENIX 34.0
Code 6 : Use pandas for masking data and reading in Boolean format.
import pandas as pd
data = {
df = pd.DataFrame(data)
IU21412300048 7CSE-A1 5
Machine Learning(CS0701)
mask = df['Age'] > 25
masked_df = df[mask]
3 True
4 True
Name: Age, dtype: bool
Filtered DataFrame (Age > 25):
Name Age City
1 Bob27 Los Angeles
3 David32 Houston
4 Eve29 Phoenix
Matplotlib
Code 1 : Importing
matplotlib
Simple line chart
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1,3,5,7])
y = x*2
plt.plot(x, y)
plt.show()
Output 1:
IU21412300048 7CSE-A1 6
Machine Learning(CS0701)
Output 2:
Code 3: Histogram
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randint(20,150,60)
plt.hist(x)
plt.show()
Output 3:
IU21412300048 7CSE-A1 7
Machine Learning(CS0701)
Output 4 :
Output 5 :
IU21412300048 7CSE-A1 8