Initialize empty matrix in Python - python

I am trying to convert a MATLAB code in Python. I don't know how to initialize empty matrix in Python.
MATLAB Code:
demod4(1) = [];
I tried in Python
demod4[0] = array([])
but it gives error:
only length-1 arrays can be converted to Python scalars

If you are using numpy arrays, you initialize to 0, by specifying the expected matrix size:
import numpy as np
d = np.zeros((2,3))
>>> d
[[ 0. 0. 0.]
[ 0. 0. 0.]]
This would be the equivalent of MATLAB 's:
d = zeros(2,3);
You can also initialize an empty array, again using the expected dimensions/size
d = np.empty((2,3))
If you are not using numpy, the closest somewhat equivalent to MATLAB's d = [] (i.e., a zero-size matrix) would be using an empty list and then
append values (for filling a vector)
d = []
d.append(0)
d.append(1)
>>> d
[0, 1]
or append lists (for filling a matrix row or column):
d = []
d.append(range(0,2))
d.append(range(2,4))
>>> d
[[0, 1], [2, 3]]
See also:
initialize a numpy array (SO)
NumPy array initialization (fill with identical values) (SO)
How do I create an empty array and then append to it in NumPy? (SO)
NumPy for MATLAB users

You could use a nested list comprehension:
# size of matrix n x m
matrix = [ [ 0 for i in range(n) ] for j in range(m) ]

What about initializing a list, populating it, then converting to an array.
demod4 = []
Or, you could just populate at initialization using a list comprehension
demod4 = [[func(i, j) for j in range(M)] for i in range(N)]
Or, you could initialize an array of all zeros if you know the size of the array ahead of time.
demod4 = [[0 for j in range(M)] for i in range(N)]
or
demod4 = [[0 for i in range(M)]*N]
Or try using numpy.
import numpy as np
N, M = 100, 5000
np.zeros((N, M))

To init matrix with M rows and N columns you can use following pattern:
M = 3
N = 2
matrix = [[0] * N for _ in range(M)]

If you want to initialize the matrix with 0s then use the below code
# for m*n matrix
matrix = [[0] * m for i in range(n)]

M=[]
n=int(input())
m=int(input())
for j in range(n):
l=[]
for k in range(m):
l.append(0)
M.append(l)
print(M)
This is the traditional way of doing it matrix[m,n], However, python offers many cool ways of doing so as mentioned in other answers.

i find that this method is the easies method to create a matrix
rows = 3
columns = 4
matrix = [[0] * columns] * rows
my output:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
if you want to print the matrix use this:
for i in range(rows):
for j in range(columns):
print(matrix[i][j], end=' ')
print()
my output:
0 0 0 0
0 0 0 0
0 0 0 0

rows = 3
columns = 2
M = [[0]*columns]*rows
Or you could also use '' instead of 0
print(M)
Output:
M = [[0, 0], [0, 0], [0, 0]]

Related

Pad list of arrays with zeros in order all arrays to have the same size

I have created this array(or I think its a list) that consist of many arrays that are different size and that is the reason I put dtype = object .
m = [data[a:b] for a, b in zip(z[0:-1:2], z[1:-1:2])]
array = np.array(m, dtype=object)
I need to pad each array with zero so that they have the same size (lets say size=smax) and become a "proper" array. My definitions are a little off and I am sorry in advance
You can do this using np.pad on each row. For example:
import numpy as np
data = np.arange(10)
z = [0, 2, 1, 4, 6, 10, 8, 9]
m = [data[a:b] for a, b in zip(z[0:-1:2], z[1:-1:2])]
max_length = max(len(row) for row in m)
result = np.array([np.pad(row, (0, max_length-len(row))) for row in m])
print(result)
# [[0 1 0 0]
# [1 2 3 0]
# [6 7 8 9]]

I want to take the XOR of all the elements of 1 list with another. How do I do it? [duplicate]

