0% found this document useful (0 votes)
3 views1 page

DMS2

The document contains two programming tasks: the first is an R function to generate a multiplication table for a given number, specifically for the number 5. The second task is a Python program that implements the k-means clustering algorithm on a synthetic dataset, visualizing the clusters and their centroids. Both tasks include code snippets demonstrating the required functionality.

Uploaded by

rambhosale0096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

DMS2

The document contains two programming tasks: the first is an R function to generate a multiplication table for a given number, specifically for the number 5. The second task is a Python program that implements the k-means clustering algorithm on a synthetic dataset, visualizing the clusters and their centroids. Both tasks include code snippets demonstrating the required functionality.

Uploaded by

rambhosale0096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Q1. Write an R program to calculate the multiplication table using a function.

[10 Marks]

table <- function(n) {


cat("Multiplication Table for", 5, ":\n")
for (i in 1:10) {
cat(n, "x", i, "=", n * i, "\n")
}
}

table(5)

Q2. Write a python program to implement k-means algorithms on a synthetic


dataset. [20 Marks]

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

# Generate synthetic dataset


n_samples = 300
n_clusters = 4
random_state = 42

X, y = make_blobs(n_samples=n_samples, centers=n_clusters, cluster_std=0.60,


random_state=random_state)

# Fit the K-means algorithm


kmeans = KMeans(n_clusters=n_clusters)
kmeans.fit(X)

# Get cluster centroids and labels


centroids = kmeans.cluster_centers_
labels = kmeans.labels_

# Visualize the results


plt.figure(figsize=(10, 6))
plt.scatter(X[:, 0], X[:, 1], c=labels, s=50, cmap='viridis')
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=200, alpha=0.75,
marker='X') # Centroids
plt.title('K-means Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.grid(True)
plt.show()

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