A case sensitive Error of not finding object 'X' in partykit::ctree when there is not even an object X - party

library(party)
library(ggparty)
fit_ctree<-partykit::ctree(Distance_dich~.,
+ data=t_train,
+ maxdepth = 5)
Error in .ctree_test_internal(x = x, X = X, ix = NULL, Xleft = Xleft, :
object 'X' not found
Why am i getting this error if there is not even 'X' in the code?

We had the same problem. Seems related to logical variables in our case. Worked with as.numeric() of the logical vars.

Related

Are there any restrictions with LUT: unbounded way in dimension

When trying to run the sample code below (similar to a look up table), it always generates the following error message: "The pure definition of Function 'out' calls function 'color' in an unbounded way in dimension 0".
RDom r(0, 10, 0, 10);
Func label, color, out;
Var x,y,c;
label(x,y) = 0;
label(r.x,r.y) = 1;
color(c) = 0;
color(label(r.x,r.y)) = 255;
out(x,y) = color(label(x,y));
out.realize(10,10);
Before calling realize, I have tried to statically set bound, like below, without success.
color.bound(c,0,10);
label.bound(x,0,10).bound(y,0,10);
out.bound(x,0,10).bound(y,0,10);
I also looked at the histogram examples, but they are a bit different.
Is this some kind of restrictions in Halide?
Halide prevents any out of bounds access (and decides what to compute) by analyzing the range of the values you pass as arguments to a Func. If those values are unbounded, it can't do that. The way to make them bounded is with clamp:
out(x, y) = color(clamp(label(x, y), 0, 9));
In this case, the reason it's unbounded is that label has an update definition, which makes the analysis give up. If you wrote label like this instead:
label(x, y) = select(x >= 0 && x < 10 && y >= 0 && y < 10, 1, 0);
Then you wouldn't need the clamp.

Can i declare a local variable in a for loop?

for x = 1, 16 do
for y = 1, 16 do
local cntr = Center:new()
cntr.point = {x = 0.5 + x - 1, y = 0.5 + y - 1}
centerLookup[cntr.point] = cntr
table.insert(self.centers, cntr)
end
end
In the code above, centerLookup[point] is meant to look up the respective Center object by inputting a point location.
However, when I try to do this:
function neighbors(center, sqrtsize)
if center.point.y + 1 < sqrtsize then
local up = {x = center.point.x, y = center.point.y+1}
local centerup = centerLookup[up]
table.insert(center.neighbors, centerup)
end
end
centerup returns as a nil value
Idk if the problem is that I can't use a table as an index, but that is what I'm thinking.
Anybody know what's wrong here?
P.S. if it's helpful, centers start at 0.5 (so [0.5, 0.5] would be the first center, then [0.5, 1.5], etc.)
Thanks in advance!
This has nothing to do with local variables and everything to do with the fact that tables are compared by-reference and not by-value.
In Lua, tables are reference types that have their own identity. Even if two tables have the same contents, Lua does not consider them equal unless they are the exact same object.
To illustrate this, here is some sample code, and the printed values:
local tbl1 = {x = 0.5, y = 0.5}
local tbl2 = tbl1
local tbl3 = {x = 0.5, y = 0.5}
print(tbl1 == tbl2) -- True; tbl1 and tbl2 both reference the same table
print(tbl1 == tbl3) -- False; tbl1 and tbl3 reference different tables
local up = {x = center.point.x, y = center.point.y+1}
local centerup = centerLookup[up]
In this snippet, up is a completely new table with only one reference (the up variable itself). This new table won't be a key in your centerLookup table, even if a table key exists with the same contents.
cntr.point = {x = 0.5 + x - 1, y = 0.5 + y - 1}
centerLookup[cntr.point] = cntr
table.insert(self.centers, cntr)
In this snippet, you create a new table, and reference it in three different places: cntr.point, centerLookup as a key, and self.centers as a value. You presumably iterate through the self.centers array, and use the exact same table to look up items in the centerLookup table. However, if you were to use a table not in the self.centers array, it would not work.
Colonel Thirty Two explained the reason why your code not working as expected. I just want to add quick solution:
function pointToKey(point)
return point.x .. "_" .. point.y
end
Use this function for lookup in both places
--setup centerLookup
centerLookup[pointToKey(cntr.point)] = cntr
--find point from lookup
local centerup = centerLookup[pointToKey(up)]

Effectively pick the variable with the min/max value that was assigned to it

So if every variable has a value assigned to it ( I mean value is an attribute of variable) . How do i most efficiently pick out the variable that has the max or min value ?
Can you give an example in Python please ?
So this would be my approach in Python :
domains = []
for var in variables :
domains.append(var.value)
min= min(domains)
for var in variables :
if var.value == min :
return var
domains.index(min(domains))
You may say that is not efficient but asymptotically you can't do any better when your list is not sorted.
A general sketch would be something like this (improvements would be providing a way of extracting the wanted value, once you have a list, the actual element can easily be retrieved using the index):
def MinAndIndex(l):
minval = l[0]
min_ix = 0
cnt = 0
for e in l:
if e < minval:
minval = e
min_ix = cnt
cnt += 1
return minval, min_ix

Remove the first char to extract an integer (unhandled exception: Subscript)

I'm trying to write a function which extracts only the integer in a string.
All my strings have the format Ci where C is a single character and i is an integer. I would like to be able to remove the C from my string.
I tried something like this :
fun transformKripke x =
if size x > 1
then String.substring (x, 1, size x)
else x
But unfortunately, I get an error like unhandled exception: Subscript.
I assume it's because sometimes my string will be empty and size of empty string is not working. But I don't know how to make it work... :/
Thanks in advance for your help
Best Regards.
The problem is calling String.substring (x, 1, size x) when x is not long enough.
The following should fix your immediate problem:
fun transformKripke s =
if size s = 0
then s
else String.substring (s, 1, size s)
or slightly prettier:
fun transformKripke s =
if size s = 0
then s
else String.extract (s, 1, NONE) (* means "until the end" *)
But you may want to consider naming your function something more general so that it can be useful in more senses than performing a Kripke transform (whatever that is). For example, you may want to be able to extract an actual int the first time one occurs anywhere in a string, regardless of how many non-integer characters that precede it:
fun extractInt s =
let val len = String.size s
fun helper pos result =
if pos = len
then result
else let val c = String.sub (s, pos)
val d = ord c - ord #"0"
in case (Char.isDigit c, result) of
(true, NONE) => helper (pos+1) (SOME d)
| (true, SOME ds) => helper (pos+1) (SOME (ds * 10 + d))
| (false, NONE) => helper (pos+1) NONE
| (false, SOME ds) => SOME ds
end
in helper 0 NONE
end
My mistake was stupid,
The string is finishing at size x -1 not size x. So now it's correct :
fun transformKripke x =
if size x > 1
then String.substring (x, 1, (size x)-1)
else x
Hope it will help ! :)

