import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Generate two sets of random data points
X = -2 * np.random.rand(100, 2)
X1 = 1 + 2 * np.random.rand(50, 2)
X[50:100, :] = X1

# Plot the random points
plt.scatter(X[:, 0], X[:, 1], s=50, c='b')
plt.show()

# Apply KMeans clustering with 2 clusters
kmean = KMeans(n_clusters=2)
kmean.fit(X)

# Display the cluster centers
print("Cluster Centers:")
print(kmean.cluster_centers_)

# Plot points and cluster centers
plt.scatter(X[:, 0], X[:, 1], s=50, c='b')
plt.scatter(kmean.cluster_centers_[0, 0], kmean.cluster_centers_[0, 1], s=200, c='g', marker='s')
plt.scatter(kmean.cluster_centers_[1, 0], kmean.cluster_centers_[1, 1], s=200, c='r', marker='s')
plt.show()

# Create a sample point and reshape it for prediction
sample_test = np.array([-3.0, -3.0])
second_test = sample_test.reshape(1, -1)

# Predict the cluster for the sample point
prediction = kmean.predict(second_test)
print("Prediction for sample point (-3.0, -3.0): Cluster", prediction[0])