Is there something like an in-place version of numpy concatenate? - numpy

I'd like to be able to concatenate multiple arrays and have changes in the original arrays reflected in the concatenated version. So the code below should not raise an AssertionError.
arr1 = np.zeros(3)
arr2 = np.zeros(3)
arr3 = np.in_place_concatenate([arr1, arr2])
arr1[0] = 10
assert arr3[0] == 10
Does this concept exist in numpy?

Related

strip and filter strings in ragged tensor

I would like learn if there is a decent and tensorflownic way to do follow conversion. Basically each string(row) as several words, and each word has a suffix like "%1", and goal is to strip string and only leave words with suffix value <= certain target value.
It is not hard to achieve using regular python programming. But I am thinking of adding the step to a tf computational graph, so a more tensorflownic way is preferred.
#From
text = tf.constant(['a1%0,a2%0,a3%1,a4%2,a5%3,a6%4','a7%3,a8%4',...]) #in shape of (n,) and n is large
#if target = 1, result will be
res = tf.ragged.constant([["a1", "a2", "a3"], [],...])
#if target = 3, result will be
res = tf.ragged.constant([["a1", "a2", "a3", "a4", "a5"], ["a7"],...])
You can do the following (tested in tensorflow 2.9)
text = tf.constant(['a1%0,a2%0,a3%1,a4%2,a5%3,a6%4','a7%3,a8%4',...])
target = 1
a = tf.strings.split(tf.strings.regex_replace(text, "%\d", ""), ",")
b = tf.strings.split(tf.strings.regex_replace(text, "[^,]*%", ""), ",")
b = tf.strings.to_number(b)
c = tf.ragged.boolean_mask(a, (b<=target))

Why does GetRows() method return a transposed array?

Im using a Adodb connection
and reading the result of a query
into an array with
array = recordSet.GetRows()
which leads to a transposed array of dimensions
(row,col)
(0, 0)
(0, 1)
(0, 2)
instead of
(row,col)
(0, 0)
(1, 0)
(2, 0)
so it should be a 3 x 1 not 1 x 3 array
Any suggestions ?
It is because it is returning an Array containing (intField, intRecord):
https://msdn.microsoft.com/en-us/library/office/ff194427.aspx
So it's a matter of different interpretation in the way that intField is basically the Column and intRecord the Row (both zero-based).
note that (using ADODB driver):
Range("A1").CopyFromRecordset recordset ;does not transpose a
;recordset
varArray = recordset.GetRows(10) ;transposes a recordset
Range("A1:L10").Value = varArray ;does not transposes array
listbox.list = varArray ;does not transposes array
listbox.column = varArray ;transpose array
listbox.column = recordset.GetRows(10) ;does not transpose a
;recordset

LinEst function

