I've noticed some strange behaviour in gdscript.
when you declare variables
var value = [0, 0, 0, 0, 0]
var values = []
and append one to the other
values.append(value)
and then change something in the array
value[1] = 1
If you would then print the results [print(value, values)]
You get
prints: [0, 1, 0, 0, 0][[0, 1, 0, 0, 0]]
expected behaviour
prints: [0, 1, 0, 0, 0][[0, 0, 0, 0, 0]]
What is happening is that, in GDScript, when you append the array value to values, you're actually appending it's reference to the array. So you end up with an array values which has as it's first entry a reference to the array value. So when you change the value of the reference in values, you're also changing it for your original variable.
for further reading, check out this wikipedia page
Arrays in gdscript are passed by the reference, so when you append an array to other array, it only stores the pointer to the first array, so every change made in first array will be visible in the second array. What you want to do is to copy the first array and append that copy to the second array.
In gdscript currently there is no direct method to copy an array. Maybe this will change with godot 4.0. Now you can use:
values.append([] + value)
Related
I want to add 2 numbers at the beginning of array using splice method . can some one explain why the 2nd method gives me an empty array as output?.
const arrayold = [5, 6, 7, 8];
arrayold.splice(0, 0, 1, 2);
const arrayNew = arrayold;
console.log(arrayNew);
const arrayold = [5, 6, 7, 8];
const arrayNew = arrayold.splice(0, 0, 1, 2);
console.log(arrayNew);
Because it returns a list of deleted objects!
The actual modified thing is in the variable/s
Like it is in the first case, you decided not to do var o1=o2.splice( /*arguments*/ ); but var _new=_old instead.
You can read more at mdn about what splice modifies and what it returns!
splice() modifies the source array and returns an array of the removed items. Since you didn't ask to remove any items, you get an empty array back. It modifies the original array to insert your new items
const arrayold = [5, 6, 7, 8];
const arrayNew= arrayold.splice(0, 0, 1, 2);
console.log(arrayNew);
The splice method generally return the removed item from an array. So In your second sceanrio arrayold.splice(0,0,1,2) you are not removing any element as you have mentioned 0 that's why it is giving you empty array
So my question is simply,
given
var myArray = new[]{new []{0, 0, 7, 0},
new []{0, 0, 0, 0},
new []{0, 0, 0, 0},
new []{0, 0, 0, 0}};
how do I change the value that is 7 to 0?
i know to index a single array I would use myArray[2] = 0
I just dont know how to index a value in an array of arrays.
is it something like this, myArray[0[2]]?
any help would be greatly appreciated.
Very close. Think about the object myArray[0] returns - It's an array itself, yes?
And to access the 3rd element in the first list, we just do myArray[0][2]. Essentially, we are indexing the 2nd item in the first item of myArray
I need to populate a vector with elements of another, smaller vector. So say the vector I need to populate is of length ten and is currently all zeros, i.e.
vector = [0,0,0,0,0,0,0,0,0,0]
Now suppose I have already define a vector
p = [1, 2, 3, 4, 5]
How could I populate "vector" with the array "p" so that the result is [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]? Bear in mind, I want the other positions in "vector" to remain unchanged. I have already tried using repmat(p, length(p)) but that ends up giving me something of the form [1,2,3,4,5,1,2,3,4,5]. Thanks!
Try a combination of vector slicing and concatenation:
vector = cat(1, p, vector(5:))
This is faster:
vector(1:5) = p
More generally,
vector(1:numel(p)) = p
I keep having an error running this part of my code:
scores = cross_val_score(XGB_Clf, X_resampled, y_resampled, cv=kf)
The error is :
DataConversionWarning: A column-vector y was passed when a 1d array
was expected. Please change the shape of y to (n_samples, ), for
example using ravel(). y = column_or_1d(y, warn=True)
I know there are lots of answers to this question, and that I need to use ravel(), but using it does not change anything!
Also, the array "y" I'm passing to the function is not a column-vector ...
See:
y_resampled
Out[82]: array([0, 0, 0, ..., 1, 1, 1], dtype=int64)
When I run
y_resampled.ravel()
I get
Out[81]: array([0, 0, 0, ..., 1, 1, 1], dtype=int64)
which is exactly the same as my initial variable...
Also, when I run y_resampled.values.ravel() I get an error telling me that this is well a numpy array...
Traceback (most recent call last):
File "<ipython-input-80-9d28d21eeab5>", line 1, in <module>
y_resampled.values.ravel()
AttributeError: 'numpy.ndarray' object has no attribute 'values'
Does any one of you have a solution to this?
Thanks a lot!
Check out this answer man!
Simply:
model = forest.fit(train_fold, train_y.values.ravel())
in you write y_resampled as dataframe, you can use values function.
import pandas as pd
y_resampled = pd.DataFrame(y_resampled)
Hey, having a wee bit of trouble. Trying to assign a variable length 1d array to different values of an array, e.g.
a(1) = [1, 0.13,0.52,0.3];
a(2) = [1, 0, .268];
However, I get the error:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> lab2 at 15
a(1) = [1, 0.13,0.52,0.3];
I presume this means that it's expecting a scalar value instead of an array. Does anybody know how to assign the array to this value?
I'd rather not define it directly as a 2d array as it is for are doing solutions to different problems in a loop
Edit: Got it!
a(1,1:4) = [1, 0.13,0.52,0.3];
a(2,1:3) = [1, 0, .268];
What you probably wanted to write was
a(1,:) = [1, 0.13,0.52,0.3];
a(2,:) = [1, 0, .268];
i.e the the first row is [1, 0.13,0.52,0.3] and the second row is [1, 0, .268]. This is not possible, because what would be the value of a(2,4) ?
There are two ways to fix the problem.
(1) Use cell arrays
a{1} = [1, 0.13,0.52,0.3];
a{2} = [1, 0, .268];
(2) If you know the maximum possible number of columns your solutions will have, you can preallocate your array, and write in the results like so (if you don't preallocate, you'll
get zero-padding. You also risk slowing down your loop a lot, if there are many iterations, because the array will have to be recreated at every iteration.
a = NaN(nIterations,maxNumCols); %# this fills the array with not-a-numbers
tmp = [1, 0.13,0.52,0.3];
a(1,1:length(tmp)) = tmp;
tmp = [1, 0, .268];
a(2,1:length(tmp)) = tmp;