Saving a decision tree model for later application in Julia - export

I have trained a pruned decision tree model in Julia using the DecisionTree module. I now want to save this model for use on other data sets later.
I have tried converting the model to a data array for export using writetable() and I have tried exporting using writedlm() and neither of these work. When I look at the type of the model I see that it is a DecisionTree.Node type. I don't know how to work with this and can't get it to export/save.
In:DataFrame(PrunedModel)
Out:LoadError: MethodError: `convert` has no method matching convert(::Type{DataFrames.DataFrame}, ::DecisionTree.Node)
This may have arisen from a call to the constructor DataFrames.DataFrame(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{DataFrames.DataFrame}, !Matched::Array{T,2})
convert(::Type{DataFrames.DataFrame}, !Matched::Dict{K,V})
...
while loading In[22], in expression starting on line 1
in call at essentials.jl:56
In:typeof(PrunedModel)
Out:DecisionTree.Node
Any ideas how I can get this model saved for use later?

If I understand correctly that this is a Julia object, you should try using the JLD.jl package to save the object to disk and load it back in, preserving the type information.

Related

Object name as variable in Logtalk

Is this possible to get the name of an object as a variable? I’m trying to make a database where each object represents each person. I’ve got objects with [name/1, surname/1], but when I ask e.g.
X::name(john).
it gives me an error. Ofc there is no problem to get the atom by using this method:
object_id::name(X).
The ::/2 message sending control construct indeed requires a bound first argument at call time. But you can enumerate existing objects using the current_object/1 built-in predicate:
| ?- current_object(Person), Person::name(john).
...
However, this solution may also result in errors as we will be enumerating all objects by backtracking and not all of them will understand a name/1 message. A better solution is thus to only enumerate objects that understand the name/1 message. Assuming that all objects representing a person implement (directly or through inheritance) a person_protocol, we can use the conforms_to_protocol/2 built-in predicate:
| ?- conforms_to_protocol(Person, person_protocol),
Person::name(john).
...
See https://logtalk.org/manuals/refman/predicates/conforms_to_protocol_2_3.html for details.

how to bind methods to (nested) numpy dtype?

I am wishing to associate object methods with dtypes in a numpy structured array. That is the underlying object instance state of my collection of object is already packed into a numpy (nested dtype) record_arrays..
I already have a set of Python objects, that are constructed on top of views of these numpy structured array dtypes that then operate on this underlying continuously packed object instance data.
That is: I am wishing to use object oriented programming methods, while working with the underlying instance state in it's native packed, structured format.
-> Currently working with these numpy structured arrays using Python object methods, requires that explicitly instantiate a Python object on top of it's associated array view each time I re-reference it within the array..
Might it be possible to more directly associate object methods with a numpy structured array (nested) dtype, so that one could avoid having to reconstruct the object instance each time ??
Your description is confusing. Some basic code might help make it clearer. But I'll throw out some possibly relevant observations.
A numpy array has a (large) continuous data buffer (flat list of bytes), which it divides into 'records'. 'shape' and 'strides' are used to step through the records. 'dtype' determines how it 'views' each record.
While these arrays are a Python object type, and dtype is also a class, in general numpy programmers don't put a lot of effort into constructing added array object classes. The arrays may be attributes of larger objects (dictionaries, lists, tuples, or custom classes).
matrix and masked_arrays are examples of subclassing ndarray. I have not seen many user defined subclasses; in part because the amount of work to make them fully functional isn't worth it. recarray is a subclass that allows you to access fields of a structured array with attribute syntax. It may be worth looking at its 'getattr` method. Getting general array operations to return the correct array class or subclass is a bit tricky.
np.lib.index_tricks defines a few classes that let you use indexing syntax inplace of function syntax (ie. they define custom __getitem__ methods).
I am not aware of ways to subclass dtype. The most general base dtype is 'object'. That just stores a pointer to a Python object that is stored elsewhere. That pointer can point to anything - a number, None, a list, another array, etc. In structured arrays, dtype is a compound type, consisting of 'list' of sub-dtypes (which may be nested). But ultimately the structured dtype consists of multiple basic dtypes (ints, floats, strings, object).

How to initialize a Ref<?> field in objectify to a dummy value

I have a collection(arraylist) of Ref `s ,the objectify documentation says that I need to initialize collections for them to be persisted and hence modified in the future.....
Now , Ref points to an object but when I launch my app for the first time I dont have any objects in the data store...so whats the best way for me to initialize a dummy value......
Is my assumption that a Ref<> needs to point to a real object in the data store?
Two things:
You should just initialize an empty collection. You don't need to add anything to it. eg, field = new ArrayList<Ref<Thing>>();
It's actually not even required that you initialize the collection. It's just a good idea for reasons that will become apparent if you use the system for a while.