Convert Excel Column Number to Column Name in Matlab

I am using Excel 2007 which supports Columns upto 16,384 Columns. I would like to obtain the Column name corresponding Column Number.
Currently, I am using the following code. However this code supports upto 256 Columns. Any idea how to obtain Column Name if the column number is greater than 256.
function loc = xlcolumn(column)
if isnumeric(column)
if column>256
error('Excel is limited to 256 columns! Enter an integer number <256');
end
letters = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
count = 0;
if column-26<=0
loc = char(letters(column));
else
while column-26>0
count = count + 1;
column = column - 26;
end
loc = [char(letters(count)) char(letters(column))];
end
else
letters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
if size(column,2)==1
loc =findstr(column,letters);
elseif size(column,2)==2
loc1 =findstr(column(1),letters);
loc2 =findstr(column(2),letters);
loc = (26 + 26*loc1)-(26-loc2);
end
end
Thanks
As a diversion, here is an all function handle example, with (almost) no file-based functions required. This is based on the dec2base function, since Excel column names are (almost) base 26 numbers, with the frustrating difference that there are no "0" characters.
Note: this is probably a terrible idea overall, but it works. Better solutions are probably found elsewhere in the file exchange.
First, the one file based function that I couldn't get around, to perform arbitrary depth function composition.
function result = compose( fnHandles )
%COMPOSE Compose a set of functions
% COMPOSE({fnHandles}) returns a function handle consisting of the
% composition of the cell array of input function handles.
%
% For example, if F, G, and H are function handles with one input and
% one output, then:
% FNCOMPOSED = COMPOSE({F,G,H});
% y = FNCOMPOSED(x);
% is equivalent to
% y = F(G(H(x)));
if isempty(fnHandles)
result = #(x)x;
elseif length(fnHandles)==1
result = fnHandles{1};
else
fnOuter = fnHandles{1};
fnRemainder = compose(fnHandles(2:end));
result = #(x)fnOuter(fnRemainder(x));
end
Then, the bizarre, contrived path to convert base26 values into the correct string
%Functions leading to "getNumeric", which creates a numeric, base26 array
remapUpper = #(rawBase)(rawBase + (rawBase>='A')*(-55)); %Map the letters 'A-P' to [10:26]
reMapLower = #(rawBase)(rawBase + (rawBase<'A')*(-48)); %Map characters '0123456789' to [0:9]
getRawBase = #(x)dec2base(x, 26);
getNumeric = #(x)remapUpper(reMapLower(getRawBase(x)));
%Functions leading to "correctNumeric"
% This replaces zeros with 26, and reduces the high values entry by 1.
% Similar to "borrowing" as we learned in longhand subtraction
borrowDownFrom = #(x, fromIndex) [x(1:(fromIndex-1)) (x(fromIndex)-1) (x(fromIndex+1)+26) (x((fromIndex+2):end))];
borrowToIfNeeded = #(x, toIndex) (x(toIndex)<=0)*borrowDownFrom(x,toIndex-1) + (x(toIndex)>0)*(x); %Ugly numeric switch
getAllConditionalBorrowFunctions = #(numeric)arrayfun(#(index)#(numeric)borrowToIfNeeded(numeric, index),(2:length(numeric)),'uniformoutput',false);
getComposedBorrowFunction = #(x)compose(getAllConditionalBorrowFunctions(x));
correctNumeric = #(x)feval(getComposedBorrowFunction(x),x);
%Function to replace numerics with letters, and remove leading '#' (leading
%zeros)
numeric2alpha = #(x)regexprep(char(x+'A'-1),'^#','');
%Compose complete function
num2ExcelName = #(x)arrayfun(#(x)numeric2alpha(correctNumeric(getNumeric(x))), x, 'uniformoutput',false)';
Now test using some stressing transitions:
>> num2ExcelName([1:5 23:28 700:704 727:729 1024:1026 1351:1355 16382:16384])
ans =
'A'
'B'
'C'
'D'
'E'
'W'
'X'
'Y'
'Z'
'AA'
'AB'
'ZX'
'ZY'
'ZZ'
'AAA'
'AAB'
'AAY'
'AAZ'
'ABA'
'AMJ'
'AMK'
'AML'
'AYY'
'AYZ'
'AZA'
'AZB'
'AZC'
'XFB'
'XFC'
'XFD'
This function I wrote works for any number of columns (until Excel runs out of columns). It just requires a column number input (e.g. 16368 will return a string 'XEN').
If the application of this concept is different than my function, it's important to note that a column of x number of A's begins every 26^(x-1) + 26^(x-2) + ... + 26^2 + 26 + 1. (e.g. 'AAA' begins on 26^2 + 26 + 1 = 703)
function [col_str] = let_loc(num_loc)
test = 2;
old = 0;
x = 0;
while test >= 1
old = 26^x + old;
test = num_loc/old;
x = x + 1;
end
num_letters = x - 1;
str_array = zeros(1,num_letters);
for i = 1:num_letters
loc = floor(num_loc/(26^(num_letters-i)));
num_loc = num_loc - (loc*26^(num_letters-i));
str_array(i) = char(65 + (loc - 1));
end
col_str = strcat(str_array(1:length(str_array)));
end
Hope this saves someone some time!

Resources