This question already has answers here:
How do you get the logical xor of two variables in Python?
(28 answers)
Closed 3 years ago.
I have a bunch of lists in the form of say [0,0,1,0,1...], and I want to take the XOR of 2 lists and give the output as a list.
Like:
[ 0, 0, 1 ] XOR [ 0, 1, 0 ] -> [ 0, 1, 1 ]
res = []
tmp = []
for i in Employee_Specific_Vocabulary_Dict['Binary Vector']:
for j in Course_Specific_Vocabulary_Dict['Binary Vector']:
tmp = [i[index] ^ j[index] for index in range(len(i))]
res.append(temp)
The size of each of my lists / vectors is around 3500 elements, so I need something to save time, since this piece of code is taking more than 20 mins to run.
I have 3085 lists, each of which need an XOR operation with 4089 other lists.
How do I do this without iterating through each list explicitly?
Use map:
answer = list(map(operator.xor, lst1, lst2)).
or zip:
answer = [x ^ y for x,y in zip(lst1, lst2)]
If you need something faster, consider using NumPy instead of Python lists to hold your data.
Assuming a and b are the same size you can use the xor operation (i.e. ^) with simple list indexing:
a = [0, 0, 1]
b = [0, 1, 1]
c = [a[index] ^ b[index] for index in range(len(a))]
print(c) # [0, 1, 0]
or you can use zip with the xor:
a = [0, 0, 1]
b = [0, 1, 1]
c = [x ^ y for x, y in zip(a, b)]
print(c) # [0, 1, 0]
zip will only go to the shortest list (if they are not the same size). If they are not the same size and you want to go to the longer list you can use zip_longest:
from itertools import zip_longest
a = [0, 0, 1, 1]
b = [0, 1, 1]
c = [x ^ y for x, y in zip_longest(a, b, fillvalue=0)]
print(c) # [0, 1, 0, 1]
Using numpy you should have some performance gains, the function you need is bitwise_xor, like so:
import numpy as np
results = []
for i in Employee_Specific_Vocabulary_Dict['Binary Vector']:
for j in Course_Specific_Vocabulary_Dict['Binary Vector']:
results.append(np.bitwise_xor(i, j))
A proof of concept:
a = [1,0,0,1,1]
b = [1,1,0,0,1]
x = np.bitwise_xor(a,b)
print("a\tb\tres")
for i in range(len(a)):
print("{}\t{}\t{}".format(a[i], b[i], x[i]))
output:
a b x
1 1 0
0 1 1
0 0 0
1 0 1
1 1 0
Edit
Note that if your arrays have the same size, you can simply do one operation and the bitwise_xor will still work, so:
a = [[1,1,0], [0,0,1]]
b = [[0,1,0], [1,0,1]]
res = np.bitwise_xor(a, b)
will still work, and you'll have:
res: [[1, 0, 0], [1, 0, 0]]
In your case, a workaround would possibily be:
results = []
n = len(Course_Specific_Vocabulary_Dict['Binary Vector'])
for a in Employee_Specific_Vocabulary_Dict['Binary Vector']:
# Get same size array w.r.t Course_Specific_Vocabulary_Dict["Binary Vector]
repeated_a = np.repeat([a], n, axis=0)
results.append(np.bitwise_xor(repeated_a, Course_Specific_Vocabulary_Dict['Binary Vector']))
However I don't know if that would actually improve performance, it is to be checked; for sure it will require some more memory.

values overwrite numpy array

I am quite new to Python. Any help would be appreciated.
ret_val is generating 0 or 1 labels and euclidean distance is genreating distance 0.55 or 0.23..
So what i want is to add those values into a numpy array like:
example
> arr= np.array([[0.55, 0.23], [0.4, 0.6], [0.8, 0.2]])
> arrdist= np.array([[1, 0], [0, 1], [1, 0]])
i want to apply it to my code
output of my code should be :
[[0.7 0.3]
[0.4 0.6]
[0.8 0.2]]
[[1 0]
[0 1]
[1 0]]
but output of my code return only the last values [[0]]
[[37.11052]]
When I try to run the code, values overwrite the array i get only the last element added on the array :
i = 1
for j in range (1,5):
ret_val, euclidean_distance = verifyFace(str(i)+"tst.jpg", str(j)+"train.jpg", epsilon)
if ret_val == '0':
a = 0
print(euclidean_distance)
arr = np.array([[(a)]])
arrdist = np.array([[(euclidean_distance)]])
elif ret_val =='1':
b=1
arr = np.array([[(b)]])
arrdist = np.array([[(euclidean_distance)]])
print(arr)
print(arrdist)
You need to bring arr and arrdist outside of your for loop. Initialize them as empty arrays like so:
i = 1
arr = []
arrdist = []
for j in range (1,5):
...
if ...:
...
arr.append([a])
arrdist.append([euclidean_distance])
...
after your for loop, you convert your list into a numpy array, if you need a numpy array:
arr = np.array(arr)
arrdist = np.array(arrdist)

Creating a special matrix in numpy

[a b c ]
[ a b c ]
[ a b c ]
[ a b c ]
Hello
For my economics course we are suppose to create an array that looks like this. The problem is I am an economist not a programmer. We are using numpy in python. Our professor says college is not preparing us for the real world and wants us to learn programming (which is a good thing). We are not allowed to use any packages and must come up with an original code. Does anybody out there have any idea how to make this matrix. I have spent hours trying codes and browsing the internet looking for help and have been unsuccessful.
This kind of matrix is called a Toeplitz matrix or constant diagonal matrix. Knowing this leads you to scipy.linalg.toeplitz:
import scipy.linalg
scipy.linalg.toeplitz([1, 0, 0, 0], [1, 2, 3, 0, 0, 0])
=>
array([[1, 2, 3, 0, 0, 0],
[0, 1, 2, 3, 0, 0],
[0, 0, 1, 2, 3, 0],
[0, 0, 0, 1, 2, 3]])
The method below fills one diagonal at a time:
import numpy as np
x = np.zeros((4, 6), dtype=np.int)
for i, v in enumerate((6,7,8)):
np.fill_diagonal(x[:,i:], v)
array([[6, 7, 8, 0, 0, 0],
[0, 6, 7, 8, 0, 0],
[0, 0, 6, 7, 8, 0],
[0, 0, 0, 6, 7, 8]])
or you could do the one liner:
x = [6,7,8,0,0,0]
y = np.vstack([np.roll(x,i) for i in range(4)])
Personally, I prefer the first since it's easier to understand and probably faster since it doesn't build all the temporary 1D arrays.
Edit:
Since a discussion of efficiency has come up, it might be worthwhile to run a test. I also included time to the toeplitz method suggested by chthonicdaemon (although personally I interpreted the question to exclude this approach since it uses a package rather than using original code -- also though speed isn't the point of the original question either).
import numpy as np
import timeit
import scipy.linalg as sl
def a(m, n):
x = np.zeros((m, m), dtype=np.int)
for i, v in enumerate((6,7,8)):
np.fill_diagonal(x[:,i:], v)
def b(m, n):
x = np.zeros((n,))
x[:3] = vals
y = np.vstack([np.roll(x,i) for i in range(m)])
def c(m, n):
x = np.zeros((n,))
x[:3] = vals
y = np.zeros((m,))
y[0] = vals[0]
r = sl.toeplitz(y, x)
return r
m, n = 4, 6
print timeit.timeit("a(m,n)", "from __main__ import np, a, b, m, n", number=1000)
print timeit.timeit("b(m,n)", "from __main__ import np, a, b, m, n", number=1000)
print timeit.timeit("c(m,n)", "from __main__ import np, c, sl, m, n", number=1000)
m, n = 1000, 1006
print timeit.timeit("a(m,n)", "from __main__ import np, a, b, m, n", number=1000)
print timeit.timeit("b(m,n)", "from __main__ import np, a, b, m, n", number=1000)
print timeit.timeit("c(m,n)", "from __main__ import np, c, sl, m, n", number=100)
# which gives:
0.03525209 # fill_diagonal
0.07554483 # vstack
0.07058787 # toeplitz
0.18803215 # fill_diagonal
2.58780789 # vstack
1.57608604 # toeplitz
So the first method is about a 2-3x faster for small arrays and 10-20x faster for larger arrays.
This is a simplified tridiagonal matrix. So it is essentially a this question
def tridiag(a, b, c, k1=-1, k2=0, k3=1):
return np.diag(a, k1) + np.diag(b, k2) + np.diag(c, k3)
a = [1, 1]; b = [2, 2, 2]; c = [3, 3]
A = tridiag(a, b, c)
print(A)
Result:
array([[2, 3, 0],
[1, 2, 3],
[0, 1, 2]])
Something along the lines of
import numpy as np
def createArray(theinput,rotations) :
l = [theinput]
for i in range(1,rotations) :
l.append(l[i-1][:])
l[i].insert(0,l[i].pop())
return np.array(l)
print(createArray([1,2,3,0,0,0],4))
"""
[[1 2 3 0 0 0]
[0 1 2 3 0 0]
[0 0 1 2 3 0]
[0 0 0 1 2 3]]
"""
If you care about efficiency, it is hard to beat this:
import numpy as np
def create_matrix(diags, n):
diags = np.asarray(diags)
m = np.zeros((n,n+len(diags)-1), diags.dtype)
s = m.strides
v = np.lib.index_tricks.as_strided(
m,
(len(diags),n),
(s[1],sum(s)))
v[:] = diags[:,None]
return m
print create_matrix(['a','b','c'], 8)
Might be a little over your head, but then again that's good inspiration ;)
Or even better: a solution which has both O(n) storage and runtime requirements, rather than all the other solutions posted thus far, which are O(n^2)
import numpy as np
def create_matrix(diags, n):
diags = np.asarray(diags)
b = np.zeros(len(diags)+n*2, diags.dtype)
b[n:][:len(diags)] = diags
s = b.strides[0]
v = np.lib.index_tricks.as_strided(
b[n:],
(n,n+len(diags)-1),
(-s,s))
return v
print create_matrix(np.arange(1,4), 8)
This is an old question, however some new input can always be useful.
I create tridiagonal matrices in python using list comprehension.
Say a matrix that is symmetric around "-2" and has a "1" on either side:
-2 1 0
Tsym(3) => 1 -2 1
0 1 -2
This can be created using the following "one liner":
Tsym = lambda n: [ [ 1 if (i+1==j or i-1==j) else -2 if j==i else 0 for i in xrange(n) ] for j in xrange(n)] # Symmetric tridiagonal matrix (1,-2,1)
A different case (that several of the other people answering has solved perfectly fine) is:
1 2 3 0 0 0
Tgen(4,6) => 0 1 2 3 0 0
0 0 1 2 3 0
0 0 0 1 2 3
Can be made using the one liner shown below.
Tgen = lambda n,m: [ [ 1 if i==j else 2 if i==j+1 else 3 if i==j+2 else 0 for i in xrange(m) ] for j in xrange(n)] # General tridiagonal matrix (1,2,3)
Feel free to modify to suit your specific needs. These matrices are very common when modelling physical systems and I hope this is useful to someone (other than me).
Hello since your professor asked you not to import any external package, while most answers use numpy or scipy.
You better use only python List to create 2D array (compound list), then populate its diagonals with the items you wish, Find the code below
def create_matrix(rows = 4, cols = 6):
mat = [[0 for col in range(cols)] for row in range(rows)] # create a mtrix filled with zeros of size(4,6)
for row in range(len(mat)): # gives number of lists in the main list,
for col in range(len(mat[0])): # gives number of items in sub-list 0, but all sublists have the same length
if row == col:
mat[row][col] = "a"
if col == row+1:
mat[row][col] = "b"
if col == row+2:
mat[row][col] = "c"
return mat
create_matrix(4, 6)
[['a', 'b', 'c', 0, 0, 0],
[0, 'a', 'b', 'c', 0, 0],
[0, 0, 'a', 'b', 'c', 0],
[0, 0, 0, 'a', 'b', 'c']]
Creating Band Matrix
Check out the definition for it in wiki :
https://en.wikipedia.org/wiki/Band_matrix
You can use this function to create band matrices like diagonal matrix with offset=1 or tridiagonal matrix (The one you are asking about) with offset=1 or Pentadiagonal Matrix with offset=2
def band(size=10, ones=False, low=0, high=100, offset=2):
shape = (size, size)
n_matrix = np.random.randint(low, high, shape) if not ones else np.ones(shape,dtype=int)
n_matrix = np.triu(n_matrix, -1*offset)
n_matrix = np.tril(n_matrix, offset)
return n_matrix
In your case you should use this
rand_tridiagonal = band(size=6,offset=1)
print(rand_tridiagonal)

Cummulative matrix multiplication on successive square arrays in an (M,M,N) or similar array?

Say I have N MxM arrays in a single MxMxN array. Is there any simple way in numpy to do a cummulative matrix multiplication of successive MxM arrays (the MxMxN array can be overwritten). I can do it with a loop as shown below but I'm wondering if there is a better way? Note the ordering MxMxN is not special, I could just as easily have NxMxM or something.
import numpy as np
a = np.arange(4).reshape((2,2))
n=3
b = np.dstack((a,)*n)
print(b[:,:,0])
#[b[:,:,k].dot(b[:,:, k - 1], out=b[:,:, k]) for k in range(1, n)]
for k in range(1, n):
b[:,:,k] = np.dot(b[:,:,k], b[:,:,k-1])
print(b[:, :, k])
From which I get the output:
[[0 1]
[2 3]]
[[ 2 3]
[ 6 11]]
[[ 6 11]
[22 39]]
I also tried the following list comprehension which failed:
[b[:,:,k].dot(b[:,:, k - 1], out=b[:,:, k]) for k in range(1, n)]
EDIT:
I'm intersted in all the intermediate results so b[:,:,0], b[:,:,0] x b[0:,:,1], b[:,:,0] x b[0:,:,1] x b[:,:,2], etc. not just the final b[:,:,0] x b[0:,:,1] x ... x b[:,:,N-1]
For a M*M*N array, how about:
reduce(np.dot, np.rollaxis(b, 2))
For Python 3 you need to import reduce from functools.

Categories