<jupyter_start><jupyter_text>Image Compression by K-means clusteringIn this exercise, you have to perform an image compression of the given picture of New York (New York.jpeg).To do so, we want to keep not all the possible colors, but only 4.Thus, you have to perform a clustering algorithm, with K=4, and show the image with those only 4 colors.```🔦 Hint:: An image is composed of pixels. Each pixel is made of 3 channels: Red, Green, Blue (RGB). Those color levels are encoded on 8 bits, meaning they are in the range [0-255]. For example, a pixel with this RGB color : (0, 0, 255) will be a very strong blue pixel.```<jupyter_code>%matplotlib inline
# TODO: Load and review the image
import matplotlib.pyplot as plt
image = plt.imread('goldcoast.jpeg')
plt.imshow(image)
plt.show()
# TODO: Reshape the image for k-means clustering
image_reshape = image.reshape(50325, 3)
print("image: {}".format(image.shape))
print("image reshape: {}".format(image_reshape.shape))
# TODO: Run k-means clustering on reshaped image

modele_km=...
modele_km.fit(image_reshape)
# TODO: Print cluster centers and construct compressed image
palette = modele_km...
compressed_image = palette[...]
print(palette)
print("compressed image: {}".format(compressed_image.shape))
# TODO: Show the compressed image
compressed_image = compressed_image.reshape(..., ..., ...)
plt.imshow(compressed_image/255.)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1119718309859155, 'fraction_numerical': 0.01619718309859155, 'mean_word_length': 5.125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '376992', 'license': 'MIT', 'n_tokens_mistral': 474, 'n_tokens_neox': 429, 'n_words': 170, 'path': 'Lab 3/image_compression.ipynb', 'repo_name': 'zacjensen/3803ICT-Repo', 'size': '1420'}