Showing posts with label Clustering. Show all posts
Showing posts with label Clustering. Show all posts

Wednesday, 14 May 2014

Counting words - Clustering–Find Related post–part2

 

 

Key point

Euclidean distance comes under the unsupervised learning.

Why Euclidean distance?

Euclidean distance measures the distance between the two points in xy plane. formula Euclidean distance formula and its explanation can be seen in the below link. the formula will match exactly with our dist_raw(v1, v2) function.

http://www.cut-the-knot.org/pythagoras/DistanceFormula.shtml

Monday, 12 May 2014

Regex pattern in token_pattern for CountVectorizer

Regex

The result of the book showed

#samples: 5, #features: 25

[u'about', u'actually', u'capabilities', u'contains', u'data', u'databases', u'images', u'imaging', u'interesting', u'is', u'it', u'learning', u'machine', u'most', u'much', u'not', u'permanently', u'post', u'provide', u'safe', u'storage', u'store', u'stuff', u'this', u'toy']

There is one missing word in the above result that is “a”, which is there in the 01.txt of the toy posts.

01.txt This is a toy post about machine learning. Actually, it contains
not much interesting stuff.

 

Why it is missed?

This is caused by the default token_pattern for CountVectorizer, which removes tokens of a single character:

Default token_pattern for CountVectorizer is (?u)\b\w\w+\b, which removed single character word ‘a’.

 

How to get the single character word?

by changing token_pattern for CountVectorizer to \\b\\w+\\b we can see the single character in the result.

 

Do we need the single character word?

Depend on the apllication, we may need to extract single character words.

Ref

http://stackoverflow.com/questions/20717641/countvectorizer-i-not-showing-up-in-vectorized-text

Tuesday, 22 April 2014

Bag-Of-word using orange

for the main work of finding related post, we have to learn bag-of-words approach for text processing in machine learning.

in a default installation we do not have text processing widgets in orange, so we have to install addons. I installed Textable addon for this work.

1. From the Textable addon, add Text Field widget.

2. Copy & Paste the following text with in quotes

“How to format my hard disk

Hard disk format problems”

2014-04-22_15-11-00

3. Add Textable\preprocessor to convert the text to smaller case. we are converting entire text smaller case. so that words of upper and lower case wont be separated as a different words. that is How and how wont be separated as a different word.2014-04-22_15-17-18

4. First we will segment the words in to lines. for this we will ad Textable\segment widget and configure it as shown in the below image.

2014-04-22_15-38-06

Configuration of Textable\segment widget

  • Check advanced settings
  • Select mode to split
  • Set Regex .+, to specify lines.
  • Check Unicode dependent(u)
  • Click add
  • under options, Change the label name
  • Check the Auto-number with key and name it

5. We will segment it further to words. for this we will ad Textable\segment widget and configure it as shown in the below image.

2014-04-22_16-27-52

Configuration of Textable\segment widget

  • Check advanced settings
  • Select mode to Tokenise
  • Set Regex \w+, to specify words.
  • Click add button
  • under options, change the segment label.

6. Count the segments based on the context and words. for this we will add Textable\count widget

2014-04-22_16-35-45

    • set segmentation to words
      • mode to containing segmentation
      • segmentation to Lines
      • annotation key to num

7. Insert Texatable\Convert widget and connect to data/ table widget.

See the complete canvas

2014-04-23_09-10-03

 

Special Thanks

Special thanks to Mr.Axanthos, Site Admin, Langtech.ch. I am new to Machine learning. I don’t know how to implement bag-of-word approach in orange and concept. I have made a support request in Langtech.ch/forums. for my request Mr. axanthos provided a clear explanation and code.

http://langtech.ch/forum/textable/viewtopic.php?f=4&t=12

 

Reference

http://langtech.ch/forum/textable/viewtopic.php?f=4&t=4

http://moodle2.unil.ch/course/view.php?id=574

https://orange-textable.readthedocs.org/en/latest/

http://orange.biolab.si/forum/viewtopic.php?f=4&t=1949#p5655

Friday, 18 April 2014

Counting words - Clustering–Find Related post–part1

assume in our website we already have few posts. we are going to find a most relevant post for the current viewing post “Imaging database”

the Below table shows existing posts in the websites

Si.No

Post Content
1

This is a toy post about machine learning. Actually, it contains
not much interesting stuff.

2 Imaging databases can get huge.
3 Most imaging databases safe images permanently.
4 Imaging databases store images.
5

Imaging databases store images. Imaging databases store
images. Imaging databases store images.

In the book, posts are opened using following script

Python_Script_2014-04-30_15-17-52

Script1 – reading file and printing output

   1: import os
   2: import sys
   3: import scipy as sp
   4: from sklearn.feature_extraction.text import CountVectorizer
   5: data_Dir = "E:\\Machine Learning\\Orange\\Finding Related Post\\toy\\"
   6: posts = [open(os.path.join(data_Dir,f)).read() for f in os.listdir(data_Dir)]
   7: print posts

Result1:


"Running script:


['This is a toy post about machine learning. Actually, it contains not much interesting stuff.', 'Imaging databases provide storage capabilities.', 'Most imaging databases safe images permanently.', 'Imaging databases store data.', 'Imaging databases store data. Imaging databases store data. Imaging databases store data.']

 

Implementation of script1 in orange


we will modify the Bag of words file.

2014-04-30_15-34-00

Text_Files_2014-04-30_15-35-36

1. Replace the Textable\TextFeild widget to Textable\TextFile widget

2. Click advanced settings,

3. Click browse button to open the file

4. select all the files and click open.

5. you can see the result by connecting, Textable\Disply to Textable\Lowercase widget.

 

 

Learning from this post


- opening the directory path and file in python


Reference


http://langtech.ch/forum/textable/viewtopic.php?f=4&t=4


http://moodle2.unil.ch/course/view.php?id=574


https://orange-textable.readthedocs.org/en/latest/


http://orange.biolab.si/forum/viewtopic.php?f=4&t=1949#p5655


how to work with directories http://www.diveintopython.net/file_handling/os_module.html