Showing posts with label files. Show all posts
Showing posts with label files. Show all posts

Wednesday, August 14, 2013

JSON in Scilab

Hi dears, today I've met a tool for Scilab that makes possible to handle JSON files into Scilab code. This tool is available through ATOMS or in http://forge.scilab.org/index.php/p/json/

Once installed, let's make a simple test with this tool just for validation.

First, it's created a json file (json_test.json) like below:

{
  "orderID": 12345,
  "softwareName": "Scilab",
  "ToolName": "JSon",
  "contents": [
    {
      "productID": 1,
      "productName": "Test1",
      "quantity": 1
    },
    {
      "productID": 2,
      "productName": "Test2",
      "quantity": 3
    }
  ],
  "demoCompleted": true
}


Now, it's needed to get this file into Scilab, what we can do uding mgetl(.) function:

json_test = mgetl("file_test.json");

Take attention for the path of the file.

And the function JSONParse(.) returns the struct that corresponds to the data in the file, like this:

mystruct = JSONParse(json_test)

The expected result is

-->mystruct
 mystruct  =

   orderID: 12345
   softwareName: "Scilab"
   ToolName: "JSon"
   contents: [1x2 struct]
   demoCompleted: "%T"


For accessing the fields, try this:

-->mystruct.orderID
 ans  =

    12345. 

-->mystruct.ToolName
 ans  =

 JSon  

-->mystruct.contents.productName
 ans  =


       ans(1)

 Test1  

       ans(2)

 Test2  



JSON is like XML pattern, much used for WebServices and in other Internet cases. Thus, I recommend to spend a few time taking a look about this technology and how Scilab can aggregate value to your solutions.

Thursday, April 4, 2013

Creation of artifical data for classification tests

In this semester, I'm teaching Artificial Intelligence discipline, and we are studying algorithms of classifying: Decision Trees and Neural Networks.

One important task of the discipline is to test the developed algorithm and estimate it accuracy. For that, I use to create artificial data which is controlled and simple to analyze.

The data consists of one table of N columns and many rows (let's use M rows). N - 1 first ones columns are of input data and the last column means the label (target), like presented below.

The variable x presented is a matrix (table) with 5 columns and 20 rows. Being 4 columns of input data and the last column a label for each row.

Label data are in a subset of natural numbers {1, 2, 3, 4, ....}, in the presented case {1, 2} where 1 means one class and 2 means the other.

N - 1 first columns are created through rand() function using M/P rows for each class of data, with it we created a equal distributed data set for classes representativeness (P means how many classes are in the data set).

For the variable x presented, it was created like following.

-->n = 10;

-->x = [[rand(n, 1); rand(n, 1) + 0.9] [1 + 2*rand(n, 1); rand(n, 1)*0.5 + 0.65] [rand(n, 1, "normal"); rand(n, 1) + 2.5] [rand(n, 1, "normal") - 2; rand(n, 1, "normal") + 2] [ones(n, 1); 2*ones(n, 1)]];

But it's possible to use only simpler forms of combined columns for creating overlapped input data.

Once created the matrix, we can write it to a file:

-->write("my_data.txt", x);


And later we can read the data again to a variable:

-->y = read("my_data.txt", -1, N);

Take a look at

http://usingscilab.blogspot.com.br/2009/03/using-files.html

http://usingscilab.blogspot.com.br/2009/08/basic-statistic.html

http://usingscilab.blogspot.com.br/2011/02/statistics-operators-mean-and-stdev.html

http://usingscilab.blogspot.com.br/search/label/matrix

for more details.


Thursday, March 19, 2009

Using files

Scilab has some functions for file manipulating.

Scilab can work with ASCII or binary files.

Why do we need to know how to use files?

If you want to share results, then give files of results is better than give scripts for generate them.

Okay, I think the most one knows what a file is.

Let's read the help (click the option like the picture).


Write "file manage" and select the first option: file.

This function is like the C function fopen().

The help contains some information over the function.

We can search for others functions, for example:

  • save;
  • load;
  • mopen;
  • mclose;
  • writeb;
  • readb.
But, I don't think these functions very necessary now, maybe in an other situation.

The most important functions are:

  • read;
  • write.
We create files using write and load files using read.

The functions read and write are used principally with matrices and vectors.

Look the examples:

-->x = rand(5,5)
x =

0.2113249 0.6283918 0.5608486 0.2320748 0.3076091
0.7560439 0.8497452 0.6623569 0.2312237 0.9329616
0.0002211 0.6857310 0.7263507 0.2164633 0.2146008
0.3303271 0.8782165 0.1985144 0.8833888 0.312642
0.6653811 0.0683740 0.5442573 0.6525135 0.3616361

-->write("test_data.dat", x);

-->y1 = read("test_data.dat", 1, 2) // 1 line and 2 columns
y1 =

0.2113249 0.6283918

-->y2 = read("test_data.dat", 2, 2) // 2 lines and 2 columns
y2 =

0.2113249 0.6283918
0.7560439 0.8497452

-->y3 = read("test_data.dat", -1, 1) // -1 indicates that "all rows"
y3 =

0.2113249
0.7560439
0.0002211
0.3303271
0.6653811

-->>y4 = read("test_data.dat", -1, 5) // it reads the full file
y4 =

0.2113249 0.6283918 0.5608486 0.2320748 0.3076091
0.7560439 0.8497452 0.6623569 0.2312237 0.9329616
0.0002211 0.6857310 0.7263507 0.2164633 0.2146008
0.3303271 0.8782165 0.1985144 0.8833888 0.312642
0.6653811 0.0683740 0.5442573 0.6525135 0.3616361


The file "test_data.dat" (click on the picture for see the real size):