Permalink
Browse files

new week of material committed, not solved

  • Loading branch information...
schneems committed Dec 12, 2011
1 parent fe15dc0 commit 2f52a1715eb12bc5076c4536ef7177662836ab41
Binary file not shown.
@@ -25,7 +25,28 @@
+values = [0.01 0.03 0.1 0.3 1 3 10 30];
+error_min = inf;
+
+
+fprintf('chill hommie i am looking for C and sigma, yo values\n');
+for C = values
+ for sigma = values
+ fprintf('.');
+ model = svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));
+ err = mean(double(svmPredict(model, Xval) ~= yval));
+ if( err <= error_min )
+ C_final = C;
+ sigma_final = sigma;
+ error_min = err;
+ fprintf('new min found C, sigma = %f, %f with error = %f', C_final, sigma_final, error_min)
+ end
+ end
+end
+C = C_final;
+sigma = sigma_final;
+fprintf('Best value C, sigma = [%f %f] with prediction error = %f\n', C, sigma, error_min);
@@ -49,10 +49,9 @@
%
-
-
-
-
+for i = word_indices
+ x(i) = 1;
+end
% =========================================================================
@@ -72,6 +72,9 @@
fprintf('Program paused. Press enter to continue.\n');
pause;
+
+
+
%% =============== Part 4: Visualizing Dataset 2 ================
% The following code will load the next dataset into your environment and
% plot the data.
@@ -16,9 +16,8 @@
%
%
-
-
-
+magnitude = sum((x1-x2).^2);
+sim = e^(-magnitude/(2*sigma^2));
% =============================================================
Binary file not shown.
@@ -97,25 +97,24 @@
% str2). It will return 1 only if the two strings are equivalent.
%
-
-
-
-
-
-
-
-
+ for i = 1:length(vocabList)
+ if(strcmp(str, vocabList{i}))
+ word_indices = [ word_indices ; i];
+ end
+ end
% =============================================================
- % Print to screen, ensuring that the output lines are not too long
+ % Print out to the screen, make sure the output lines are not crazy long
if (l + length(str) + 1) > 78
fprintf('\n');
l = 0;
end
fprintf('%s ', str);
- l = l + length(str) + 1;
+
+ % =============================================================
+
end
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,40 @@
+function centroids = computeCentroids(X, idx, K)
+%COMPUTECENTROIDS returs the new centroids by computing the means of the
+%data points assigned to each centroid.
+% centroids = COMPUTECENTROIDS(X, idx, K) returns the new centroids by
+% computing the means of the data points assigned to each centroid. It is
+% given a dataset X where each row is a single data point, a vector
+% idx of centroid assignments (i.e. each entry in range [1..K]) for each
+% example, and K, the number of centroids. You should return a matrix
+% centroids, where each row of centroids is the mean of the data points
+% assigned to it.
+%
+
+% Useful variables
+[m n] = size(X);
+
+% You need to return the following variables correctly.
+centroids = zeros(K, n);
+
+
+% ====================== YOUR CODE HERE ======================
+% Instructions: Go over every centroid and compute mean of all points that
+% belong to it. Concretely, the row vector centroids(i, :)
+% should contain the mean of the data points assigned to
+% centroid i.
+%
+% Note: You can use a for-loop over the centroids to compute this.
+%
+
+
+
+
+
+
+
+
+% =============================================================
+
+
+end
+
@@ -0,0 +1,59 @@
+function [h, display_array] = displayData(X, example_width)
+%DISPLAYDATA Display 2D data in a nice grid
+% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
+% stored in X in a nice grid. It returns the figure handle h and the
+% displayed array if requested.
+
+% Set example_width automatically if not passed in
+if ~exist('example_width', 'var') || isempty(example_width)
+ example_width = round(sqrt(size(X, 2)));
+end
+
+% Gray Image
+colormap(gray);
+
+% Compute rows, cols
+[m n] = size(X);
+example_height = (n / example_width);
+
+% Compute number of items to display
+display_rows = floor(sqrt(m));
+display_cols = ceil(m / display_rows);
+
+% Between images padding
+pad = 1;
+
+% Setup blank display
+display_array = - ones(pad + display_rows * (example_height + pad), ...
+ pad + display_cols * (example_width + pad));
+
+% Copy each example into a patch on the display array
+curr_ex = 1;
+for j = 1:display_rows
+ for i = 1:display_cols
+ if curr_ex > m,
+ break;
+ end
+ % Copy the patch
+
+ % Get the max value of the patch
+ max_val = max(abs(X(curr_ex, :)));
+ display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
+ pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
+ reshape(X(curr_ex, :), example_height, example_width) / max_val;
+ curr_ex = curr_ex + 1;
+ end
+ if curr_ex > m,
+ break;
+ end
+end
+
+% Display Image
+h = imagesc(display_array, [-1 1]);
+
+% Do not show axis
+axis image off
+
+drawnow;
+
+end
@@ -0,0 +1,8 @@
+function drawLine(p1, p2, varargin)
+%DRAWLINE Draws a line from point p1 to point p2
+% DRAWLINE(p1, p2) Draws a line from point p1 to point p2 and holds the
+% current figure
+
+plot([p1(1) p2(1)], [p1(2) p2(2)], varargin{:});
+
+end
@@ -0,0 +1,174 @@
+%% Machine Learning Online Class
+% Exercise 7 | Principle Component Analysis and K-Means Clustering
+%
+% Instructions
+% ------------
+%
+% This file contains code that helps you get started on the
+% exercise. You will need to complete the following functions:
+%
+% pca.m
+% projectData.m
+% recoverData.m
+% computeCentroids.m
+% findClosestCentroids.m
+% kMeansInitCentroids.m
+%
+% For this exercise, you will not need to change any code in this file,
+% or any other files other than those mentioned above.
+%
+
+%% Initialization
+clear ; close all; clc
+
+%% ================= Part 1: Find Closest Centroids ====================
+% To help you implement K-Means, we have divided the learning algorithm
+% into two functions -- findClosestCentroids and computeCentroids. In this
+% part, you shoudl complete the code in the findClosestCentroids function.
+%
+fprintf('Finding closest centroids.\n\n');
+
+% Load an example dataset that we will be using
+load('ex7data2.mat');
+
+% Select an initial set of centroids
+K = 3; % 3 Centroids
+initial_centroids = [3 3; 6 2; 8 5];
+
+% Find the closest centroids for the examples using the
+% initial_centroids
+idx = findClosestCentroids(X, initial_centroids);
+
+fprintf('Closest centroids for the first 3 examples: \n')
+fprintf(' %d', idx(1:3));
+fprintf('\n(the closest centroids should be 1, 3, 2 respectively)\n');
+
+fprintf('Program paused. Press enter to continue.\n');
+pause;
+
+%% ===================== Part 2: Compute Means =========================
+% After implementing the closest centroids function, you should now
+% complete the computeCentroids function.
+%
+fprintf('\nComputing centroids means.\n\n');
+
+% Compute means based on the closest centroids found in the previous part.
+centroids = computeCentroids(X, idx, K);
+
+fprintf('Centroids computed after initial finding of closest centroids: \n')
+fprintf(' %f %f \n' , centroids');
+fprintf('\n(the centroids should be\n');
+fprintf(' [ 2.428301 3.157924 ]\n');
+fprintf(' [ 5.813503 2.633656 ]\n');
+fprintf(' [ 7.119387 3.616684 ]\n\n');
+
+fprintf('Program paused. Press enter to continue.\n');
+pause;
+
+
+%% =================== Part 3: K-Means Clustering ======================
+% After you have completed the two functions computeCentroids and
+% findClosestCentroids, you have all the necessary pieces to run the
+% kMeans algorithm. In this part, you will run the K-Means algorithm on
+% the example dataset we have provided.
+%
+fprintf('\nRunning K-Means clustering on example dataset.\n\n');
+
+% Load an example dataset
+load('ex7data2.mat');
+
+% Settings for running K-Means
+K = 3;
+max_iters = 10;
+
+% For consistency, here we set centroids to specific values
+% but in practice you want to generate them automatically, such as by
+% settings them to be random examples (as can be seen in
+% kMeansInitCentroids).
+initial_centroids = [3 3; 6 2; 8 5];
+
+% Run K-Means algorithm. The 'true' at the end tells our function to plot
+% the progress of K-Means
+[centroids, idx] = runkMeans(X, initial_centroids, max_iters, true);
+fprintf('\nK-Means Done.\n\n');
+
+fprintf('Program paused. Press enter to continue.\n');
+pause;
+
+%% ============= Part 4: K-Means Clustering on Pixels ===============
+% In this exercise, you will use K-Means to compress an image. To do this,
+% you will first run K-Means on the colors of the pixels in the image and
+% then you will map each pixel on to it's closest centroid.
+%
+% You should now complete the code in kMeansInitCentroids.m
+%
+
+fprintf('\nRunning K-Means clustering on pixels from an image.\n\n');
+
+% Load an image of a bird
+A = double(imread('bird_small.png'));
+
+% If imread does not work for you, you can try instead
+% load ('bird_small.mat');
+
+A = A / 255; % Divide by 255 so that all values are in the range 0 - 1
+
+% Size of the image
+img_size = size(A);
+
+% Reshape the image into an Nx3 matrix where N = number of pixels.
+% Each row will contain the Red, Green and Blue pixel values
+% This gives us our dataset matrix X that we will use K-Means on.
+X = reshape(A, img_size(1) * img_size(2), 3);
+
+% Run your K-Means algorithm on this data
+% You should try different values of K and max_iters here
+K = 16;
+max_iters = 10;
+
+% When using K-Means, it is important the initialize the centroids
+% randomly.
+% You should complete the code in kMeansInitCentroids.m before proceeding
+initial_centroids = kMeansInitCentroids(X, K);
+
+% Run K-Means
+[centroids, idx] = runkMeans(X, initial_centroids, max_iters);
+
+fprintf('Program paused. Press enter to continue.\n');
+pause;
+
+
+%% ================= Part 5: Image Compression ======================
+% In this part of the exercise, you will use the clusters of K-Means to
+% compress an image. To do this, we first find the closest clusters for
+% each example. After that, we
+
+fprintf('\nApplying K-Means to compress an image.\n\n');
+
+% Find closest cluster members
+idx = findClosestCentroids(X, centroids);
+
+% Essentially, now we have represented the image X as in terms of the
+% indices in idx.
+
+% We can now recover the image from the indices (idx) by mapping each pixel
+% (specified by it's index in idx) to the centroid value
+X_recovered = centroids(idx,:);
+
+% Reshape the recovered image into proper dimensions
+X_recovered = reshape(X_recovered, img_size(1), img_size(2), 3);
+
+% Display the original image
+subplot(1, 2, 1);
+imagesc(A);
+title('Original');
+
+% Display compressed image side by side
+subplot(1, 2, 2);
+imagesc(X_recovered)
+title(sprintf('Compressed, with %d colors.', K));
+
+
+fprintf('Program paused. Press enter to continue.\n');
+pause;
+
Oops, something went wrong.

0 comments on commit 2f52a17

Please sign in to comment.