import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans from sklearn.metrics import pairwise_distances_argmin from skimage.io import imread from sklearn.utils import shuffle from skimage import img_as_float from time import time
pepper = imread("../images/pepper.jpg")
# Display the original image plt.figure(1), plt.clf() ax = plt.axes([0, 0, 1, 1]) plt.axis('off'), plt.title('Original image (%d colors)' %(len(np.unique(pepper)))), plt.imshow(pepper)
输入的辣椒原始图像如图1所示。
现在,应用k均值聚类算法对图像进行分割,如下面的代码所示:
n_colors = 64
# Convert to floats instead of the default 8 bits integer coding. Dividingby # 255 is important so that plt.imshow behaves works well on float data # (need tobe in the range [0-1]) pepper = np.array(pepper, dtype=np.float64) / 255
# Load Image and transform to a 2D numpy array. w, h, d = original_shape = tuple(pepper.shape) assert d == 3 image_array = np.reshape(pepper, (w * h, d))
defrecreate_image(codebook, labels, w, h): """Recreate the (compressed) image from the code book & labels""" d = codebook.shape[1] image = np.zeros((w, h, d)) label_idx = 0 for i in range(w): for j in range(h): image[i][j] = codebook[labels[label_idx]] label_idx += 1 return image
plt.figure(2, figsize=(10,10)) plt.clf() i = 1 for k in [64, 32, 16, 4]: t0 = time() plt.subplot(2,2,i) plt.axis('off') image_array_sample = shuffle(image_array, random_state=0)[:1000] kmeans = KMeans(n_clusters=k, random_state=0).fit(image_array_sample) print("done in %0.3fs." % (time() - t0)) # Get labels for all points
print("Predicting color indices on the full image (k-means)") t0 = time() labels = kmeans.predict(image_array) print("done in %0.3fs." % (time() - t0)) plt.title('Quantized image (' + str(k) + ' colors, K-Means)') plt.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h)) i += 1 plt.show() plt.figure(3, figsize=(10,10)) plt.clf() i = 1 for k in [64, 32, 16, 4]: t0 = time() plt.subplot(2,2,i) plt.axis('off') codebook_random = shuffle(image_array, random_state=0)[:k + 1] print("Predicting color indices on the full image (random)") t0 = time() labels_random = pairwise_distances_argmin(codebook_random,image_array,axis=0)
import numpy as np import matplotlib.pylab as plt from sklearn.datasets import load_digits from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.pipeline import Pipeline
digits = load_digits() #print(digits.keys())
print(digits.data.shape) j = 1 np.random.seed(1) fig = plt.figure(figsize=(3,3)) fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05,wspace=0.05) for i in np.random.choice(digits.data.shape[0], 25): plt.subplot(5,5,j), plt.imshow(np.reshape(digits.data[i,:], (8,8)),cmap='binary'), plt.axis('off') j += 1 plt.show()
from sklearn.datasets import fetch_olivetti_faces faces = fetch_olivetti_faces().data print(faces.shape) # there are 400 faces each of them is of 64x64=4096 pixels fig = plt.figure(figsize=(5,5)) fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05) # plot 25 random faces j = 1 np.random.seed(0) for i in np.random.choice(range(faces.shape[0]), 25): ax = fig.add_subplot(5, 5, j, xticks=[], yticks=[]) ax.imshow(np.reshape(faces[i,:],(64,64)), cmap=plt.cm.bone,interpolation='nearest') j += 1 plt.show()
%matplotlib inline import gzip, os, sys import numpy as np from scipy.stats import multivariate_normal from urllib.request import urlretrieve import matplotlib.pyplot as pylab
01 下载MNIST(手写数字)数据集
从下载MNIST数据集开始。如下代码展示了如何下载训练数据集和测试数据集:
# Function that downloads a specified MNIST data file from Yann Le Cun's website defdownload(filename, source='http://yann.lecun.com/exdb/mnist/'): print("Downloading %s" % filename) urlretrieve(source + filename, filename)
# Invokes download() if necessary, then reads in images defload_mnist_images(filename): ifnot os.path.exists(filename): download(filename) with gzip.open(filename, 'rb') as f: data = np.frombuffer(f.read(), np.uint8, offset=16) data = data.reshape(-1,784) return data defload_mnist_labels(filename): ifnot os.path.exists(filename): download(filename) with gzip.open(filename, 'rb') as f: data = np.frombuffer(f.read(), np.uint8, offset=8) return data
## Load the training set train_data = load_mnist_images('train-images-idx3-ubyte.gz') train_labels = load_mnist_labels('train-labels-idx1-ubyte.gz') ## Load the testing set test_data = load_mnist_images('t10k-images-idx3-ubyte.gz') test_labels = load_mnist_labels('t10k-labels-idx1-ubyte.gz')
import time from sklearn.neighbors import BallTree
## Build nearest neighbor structure on training data t_before = time.time() ball_tree = BallTree(train_data) t_after = time.time()
## Compute training time t_training = t_after - t_before print("Time to build data structure (seconds): ", t_training)
## Get nearest neighbor predictions on testing data t_before = time.time() test_neighbors = np.squeeze(ball_tree.query(test_data, k=1,return_distance=False)) test_predictions = train_labels[test_neighbors] t_after = time.time()
## Compute testing time t_testing = t_after - t_before print("Time to classify test set (seconds): ", t_testing) # Time to build data structure (seconds): 20.65474772453308 # Time to classify test set (seconds): 532.3929145336151
deffit_generative_model(x,y): k = 10# labels 0,1,...,k-1 d = (x.shape)[1] # number of features mu = np.zeros((k,d)) sigma = np.zeros((k,d,d)) pi = np.zeros(k) c = 3500#10000 #1000 #100 #10 #0.1 #1e9 for label in range(k): indices = (y == label) pi[label] = sum(indices) / float(len(y)) mu[label] = np.mean(x[indices,:], axis=0) sigma[label] = np.cov(x[indices,:], rowvar=0, bias=1) + c*np.eye(d) return mu, sigma, pi
mu, sigma, pi = fit_generative_model(train_data, train_labels) display_char(mu[0]) display_char(mu[1]) display_char(mu[2])
# Compute log Pr(label|image) for each [test image,label] pair. k = 10 score = np.zeros((len(test_labels),k)) for label in range(0,k): rv = multivariate_normal(mean=mu[label], cov=sigma[label]) for i in range(0,len(test_labels)): score[i,label] = np.log(pi[label]) + rv.logpdf(test_data[i,:]) test_predictions = np.argmax(score, axis=1) # Finally, tally up score errors = np.sum(test_predictions != test_labels) print("The generative model makes " + str(errors) + " errors out of 10000") # The generative model makes 438 errors out of 10000 t_accuracy = sum(test_predictions == test_labels) / float(len(test_labels) t_accuracy # 0.95620000000000005