Still new to rust, so sorry if a little bit of a basic question, but I can't find any good resource. I'm writing some ipc code, and I have the following snippet:
// Header format | "magic string" | msg len | msg type id |
let mut header: [u8; 14] = [105, 51, 45, 105, 112, 99, 0, 0, 0, 0, 0, 0, 0, 0];
I want to set header [10..14] to the native byte order encoding of a 32bit message type. I also want to set header[6..10] to the message length (again native byte order 32 bit int).
So far I have tried:
Using slices: (cannot find a form that compiles)
header[10 .. 14] = msg_type.to_ne_bytes();
Some weird array design (cannot find a form that compiles)
[header[10], header[11], header[12], header[13]] = msg_type.to_ne_bytes();
Saving the result and writing it over. This works, but seems inelegant. If this is the right answer, I understand, it just feels wrong.
let res = msg_type.to_ne_bytes();
header[10] = res[0];
header[11] = res[1];
header[12] = res[2];
header[13] = res[3];
If there is also some way to do this with the array creation, I am happy to look into it. Thanks in advance!
P.S. this is a client for swaywm's ipc messaging if anyone is curious.
Using slices is the way to go. It's an array, so you get all the slice methods on the array as well.
We take your example header[10 .. 14] = msg_type.to_ne_bytes(); and turn it into this, which works:
header[10..14].copy_from_slice(&msg_type.to_ne_bytes());
Note that in a.copy_from_slice(b) you need to ensure that a and b have the same length, otherwise the method call panics.
(An alternative but very similar way would be to use the crate byteorder.)
Related
This is an example array i stored in a string property:
[12, 15, 75, 60]
When i tried to retrieve it
and access the index number, it included every character that denotes the array, for example if i ask for array[0], it gives me '[', instead of '12', array[3] gives me ',' array[6] gives me '5' and so on.
How can blender Python read the string like it does with array?
I tried enclosing things in int(), it throws a message "string indices must be integers" or "invalid literal for int() with base 10
Thank for your advance response!
I didn't include any code snippet as this problem is simple enough to understand, thanks, more power
If you want a string array let's convert it this way.
a = '[12, 15, 75, 60]'
b = a[1:len(a)-1].split(",")
print(b[0])
If you want a int array let's convert it this way.
a = '[12, 15, 75, 60]'
b = list(map(int, a[1:len(a)-1].split(",")))
print(b[0])
numpy experts,
I'm using numpy.
I want to compare two arrays, get the largest value that is smaller than one of the arrays, and calculate the difference between them.
For example,
A = np.array([3, 5, 7, 12, 13, 18])
B = np.array([4, 7, 17, 20])
I want [1, 0, 4, 2] (4-3, 7-7, 17-13, 20-18) , in this case.
The problem is that the size of the A and B arrays is so large that it would take a very long time to do this by simple means. I can try to divide them to some size, but I wonder if there is a simple numpy function to solve this problem.
Or can I use numba?
For your information, This is my current very stupid codes.
delta = np.zeros_like(B)
for i in range(len(B)):
index_A = (A <= B[i]).argmin() - 1
delta[i] = B[i] - A[index_A]
I agree with #tarlen555 that the problem is mostly related to the for-loop. I guess this one is already much faster:
diff = B-A[:,np.newaxis]
diff[diff<0] = max(A.max(), B.max())
diff.min(axis=0)
In the second line, I wanted to fill all entries with negative values with something ridiculously large. Since your numbers are integer, np.inf doesn't work, but something like that could be more elegant.
EDIT:
Another way:
from scipy.spatial import cKDTree
tree = cKDTree(A.reshape(-1, 1))
k = 2
large_value = max(A.max(), B.max())
while True:
indices = tree.query(B.reshape(-1, 1), k=k)[1]
diff = B[:,np.newaxis]-A[indices]
if np.all(diff.max(axis=-1)>=0):
break
k += 1
diff[diff<0] = large_value
diff.min(axis=1)
This solution could be more memory-efficient but frankly I'm not sure how much more.
As the title suggests, I need to sort the rows of a certain matrix by one of its columns, preferably in place if at all possible. Said column contains Strings (the array being of type Array{Union{Float64,String}}), and ideally the rows should end up in an alphabetial order, determined by this column. The line
sorted_rows = sort!(data, by = i -> data[i,2]),
where data is my matrix, produces the error ERROR: LoadError: UndefKeywordError: keyword argument dims not assigned. Specifying which part of the matrix I want sorted and adding the parameter dims=2 (which I assume is the dimension I want to sort along), namely
sorted_rows = sort!(data[2:end-1,:], by = i -> data[i,2],dims=2)
simply changes the error message to ERROR: LoadError: ArgumentError: invalid index: 01 Suurin yhteinen tekijä ja pienin yhteinen jaettava of type String. So the compiler is complainig about a string being an invalid index.
Any ideas on how this type of sorting cound be done? I should say that in this case the string in the column can be expected to start with a number, but I wouldn't mind finding a solution that works in the general case.
I'm using Julia 1.1.
You want sortslices, not sort — the latter just sorts all columns independently, whereas the former rearranges whole slices. Secondly, the by function doesn't take an index, it takes the value that is about to be compared (and allows you to transform it in some way). Thus:
julia> using Random
data = Union{Float64, String}[randn(100) [randstring(10) for _ in 1:100]]
100×2 Array{Union{Float64, String},2}:
0.211015 "6VPQbWU5f9"
-0.292298 "HgvHLkufqI"
1.74231 "zTCu1U5Vdl"
0.195822 "O3j43sbhKV"
⋮
-0.369007 "VzFH2OpWfU"
-1.30459 "6C68G64AWg"
-1.02434 "rldaQ3e0GE"
1.61653 "vjvn1SX3FW"
julia> sortslices(data, by=x->x[2], dims=1)
100×2 Array{Union{Float64, String},2}:
0.229143 "0syMQ7AFgQ"
-0.642065 "0wUew61bI5"
1.16888 "12PUn4V4gL"
-0.266574 "1Z2ONSBP04"
⋮
1.85761 "y2DDANcFCe"
1.53337 "yZju1uQqMM"
1.74231 "zTCu1U5Vdl"
0.974607 "zdiU0sVOZt"
Unfortunately we don't have an in-place sortslices! yet, but you can easily construct a sorted view with sortperm. This probably won't be as fast to use, but if you need the in-place-ness for semantic reasons it'll do just the trick.
julia> p = sortperm(data[:,2]);
julia> #view data[p, :]
100×2 view(::Array{Union{Float64, String},2}, [26, 45, 90, 87, 6, 96, 82, 75, 12, 27 … 53, 69, 100, 93, 36, 37, 39, 8, 3, 61], :) with eltype Union{Float64, String}:
0.229143 "0syMQ7AFgQ"
-0.642065 "0wUew61bI5"
1.16888 "12PUn4V4gL"
-0.266574 "1Z2ONSBP04"
⋮
1.85761 "y2DDANcFCe"
1.53337 "yZju1uQqMM"
1.74231 "zTCu1U5Vdl"
0.974607 "zdiU0sVOZt"
(If you want the in-place-ness for performance reasons, I'd recommend using a DataFrame or similar structure that holds its columns as independent homogenous vectors — a Union{Float64, String} will be slower than two separate well-typed vectors, and sort!ing a DataFrame works on whole rows like you want.)
you may want to look at SortingLab.jls fast string sort functions.
]add SortingLab
using SortingLab
idx = fsortperm(data[:,2])
new_data = data[idx]
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)
I faced a problem where I needed to add a new value in the middle of an Array (i.e. make a copy of the original array and replace that with the new one). I successfully solved my problem, but I was wondering whether there were other methods to do this without changing the array to buffer for a while.
val original = Array(0, 1, 3, 4)
val parts = original.splitAt(2)
val modified = parts._1 ++ (2 +: parts._2)
res0: Array[Int] = Array(0, 1, 2, 3, 4)
What I don't like on my solution is the parts variable; I'd prefer not using an intermediate step like that. Is that the easiest way to add the value or is there some better ways to do add an element?
This is precisely what patch does:
val original = Array(0, 1, 3, 4)
original.patch(2, Array(2), 0) // Array[Int] = Array(0, 1, 2, 3, 4)
You can use a mutable version of a collection to do this. The method insert do what you want (insert an element at a given index).
Well, if indeed the extra variable is what's troubling you, you can do it in one go:
val modified = original.take(2) ++ (2 +: original.drop(2))
But using a mutable collection like Augusto suggested might fit better, depending on your use case (e.g. performance, array size, multiple such edits...).
The question is, what's the context? If you are doing this in a loop, allocating a new array every time will kill your performance anyway, and you should rethink your approach (e.g. collect all the elements you want to insert before inserting them).
If you aren't, well, you can use System.arraycopy to avoid any intermediate conversions:
val original = Array(0, 1, 3, 4)
val index = 2
val valueToInsert = 2
val modified = Array.ofDim[Int](original.length + 1)
System.arraycopy(original, 0, modified, 0, index)
modified(index) = valueToInsert
System.arraycopy(original, index, modified, index + 1, original.length - index)
But note how easy it's to make an off-by-one error here (I think there isn't one, but I haven't tested it). So the only reason to do it is if you really need high performance, and that's only likely if it happens in a loop, in which case go back to the second sentence.