(De)serializing an object as an array in XStream

I'm trying to clean up some old code by replacing some arrays that were being passed around with proper objects to improve readability and to encapsulate some behaviour. I ran into a problem when it turned out the arrays were being run through XStream for persistence.
I need to retain the format of the serialization and the arrays in question are inside various other objects being (de)serialized through XStream. Is there and easy way to handle this?
I'm hoping there's an Annotation I can apply or simple XStream Converter I can write for my new classes and be done with it, but from what I can see it would require writing Converters for each of the containing classes instead. I'm not sure as I'm not familiar with XStream. If there isn't a easy solution I'm just going to have to give up and leave the arrays in place as I don't have the time budgeted for anything fancy or to learn the finer points of XStream.
Specifically, I have a TileLayer that has a member int[] metaTileFactors and I want to replace that with class MetaTiling which has members final int x and final int y and still have it serialize and deserialize to/from the same XML as before.

Sort ArrayBuffer[A] in scala?

I have an array in Scala with the class ArrayBuffer[Actor], where Actor is a class that implements the Ordered[Actor] trait. How do I sort this array without coding it manually?
I know there is an object called Sorting, but it doesnt seem to work since ArrayBuffer doesn't implement/extend the right classes.
How do I sort ArrayBuffer[A] type arrays?
If you are using Scala 2.8, you could use the sortWith method of the ArrayBuffer[T] class, which is inherited from the SeqLike trait.
The following code snippet sorts an ArrayBuffer[T] object in ascending order:
def ascendingSort[T <% Ordered[T]](xs: ArrayBuffer[T]) = xs.sortWith(_ < _)
Note that this does not mutate the actual ArrayBuffer, but creates a new one with the elements in the right order.
If you are using Scala 2.7, you could use the stableSort method of the Sorting object. This takes the elements of the ArrayBuffer and produces an array of elements sorted in the right order (given by a closure as an argument, ascending as a default).
For example:
val a = new scala.collection.mutable.ArrayBuffer[Int]()
a += 5
a += 2
a += 3
scala.util.Sorting.stableSort(a)
The important question is what do you want to do with the ArrayBuffer. Usually, a Buffer is used internally in different algorithms in order to increase the performance of intermediate results. If you are using it for that, have a look at the ways of sorting the collection you want to return at the end of your algorithm. The Sorting object already provides a way of transforming an ArrayBuffer into a sorted Array.
From the scaladoc of the Buffer class:
Buffers are used to create sequences of elements incrementally
As you are using it with Actors, it might be used for some kind of actor queue - in which case, you might want to have a look at the Queue collection.
Hope it helps,
-- Flaviu Cipcigan
Btw, the Actor class here is my own class used for "Actors" in a world created using my new game engine for scala ("Awesome Game Engine for Scala ~ AGES"), so it has nothing to do with the concurrency actor class. Also, implementations of lists in scala are a jungle, everything is either deprecated or implemented in a lot of different ways...ArrayBuffer works for my need (my need being a variable size array for containing actors).
Hope this clarifies :)

Resources