Menu

[Solved]Utilizing Following Python Code Clustering Pysparkmlclustering Import Kmeans Pysparkmleval Q37170431

Utilizing the following Python Code for Clustering:

from pyspark.ml.clustering import KMeans
from pyspark.ml.evaluation import ClusteringEvaluator

# Loads data.
dataset =spark.read.format(“libsvm”).load(“/FileStore/tables/colon_cancer-ecfbf.txt”)

# Trains a k-means model.
kmeans = KMeans().setK(2).setSeed(1)
model = kmeans.fit(dataset)

# Make predictions
predictions = model.transform(dataset)

# Evaluate clustering by computing Silhouette score
evaluator = ClusteringEvaluator()

silhouette = evaluator.evaluate(predictions)
print(“Silhouette with squared euclidean distance = ” +str(silhouette))

# Shows the result.
centers = model.clusterCenters()
print(“Cluster Centers: “)
for center in centers:
print(center)

Create a for loop to add to the above code to make it run tenmore times to build different number of clusters: 2, 3, 4, 5, 6… 1You can change the number of clusters by changing the parametervalue that you pass to this function in the code: setK(2).

Expert Answer


Answer to Utilizing the following Python Code for Clustering: from pyspark.ml.clustering import KMeans from pyspark.ml.evaluation … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *