Populating a 2D array to calculate a function of two linspaces - python

I have this set of equations I want to perform:
x = np.linspace(0, 2, 3)
y = np.linspace(x, x+2, 3)
I then want to populate the 2D array with a calculation that does:
a = 2*x + y
So for example, given an array:
x = [0, 1, 2]
Then, the array y is:
y = [[0, 1, 2],
[1, 2, 3],
[2, 3, 4]]
When I perform the operation a = 2*x + y I should get the array:
a = [[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]
How do I do this, keeping in mind I want to perform this operation quickly for array of size up to 10000x10000 (or larger)?

Or do your code adding two Ts:
print((2*x+y.T).T)
Output:
[[0 1 2]
[3 4 5]
[6 7 8]]

Related

Find the row index number of an array in a 2D numpy array

If I have a 2D numpy array A:
[[6 9 6]
[1 1 2]
[8 7 3]]
And I have access to array [1 1 2]. Clearly, [1 1 2] belongs to index 1 of array A. But how do I do this?
Access the second row using the following operator:
import numpy as np
a = np.array([[6, 9, 6],
[1, 1, 2],
[8, 7, 3]])
row = [1, 1, 2]
i = np.where(np.all(a==row, axis=1))
print(i[0][0])
np.where will return a tuple of indices (lists), which is why you need to use the operators [0][0] consecutively in order to obtain an int.
One option:
a = np.array([[6, 9, 6],
[1, 1, 2],
[8, 7, 3]])
b = np.array([1, 1, 2])
np.nonzero((a == b).all(1))[0]
output: [1]
arr1 = [[6,9,6],[1,1,2],[8,7,3]]
ind = arr1.index([1,1,2])
Output:
ind = 1
EDIT for 2D np.array:
arr1 = np.array([[6,9,6],[1,1,2],[8,7,3]])
ind = [l for l in range(len(arr1)) if (arr1[l,:] == np.array([1,1,2])).all()]
import numpy as np
a = np.array([[6, 9, 6],
[1, 1, 2],
[8, 7, 3]])
b = np.array([1, 1, 2])
[x for x,y in enumerate(a) if (y==b).all()] # here enumerate will keep the track of index
#output
[1]

finding every squares value in array

I have different sizes of arrays that each element is its index if it was flatten. Is there a way to print out every element per square going clockwise? I thought about slicing the arrays but that doesn't go clockwise and only prints one square and not all.
arr1 = np.array([[0, 1],[2, 3]])
arr2 = np.array([[0, 1, 2],[3, 4, 5]])
arr3 = np.array([[0, 1],[2, 3],[4, 5]])
print(arr1[0:2,0:2])
print()
print(arr2[0:2,0:2])
print()
print(arr3[0:2,0:2])
output:
[[0 1]
[2 3]]
[[0 1]
[3 4]]
[[0 1]
[2 3]]
Maybe this helps
import numpy as np
a = np.random.randint(0, 10, size=(7, 9))
print(a)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
x = a[i:i+2, j:j+2]
if x.flatten().size == 4:
print(x) # every 2 by 2 array of 4 elements
m = x.copy() # copy x so not to be changed!
m[1] = m[1][::-1] # reverse row 1 elements
print(m.flatten()) # 1d array clockwise
from numpy.lib.stride_tricks import sliding_window_view
def zay_117(arr):
output = []
for row in sliding_window_view(arr, window_shape=(2,2)):
for sq in row:
output.append(np.hstack((sq[0, 0:2], sq[1, 0:2][::-1])).tolist())
return output
# zay_117(arr1)
# [[0, 1, 3, 2]]
# zay_117(arr2)
# [[0, 1, 4, 3], [1, 2, 5, 4]]
# zay_117(arr3)
# [[0, 1, 3, 2], [2, 3, 5, 4]]

How do I create an nxn array of m-m arrays?

I want to create an array made of arrays in Python with numpy
I'm trying to calcule the inverse of a matrix made by some other matrix using numpy method linalg.inv() but it calculates one inverse for each submatrix instead of a general inverse
for example, lets say I have:
a = np.array([[1, 2],
[3, 4]])
b = np.array([[5, 6],
[7, 8]])
i = np.array([[1, 0],
[0, 1]])
what I've tried is:
c = np.array([[a, i],
[i, b]])
what I want is
>> [[1, 2, 1, 0]
[3, 4, 0, 1]
[1, 0, 5, 6]
[0, 1, 7, 8]]
what I get is
>> [[[[1 2]
[3 4]]
[[1 0]
[0 1]]]
[[[1 0]
[0 1]]
[[5 6]
[7 8]]]]
You can use the np.block function, which can be used to assemble a block of matrices. You can do something like this,
np.block([[a,i],[i,b]])

Vectorizing this for-loop in numpy

I was wondering how I would vectorize this for loop. Given a 2x2x2 array x and an array where each element is the ith, jth, and kth element of the array I want to get x[i,j,k]
Given an arrays x and y
x = np.arange(8).reshape((2, 2, 2))
y = [[0, 1, 1], [1, 1, 0]]
I want to get:
x[0, 1, 1] = 3 and x[1, 1, 0] = 6
I tried:
print(x[y])
But it prints:
array([[2, 3],
[6, 7],
[4, 5]])
So I ended up doing:
for y_ in y:
print(x[y_[0], y_[1], y_[2]])
Which works, but I can't help but think there is a better way.
Use transposed y i.e zip(*y) as the index; You need to have the indices for each dimension as an element for advanced indexing to work:
x[tuple(zip(*y))]
# array([3, 6])

Numpy: calculate edges of a matrix

I have the following to calculate the difference of a matrix, i.e. the i-th element - the (i-1) element.
How can I (easily) calculate the difference for each element horizontally and vertically? With a transpose?
inputarr = np.arange(12)
inputarr.shape = (3,4)
inputarr+=1
#shift one position
newarr = list()
for x in inputarr:
newarr.append(np.hstack((np.array([0]),x[:-1])))
z = np.array(newarr)
print inputarr
print 'first differences'
print inputarr-z
Output
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
first differences
[[1 1 1 1]
[5 1 1 1]
[9 1 1 1]]
Check out numpy.diff.
From the documentation:
Calculate the n-th order discrete difference along given axis.
The first order difference is given by out[n] = a[n+1] - a[n] along
the given axis, higher order differences are calculated by using diff
recursively.
An example:
>>> import numpy as np
>>> a = np.arange(12).reshape((3,4))
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> np.diff(a,axis = 1) # row-wise
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
>>> np.diff(a, axis = 0) # column-wise
array([[4, 4, 4, 4],
[4, 4, 4, 4]])

Categories