How to copy numpy array in matlab style - python

in matlab/ GNU Octave( which i am actually using ), I use this method to copy particular elements of a 2D array to another 2D array:
B(2:6, 2:6) = A
where
size(A) = (5, 5)
My question is, "How can this be achieved in python using numpy?"
currently, for example, I am using the following nested loop in python:
>>> import numpy as np
>>> a = np.int32(np.random.rand(5,5)*10)
>>> b = np.zeros((6,6), dtype = np.int32)
>>> print a
[[6 7 5 1 3]
[3 9 7 2 0]
[9 3 7 6 7]
[9 8 2 0 8]
[8 7 7 9 9]]
>>> print b
[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
>>> for i in range(1,6):
for j in range(1,6):
b[i][j] = a[i-1][j-1]
>>> print b
[[0, 0, 0, 0, 0, 0],
[0, 6, 7, 5, 1, 3],
[0, 3, 9, 7, 2, 0],
[0, 9, 3, 7, 6, 7],
[0, 9, 8, 2, 0, 8],
[0, 8, 7, 7, 9, 9]]
Is there a better way to do this?

It's almost the same as the MATLAB:
b[1:6, 1:6] = a
The only thing is that Python uses 0-based indexing so the second element is 1 instead of 2.

Related

How to strip a 2d array in python?

For editors: this is NOT stripping all strings in an array but stripping the array itself
So suppose i have an array like this:
[[0, 1, 8, 4, 0, 0],
[1, 2, 3, 0, 0, 0],
[3, 2, 3, 0, 5, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
I want a function stripArray(0, array) where the first argument is the "empty" value. After applying this function i want the returned array to look like this:
[[0, 1, 8, 4, 0],
[1, 2, 3, 0, 0],
[3, 2, 3, 0, 5]]
Values that were marked as empty (in this case 0) were stripped from the right and bottom sides. How would I go about implementing such a function?
In the real case where I want to use it in the array instead of numbers there are dictionaries.
It is better to do this vectorized
import numpy as np
arr = np.array([[0, 1, 8, 4, 0, 0],
[1, 2, 3, 0, 0, 0],
[3, 2, 3, 0, 5, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
def stripArray(e, arr):
return arr[(arr!=e).any(axis = 1), :][:, (arr!=e).any(axis = 0)]
stripArray(0, arr)
array([[0, 1, 8, 4, 0],
[1, 2, 3, 0, 0],
[3, 2, 3, 0, 5]])
Here is an answer which doesnt need numpy:
from typing import List, Any
def all_value(value: Any, arr: List[float]) -> bool:
return all(map(lambda x: x==value, arr))
def transpose_array(arr: List[List[float]]) -> List[List[float]]:
return list(map(list, zip(*arr)))
def strip_array(value: Any, arr: List[List[float]]) -> List[List[float]]:
# delete empty rows
arr = [row for row in arr if not all_value(value, row)]
#transpose and delete empty columns
arr = transpose_array(arr)
arr = [col for col in arr if not all_value(value, col)]
#transpose back
arr = transpose_array(arr)
return arr
test = [[0, 1, 8, 4, 0, 0],
[1, 2, 3, 0, 0, 0],
[3, 2, 3, 0, 5, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
result = strip_array(0, test)
Output:
result
[[0, 1, 8, 4, 0],
[1, 2, 3, 0, 0],
[3, 2, 3, 0, 5]]
Code:
def strip_array(array, empty_val=0):
num_bad_columns = 0
while np.all(array[:, -(num_bad_columns+1)] == 0):
num_bad_columns += 1
array = array[:, :(-num_bad_columns)]
num_bad_rows = 0
while np.all(array[-(num_bad_rows+1), :] == 0):
num_bad_rows += 1
array = array[:(-num_bad_rows), :]
return array
array = np.array(
[[0, 1, 8, 4, 0, 0],
[1, 2, 3, 0, 0, 0],
[3, 2, 3, 0, 5, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
)
print(array)
print(strip_array(array, 0))
Output:
[[0 1 8 4 0 0]
[1 2 3 0 0 0]
[3 2 3 0 5 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 1 8 4 0]
[1 2 3 0 0]
[3 2 3 0 5]]
try using np.delete to remove unwanted rows or columns
data=[[0, 1, 8, 4, 0, 0],
[1, 2, 3, 0, 0, 0],
[3, 2, 3, 0, 5, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
def drop_row(data):
lstIdx=[]
for i in range(len(data)):
count=0
for j in range(len(data[i])):
if data[i][j] == 0:
count+=1
if count==len(data[i]):
print("row zero")
lstIdx.append(i)
#for i in lstIdx:
data=np.delete(data,lstIdx,axis=0)
return data
def drop_column(data):
lstIdx=[]
if len(data)==0:
return data
for j in range(len(data[0])):
count=0
for i in range(len(data)):
if data[i][j] == 0:
count+=1
if count==len(data):
print("column zero")
lstIdx.append(j)
data=np.delete(data,lstIdx,axis=1)
return data
data=drop_row(data)
data=drop_column(data)
print(data)
output:
[[0 1 8 4 0]
[1 2 3 0 0]
[3 2 3 0 5]]

Using list comprehension and extend

I have this
rows = self.rows()
aaa = []
for r in range(0, 9, 3):
bbb = []
for c in range(0, 9, 3):
ccc = []
for s in range(3):
ccc.extend(rows[r+s][c:c+3])
bbb.append(ccc)
aaa.append(bbb)
and it returns this
[
[
[5, 0, 0, 0, 6, 0, 0, 2, 9],
[3, 8, 0, 4, 9, 2, 0, 0, 6],
[0, 6, 2, 1, 0, 0, 3, 0, 4]
],
[
[0, 7, 6, 0, 0, 8, 0, 4, 0],
[0, 4, 0, 2, 0, 5, 0, 3, 1],
[0, 3, 1, 0, 4, 9, 0, 0, 0]
],
[
[4, 0, 0, 6, 0, 3, 0, 0, 1],
[0, 0, 0, 7, 0, 0, 0, 0, 0],
[0, 5, 3, 2, 0, 8, 0, 0, 6]
]
]
which is correct.
rows is just a list containing 9 other nested lists, each with exactly 9 integers ranging 0 through 9.
When I try to use list comprehension
[[rows[r+s][c:c+3] for s in range(3) for c in range(0, 9, 3)] for r in range(0, 9, 3)]
I get this
[
[
[5, 0, 0],
[3, 8, 0],
[0, 6, 2],
[0, 6, 0],
[4, 9, 2],
[1, 0, 0],
[0, 2, 9],
[0, 0, 6],
[3, 0, 4]
],
[
[0, 7, 6],
[0, 4, 0],
[0, 3, 1],
[0, 0, 8],
[2, 0, 5],
[0, 4, 9],
[0, 4, 0],
[0, 3, 1],
[0, 0, 0]
],
[
[4, 0, 0],
[0, 0, 0],
[0, 5, 3],
[6, 0, 3],
[7, 0, 0],
[2, 0, 8],
[0, 0, 1],
[0, 0, 0],
[0, 0, 6]
]
]
Clearly I'm doing something wrong, but I can't see what? I've checked other SO questions and they allude to structuring the LC a certain way to stop the innermost list splitting into 9 separate lists, but so far, it's not happening.
Try:
import itertools
[[list(itertools.chain(*[rows[r+s][c:c+3] for s in range(3)])) for c in range(0, 9, 3)] for r in range(0, 9, 3)]
This will give you what you want. This version uses bare Python with no libraries, but seems to be the most concise (of the solutions that work):
[[[rows[r+s][c+i] for s in range(3) for i in range(3)] for c in range(0, 9, 3)] for r in range(0, 9, 3)]
Formatted more readably, this is just:
[[[rows[r+s][c+i] for s in range(3)
for i in range(3)]
for c in range(0, 9, 3)]
for r in range(0, 9, 3)]
This replaces the slice in the original loop nest with an inner i loop, eliminating the need to concatenate intermediate slices (or to generate them).
You can do something like this,
print([[rows[r+s][c:c+3] * 3 for c in range(0, 9, 3)] for r in range(0, 9, 3)])

Combining arrays to yield a new collective array

I have three (n,n) arrays that I need to combine in a very specific way, in order to yield n*n new arrays, that have to be combined into one big array.
I essentially need to take one element from each array and create a new (3,3) array, wherein the diagonal is the three elements (the rest is empty) and then combine these new arrays into one.
It's a bit difficult to explain properly. I've attempted to give an example below which hopefully gives an idea of what I'm trying to do.
Example: Given three (2,3) arrays:
a = np.array([[2,5,9], [7,2,4]])
b = np.array([[3,6,2], [1,6,8]])
c = np.array([[8,7,4], [9,3,1]])
create six arrays with the elements from a, b, and c as the diagonals:
T1 = ([[ 2, 0, 0],
[ 0, 3, 0],
[ 0, 0, 8]])
T2 = ([[ 5, 0, 0],
[ 0, 6, 0],
[ 0, 0, 7]])
T3 = ([[ 9, 0, 0],
[ 0, 2, 0],
[ 0, 0, 4]])
T4 = ([[ 7, 0, 0],
[ 0, 1, 0],
[ 0, 0, 9]])
T5 = ([[ 2, 0, 0],
[ 0, 6, 0],
[ 0, 0, 3])
T6 = ([[ 4, 0, 0],
[ 0, 8, 0],
[ 0, 0, 1]])
combine the six arrays to yield
array([[ 2, 0, 0, 5, 0, 0, 9, 0, 0],
[ 0, 3, 0, 0, 6, 0, 0, 2, 0],
[ 0, 0, 8, 0, 0, 7, 0, 0, 4],
[ 7, 0, 0, 2, 0, 0, 4, 0, 0],
[ 0, 1, 0, 0, 6, 0, 0, 8, 0],
[ 0, 0, 9, 0, 0, 3, 0, 0, 1]])
as in
array([[ T1, T2, T3],
[ T4, T5, T6]])
*The six arrays are not needed in themselves as separate arrays, only the final array is needed. I've just chosen this route as it makes it a bit more apparent what the final one consists of.
It can be done with einsum:
ABC = np.array((a,b,c))
i,j,k = ABC.shape
out = np.zeros((i*j,i*k),ABC.dtype)
np.einsum("jiki->ijk",out.reshape(j,i,k,i))[...] = ABC
out
# array([[2, 0, 0, 5, 0, 0, 9, 0, 0],
# [0, 3, 0, 0, 6, 0, 0, 2, 0],
# [0, 0, 8, 0, 0, 7, 0, 0, 4],
# [7, 0, 0, 2, 0, 0, 4, 0, 0],
# [0, 1, 0, 0, 6, 0, 0, 8, 0],
# [0, 0, 9, 0, 0, 3, 0, 0, 1]])
Explanation:
What does the reshape do?
axis 2 (size k)
/-----------------------\
axis 3 (size i)
/-----\ /-----\ /-----\
a s / a s / [[2, 0, 0, 5, 0, 0, 9, 0, 0],
x i | x i | [0, 3, 0, 0, 6, 0, 0, 2, 0],
i z | i z \ [0, 0, 8, 0, 0, 7, 0, 0, 4],
s e | s e / [7, 0, 0, 2, 0, 0, 4, 0, 0],
| | [0, 1, 0, 0, 6, 0, 0, 8, 0],
0 j \ 1 i \ [0, 0, 9, 0, 0, 3, 0, 0, 1]]
It isolates the 3x3 diagonal matrices into axes 1,3.
What does einsum do here?
It maps the axes of the reshaped out to those of ABC;
"jiki->ijk" means that axis 0 ("j") maps to axis 1, axes 1 and 3 ("i") map to axis 0, and axis 2 ("k") maps to axis 2.
Mapping two axes to one (as with "i") has the special meaning of taking the diagonal.
einsum creates a writeable view, so all that's left to do is assigning ABC to that.
Note: that we use the same letters i,j,k for the shape and for the einsum spec doesn't syntactically mean anything, it just makes the thing a lot more readable.
We can combine the 3 arrays with stack (or np.array):
In [65]: a = np.array([[2,5,9], [7,2,4]])
...: b = np.array([[3,6,2], [1,6,8]])
...: c = np.array([[8,7,4], [9,3,1]])
In [66]: abc = np.stack((a,b,c))
In [67]: abc.shape
Out[67]: (3, 2, 3)
One 'column' of abc is one of your diagonals:
In [68]: abc[:,0,0]
Out[68]: array([2, 3, 8])
Make a target array to hold all 6 diagonals:
In [69]: TT = np.zeros((6,3,3),int)
We can then set one diagonal with:
In [70]: idx=np.arange(3)
In [71]: TT[0,idx,idx] = abc[:,0,0]
In [72]: TT
Out[72]:
array([[[2, 0, 0],
[0, 3, 0],
[0, 0, 8]],
...
To set all 6 we need an array that matches this shape:
In [74]: TT[:,idx,idx].shape
Out[74]: (6, 3)
Reshape abc. The result is (3,6). Transpose to make a (6,3):
In [75]: abc.reshape(3,6)
Out[75]:
array([[2, 5, 9, 7, 2, 4],
[3, 6, 2, 1, 6, 8],
[8, 7, 4, 9, 3, 1]])
In [76]: TT[:,idx,idx] = abc.reshape(3,6).T
In [77]: TT
Out[77]:
array([[[2, 0, 0],
[0, 3, 0],
[0, 0, 8]],
[[5, 0, 0],
[0, 6, 0],
[0, 0, 7]],
[[9, 0, 0],
[0, 2, 0],
[0, 0, 4]],
[[7, 0, 0],
[0, 1, 0],
[0, 0, 9]],
[[2, 0, 0],
[0, 6, 0],
[0, 0, 3]],
[[4, 0, 0],
[0, 8, 0],
[0, 0, 1]]])
Rearrange elements with reshapes and transpose:
In [82]: TT.reshape(2,3,3,3).transpose(0,2,1,3).reshape(6,9)
Out[82]:
array([[2, 0, 0, 5, 0, 0, 9, 0, 0],
[0, 3, 0, 0, 6, 0, 0, 2, 0],
[0, 0, 8, 0, 0, 7, 0, 0, 4],
[7, 0, 0, 2, 0, 0, 4, 0, 0],
[0, 1, 0, 0, 6, 0, 0, 8, 0],
[0, 0, 9, 0, 0, 3, 0, 0, 1]])
I came up that, step by step. You may want to recreate those steps for yourself. I won't take up the space here.
There may be more direct ways of creating this, but I think the steps are instructive.
Here is one way to do that with advanced indexing:
import numpy as np
a = np.array([[2, 5, 9], [7, 2, 4]])
b = np.array([[3, 6, 2], [1, 6, 8]])
c = np.array([[8, 7, 4], [9, 3, 1]])
# Put all input arrays together
abc = np.stack([a, b, c])
# Works with any shape and number of arrays
n, r, c = abc.shape
# Row and column index grid
ii, jj = np.ogrid[:r, :c]
# Shift row and column indices over submatrices of result
idx = np.arange(n)[:, np.newaxis, np.newaxis]
row_idx = ii * n + idx
col_idx = jj * n + idx
# Broadcast indices
row_idx, col_idx = np.broadcast_arrays(row_idx, col_idx)
# Make output
out = np.zeros((n * r, n * c), abc.dtype)
out[row_idx, col_idx] = abc
print(out)
# [[2 0 0 5 0 0 9 0 0]
# [0 3 0 0 6 0 0 2 0]
# [0 0 8 0 0 7 0 0 4]
# [7 0 0 2 0 0 4 0 0]
# [0 1 0 0 6 0 0 8 0]
# [0 0 9 0 0 3 0 0 1]]
I am unsure as to why you would need to do this, but I believe that I have answered your question anyway. The code is roughly commented, and the variable names are slightly odd, however, it does what you wanted it to do and it does it in the way you suggested above. The code is not very efficient or fast, though it could be cleaned up and made much faster. It takes the arrays you want to convert into the larger output array, makes them the diagonals of 6 3x3 arrays, and then inserts them into the required spot in the output array.
# Import numpy
import numpy as np
# Create your arrays
a = np.array([[2,5,9], [7,2,4]])
b = np.array([[3,6,2], [1,6,8]])
c = np.array([[8,7,4], [9,3,1]])
# Make them into a list
abc = []
abc.append(a)
abc.append(b)
abc.append(c)
# Create an array that will contain T1, T2, ...
arrays = []
for i in range(6):
arr = np.ndarray(shape=(3, 3))
# Fill the array with zeros
for x in range(3):
for y in range(3):
arr[x][y] = 0
for j in range(3):
arr[j][j] = abc[j][0 if i < 3 else 1][i % 3]
arrays.append(arr)
# Combine the arrays into one, in the way specified
bigarr = np.ndarray(shape=(6, 9))
offsetX = 0
offsetY = 0
arr = 0
# Loop over all of the arrays (T1, T2, etc.)
for arr in range(6):
for i in range(3):
for j in range(3):
bigarr[i + offsetX][j + offsetY] = arrays[arr][i][j]
# Offset the place the arrays will be inserted
offsetY += 3
if offsetY >= 9:
offsetY = 0
offsetX += 3
# The final output is bigarr
print(bigarr)
I hope this answers your question, and if not helps you find another answer.

How to print a matrix partialy in python

I have a numeric square matrix, and want to print only a part of the data like:
I have:
[1, 2, 3, 0, 0]
[6, 7, 2, 0, 0]
[4, 5, 8, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
And I want to print just:
[1, 2, 3]
[6, 7, 2]
[4, 5, 8]
Is that possible?
Assuming you're using NumPy (and you should be), just slice it:
print a[:3, :3]

How do I add a guard ring to a matrix in NumPy?

Using NumPy, a matrix A has n rows and m columns, and I want add a guard ring to matrix A. That guard ring is all zero.
What should I do? Use Reshape? But the element is not enough to make a n+1 m+1 matrix.
Or etc.?
Thanks in advance
I mean an extra ring of cells that always contain 0 surround matrix A.Basically there is a Matrix B has n+2rows m+2columns where the first row and columns and the last row and columns are all zero,and the rest of it are same as matrix A.
Following up on your comment:
>>> import numpy
>>> a = numpy.array(range(9)).reshape((3,3))
>>> b = numpy.zeros(tuple(s+2 for s in a.shape), a.dtype)
>>> b[tuple(slice(1,-1) for s in a.shape)] = a
>>> b
array([[0, 0, 0, 0, 0],
[0, 0, 1, 2, 0],
[0, 3, 4, 5, 0],
[0, 6, 7, 8, 0],
[0, 0, 0, 0, 0]])
This is a less general but easier to understand version of Alex's answer:
>>> a = numpy.array(range(9)).reshape((3,3))
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> b = numpy.zeros(a.shape + numpy.array(2), a.dtype)
>>> b
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
>>> b[1:-1,1:-1] = a
>>> b
array([[0, 0, 0, 0, 0],
[0, 0, 1, 2, 0],
[0, 3, 4, 5, 0],
[0, 6, 7, 8, 0],
[0, 0, 0, 0, 0]])
This question is ancient now, but I just want to alert people finding it that numpy has a function pad that very easily accomplishes this now.
import numpy as np
a = np.array(range(9)).reshape((3, 3))
a
Out[15]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
a = np.pad(a, pad_width=((1,1),(1,1)), mode='constant', constant_values=0)
a
Out[16]:
array([[0, 0, 0, 0, 0],
[0, 0, 1, 2, 0],
[0, 3, 4, 5, 0],
[0, 6, 7, 8, 0],
[0, 0, 0, 0, 0]])

Categories