I'm trying to teach myself some basic VBA in Excel 2010 and I've come across a problem I can't google myself out of. The objective is to create a button which when pressed, automatically does linest for me and writes the values into an array. So far, this is my code.
Private Sub CommandButton1_Click()
Dim linest As Variant
Dim linestArray(4,1) as Variant
Dim i As Integer, j as Integer
linest = Application.LinEst(Range("U49:U51"), Range("T49:T51"), True, True)
For i = 0 To 4
linestArray(i,0) = accessing values of linest variable fyrst column
Cells(68 + i, 21) = linestArray(i,0)
Next
For j = 0 To 4
linestArray(j,1) = accessing values of linest variable second column
Cells(68 + j, 22) = linestArray(j,0)
Next
End Sub
How do I access the values of variable linest so I can store them to an array and print them? Thank you.
EDIT: I figured it out. Variable linest is already an array! I feel pretty dumb. Sorry, this can be ignored.
New code:
Dim linestArray As Variant
linestArray = Application.LinEst(Range("U49:U51"), Range("T49:T51"), True, True)
For i = 0 To 4
For j = 0 To 1
Cells(68 + i, 21 + j) = linestArray(i + 1, j + 1)
Next
Next
The output of any such formula is a Variant array. So you've got that part right.
For a general approach to these Application. (use WorksheetFunction. instead, it's much faster) type functions is...
Type the function in Excel (as an array formula, Ctrl-Shift-Return, if need be)
The output is an N x M matrix of some sort (N =1 , M =1) for most cases
When you do Var = Application.Linest(xyx), the answer gets stored in Var
Get the size of the Var using Ubound(Var, 1), Ubound(Var, 2) to get number of rows and columns (note that these are base 0 type arrays)
Usually, the size will be one x one. In that case, your variable is stored in Var(0,0) i.e. a zero base multidimensional variant array, the top left element.
Hope this helps.

Rhino: Not All Arguments Converted During String Formatting

I am attempting to execute a code using Rhino Python and I am having some issues with the following TypeError:
Message: not all arguments converted during string formatting
The code I have written is meant to read point coordinates from a file "newpoints.csv" and use them as arguments for Rhino Python's 'AddLine' function.
#!/usr/bin/env python
import rhinoscriptsyntax as rs
file = open("C:\\Users\\Seshane Mahlo\\Documents\\MSc Thesis\\newpoints.csv", "r")
lines = file.readlines()
file.close()
ab = len(lines)
seq = range(0, ab-1, 2)
coordinates = []
startvals = []
stopvals = []
for line in lines:
coords = line.split(',')
xcoord = float(coords[0])
ycoord = float(coords[1])
point = (xcoord, ycoord)
coordinates.append(point)
starts = range(0, ab-2, 2)
ends = range(1, ab+1, 2)
for i,j in zip(starts, ends):
strt = coordinates[i]
stp = coordinates[j]
rs.AddLine(start=strt,end=stp)
I think there is a small mistake in your code here:
starts = range(0, ab-2, 2)
ends = range(1, ab-1, 2)
which should be
starts = range(0, ab-1, 2)
ends = range(1, ab, 2)
because the last element you get from the range function is one less than the stop argument.
But what is causing the error is that you are trying to add a line, which is composed of two 3d points using a 2-tuple (x,y)
To fix this change:
point = (xcoord, ycoord)
to
point = (xcoord, ycoord, 0)
or whatever you want your z-coordinate to be.

How do I make a multidimensional jagged array?

I am trying to make a jagged array that has,
3 columns
with 5,4,4 rows respectively
that each have 2 rows
that have a varying number of rows, for example 6,9,5,6,4
I have the first part setup but I don't know how to get to another level of the jagged array.
Dim potentialStructure()() As Short = {New Short() {0, 1, 2, 3, 4}, New Short() {0, 1, 2, 3}, New Short() {0, 1, 2, 3}}
And how would I access an element at say, the lowest level? Thanks!
I have only just spotted your question so you may have solved it by now. If not, the following extract from something I am playing with may help. Here I have a regular 2D array with a jagged 2D array inside. I trust you can strip out the bits you do not need and understand the bits relevant to your requirement.
I have a structure which includes a jagged array:
Structure Sabc
Dim def()() As Long
Dim ghi As String
End Structure
I have a non-jagged 2D array of these structures:
Dim abc(,) As Sabc
I can calculate the required size of abc at the beginning of my program:
ReDim abc(sizeD1abc, sizeD2abc)
I can access simple variables within an element of abc easily:
With abc(crntD1abc, crntD2abc)
.ghi = "Example"
End With
The first dimension of the jagged array always has an upper bound of 3. I set it so:
With abc(crntD1abc, crntD2abc)
.def = New Long(3)() {}
End With
I avoid using ReDim more often that necessary because it is an resource hungry command. Here I initialise and step the number of elements by 10 but the values I choose depends on how big I expect the array to be and how much the actual size may vary.
With abc(crntD1abc, crntD2abc)
.def(crntD1def) = New Long(9) {}
End With
crntD2def = -1
The following adds more elements if I need them before storing a value:
With abc(crntD1abc, crntD2abc)
crntD2def +=1
If crntD2def > UBound(.def(crntD1def)) Then
ReDim Preserve .def(crntD1def)(UBound(.def(crntD1def)) + 10)
End If
.def(crntD1def)(crntD2def) = expression
End With
When I have finished, I discard the excess elements:
With abc(crntD1abc, crntD2abc)
If crntD2def < UBound(.def(crntD1def)) Then
ReDim Preserve .def(crntD1def)(crntD2def - 1)
End If
End With