## Friday, July 20, 2012

### Distribution fitting with scipy

Distribution fitting is the procedure of selecting a statistical distribution that best fits to a dataset generated by some random process. In this post we will see how to fit a distribution using the techniques implemented in the Scipy library.
This is the first snippet:
```from scipy.stats import norm
from numpy import linspace
from pylab import plot,show,hist,figure,title

# picking 150 of from a normal distrubution
# with mean 0 and standard deviation 1
samp = norm.rvs(loc=0,scale=1,size=150)

param = norm.fit(samp) # distribution fitting

# now, param and param are the mean and
# the standard deviation of the fitted distribution
x = linspace(-5,5,100)
# fitted distribution
pdf_fitted = norm.pdf(x,loc=param,scale=param)
# original distribution
pdf = norm.pdf(x)

title('Normal distribution')
plot(x,pdf_fitted,'r-',x,pdf,'b-')
hist(samp,normed=1,alpha=.3)
show()
```
The result should be as follows

In the code above a dataset of 150 samples have been created using a normal distribution with mean 0 and standar deviation 1, then a fitting procedure have been applied on the data. In the figure we can see the original distribution (blue curve) and the fitted distribution (red curve) and we can observe that they are really similar.
Let's do the same with a Rayleigh distribution:
```from scipy.stats import norm,rayleigh

samp = rayleigh.rvs(loc=5,scale=2,size=150) # samples generation

param = rayleigh.fit(samp) # distribution fitting

x = linspace(5,13,100)
# fitted distribution
pdf_fitted = rayleigh.pdf(x,loc=param,scale=param)
# original distribution
pdf = rayleigh.pdf(x,loc=5,scale=2)

title('Rayleigh distribution')
plot(x,pdf_fitted,'r-',x,pdf,'b-')
hist(samp,normed=1,alpha=.3)
show()
```
The resulting plot:

As expected, the two distributions are very close.

## Sunday, July 8, 2012

### Color quantization

The aim of color clustering is to produce a small set of representative colors which captures the color properties of an image. Using the small set of color found by the clustering, a quantization process can be applied to the image to find a new version of the image that has been "simplified," both in colors and shapes.
In this post we will see how to use the K-Means algorithm to perform color clustering and how to apply the quantization. Let's see the code:
```from pylab import imread,imshow,figure,show,subplot
from numpy import reshape,uint8,flipud
from scipy.cluster.vq import kmeans,vq

# reshaping the pixels matrix
pixel = reshape(img,(img.shape*img.shape,3))

# performing the clustering
centroids,_ = kmeans(pixel,6) # six colors will be found
# quantization
qnt,_ = vq(pixel,centroids)

# reshaping the result of the quantization
centers_idx = reshape(qnt,(img.shape,img.shape))
clustered = centroids[centers_idx]

figure(1)
subplot(211)
imshow(flipud(img))
subplot(212)
imshow(flipud(clustered))
show()
```
The result shoud be as follows:

We have the original image on the top and the quantized version on the bottom. We can see that the image on the bottom has only six colors. Now, we can plot the colors found with the clustering in the RGB space with the following code:
```# visualizing the centroids into the RGB space
from mpl_toolkits.mplot3d import Axes3D
fig = figure(2)
ax = fig.gca(projection='3d')
ax.scatter(centroids[:,0],centroids[:,1],centroids[:,2],c=centroids/255.,s=100)

show()
```
And this is the result:
{"ft_lang_label":"__label__en","ft_lang_prob":0.7264995,"math_prob":0.9780402,"size":1536,"snap":"2019-35-2019-39","text_gpt3_token_len":403,"char_repetition_ratio":0.17689295,"word_repetition_ratio":0.020725388,"special_character_ratio":0.26302084,"punctuation_ratio":0.170347,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9984862,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-20T01:07:59Z\",\"WARC-Record-ID\":\"<urn:uuid:460a4eee-349b-4daf-9fc3-2123424ca55b>\",\"Content-Length\":\"92294\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ce4cf29b-3800-4389-85cc-b7083f2b99fe>\",\"WARC-Concurrent-To\":\"<urn:uuid:a9d5bd26-40be-42a4-8eee-d86cb5497ba0>\",\"WARC-IP-Address\":\"172.217.8.1\",\"WARC-Target-URI\":\"https://glowingpython.blogspot.com/2012/07/\",\"WARC-Payload-Digest\":\"sha1:Y62V6LKJAOSZQKXUMYMWFIDKHT4F6OHN\",\"WARC-Block-Digest\":\"sha1:LNRXWOTOWPXRBHOI3Y42GRJPKRVST4TP\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514573801.14_warc_CC-MAIN-20190920005656-20190920031656-00259.warc.gz\"}"}