ML Lab
ML Lab
INDEX
3
Output: Star Pattern
5
n=5
for i in range(n):
# Print stars
# Print stars
print('* ' * i)
Output: Program to replace vowel from string
7
# Define vowels
vowels = "AEIOUaeiou"
return result
import pandas as pd
iris = load_iris()
print(iris_df.head())
Output: program to reverse a number
11
def reverse_number(number):
# Convert the number to a string, reverse it, and convert it back to an integer
reversed_num = int(str(number)[::-1])
return reversed_num
import numpy as np
import numpy as np
y = np.sin(x)
plt.figure(figsize=(8, 5))
plt.grid(True)
plt.legend()
plt.show()
Output: Draw a graph using matplotlib in 3D
17
# Create a 3D plot
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Add legend
ax.legend()
import turtle
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 5, 7, 11])
x_mean = np.mean(x)
y_mean = np.mean(y)
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.show()
Output: Split the iris dataset in the ratio of 7:3
23
iris = load_iris()
X = iris.data
y = iris.target
# Split the dataset into training (70%) and testing (30%) sets
iris = load_iris()
X = iris.data
# Features
# Create a K-means model with 3 clusters (as there are three classes in the Iris
dataset)
kmeans = KMeans(n_clusters=3, random_state=0)
# Fit the model to the data
kmeans.fit(X)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
# Data for the pie chart labels = ['A', 'B', 'C', 'D']
plt.figure(figsize=(8, 6))
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal') # Equal aspect ratio ensures that pie is
drawn as a circle.