Related
For the program I am writing my goal is to call a function, give it 2 values and then have it spit back lists based on those 2 numbers. Here's what I have so far,
import numpy as np
def list_maker (n, m):
for n in range(n):
l = list(np.random.randint(1,9, m))
print(l)
My goal is to type "list_maker(3,5)" and have it output 3 lists, each with 5 elements. I want to keep using numpy so I can learn more about it rather than another type of operation. Whenever I call the function my out it,
list_maker(3,5)
[2, 7, 1, 5, 6]
[8, 5, 1, 3, 5]
[8, 2, 6, 3, 7]
However, I can not specifically change one element in one list, if I do l[0] = "Blank", all the elements at 0 position turn to blank and I can't do [0],[1]....
Any idea how to get an output like,
list_maker(3,5)
[[2, 7, 1, 5, 6],
[8, 5, 1, 3, 5],
[8, 2, 6, 3, 7]]
Where I can then specifically edit one element in one of the lists done by numpy?
Thank you for all the replies!
you want to return a list of lists. A simple list comprehension would work:
import numpy as np
def list_maker (n, m):
return [list(np.random.randint(1,9, m)) for _ in range(n)]
then:
>>> list_maker(3,5)
[[1, 7, 2, 5, 7], [3, 5, 5, 7, 7], [8, 5, 1, 1, 1]]
At the moment your function is just printing the lists and not returning them.
And I'm not entirely sure of your intent; at the moment you're not creating a numpy array, but a list. More specifically, you're creating a list of 3 lists, not 3 separate lists.
You could create a numpy array by passing the n, m values directly to numpy's randint:
np.random.randint(1,9, size=(n, m))
I have two arrays of different sizes that I want to perform logical functions on in a for loop. For example, if I have:
array1 = [6,7,8]
array2 = [1,2,3,4,5]
I want to do each element from array1 minus each element of array2 to get something like
[6-1,6-2,6-3,6-4,6-5],[7-1,7-2,7-3,7-4,7-5],[8-1,8-2,8-3,8-4,8-5]
which is subtracting each element from array2 from each element of array1
So i tried to do a for loop like:
for i in range(len(array1)):
ar = array1[i]-array2
and also
for i in range(len(array1)):
for j in range(len(array2)):
ar = array1[i]-array2[j]
But neither of these seem to be working
The first way seems to be returning an array of the right shape but certainly not the right values.
I'd like it to be returned in separate arrays because in reality, I have a very large sample size.
We can solve this using itertools.product
from itertools import product
from operator import sub
final = []
for item in array1:
prods = product([item], array2)
subs = [sub(*p) for p in prods]
final.append(subs)
print(final)
# [[5, 4, 3, 2, 1], [6, 5, 4, 3, 2], [7, 6, 5, 4, 3]]
How it works is product returns the cartesian product of the two arrays in the form of tuples, so:
(6, 1), (6, 2), etc....
Then we simply apply the sub function to each tuple using a list-comprehension.
The following solution should work using a list comprehension:
result = []
for value1 in array1:
result.append([value1 - value2 for value2 in array2])
You could even write this in 1 line using both for loops for the list comprehension:
result = [[value1 - value2 for value2 in array2] for value1 in array1]
Loops solution:
array1 = [6, 7, 8]
array2 = [1, 2, 3, 4, 5]
arr = []
for i in array1:
tmp = []
for j in array2:
tmp.append(i - j)
arr.append(tmp)
print(arr)
Output:
[[5, 4, 3, 2, 1], [6, 5, 4, 3, 2], [7, 6, 5, 4, 3]]
Your for loop is nearly correct except, you overwrite the value of ar every iteration (and you make it slightly more complicated with range)
You can achieve this through list comprehension
[[i - j for j in array2] for i in array1]
# [[5, 4, 3, 2, 1], [6, 5, 4, 3, 2], [7, 6, 5, 4, 3]]
You were on the right track but you had your arrays switched. You want array1 inside the inner loop to perform the operation you want to perform:
array1 = [6,7,8]
array2 = [1,2,3,4,5]
finalarray=[]
for i in range(len(array2)):
for j in range(len(array1)):
ar = array1[j]-array2[i]
finalarray.append(ar)
print(finalarray)
>>>[5, 6, 7, 4, 5, 6, 3, 4, 5, 2, 3, 4, 1, 2, 3]
ar is not being treated properly in your code, it needs to be an array of arrays (matrix) and you'll need another variable to hold the result per iteration, there's probably a better way to do this using list comprehensions but here is one way:
array1 = [6,7,8]
array2 = [1,2,3,4,5]
ar = []
row = []
for i in range(len(array1)):
for j in range(len(array2)):
row.append(array1[i]-array2[j])
ar.append(row)
row = []
print ar
There are plenty of good answers here, but another option especially useful for larger arrays is to use numpy, a package designed for moving large arrays of numbers efficiently. One possible answer in numpy would be this:
import numpy as np
array1 = np.arange(6,9) # make arrays 1 and 2
array2 = np.arange(1,6)
output = (array1.repeat(array2.shape[0]) # repeat array1 as many times as there are elements in array2
.reshape(array1.shape[0], -1) # reshape so we have a row for each element in array1
) - array2 # subtract array2 from each row
output
array([[5, 4, 3, 2, 1],
[6, 5, 4, 3, 2],
[7, 6, 5, 4, 3]])
What can I change so that I will get a list of only the first row?
i.e
0 1
1 2
2 3
3 4
I have tried the below:
matrix = [[1, 2, 3, 4],
[3, 5, 7, 9],
[4, 6, 8, 10],
[5, 7, 9, 11]]
for index in range(len(matrix)):
print(index,matrix[index][0])
0 1
1 3
2 4
3 5
You need:
print(index,matrix[0][index])
instead of:
print(index,matrix[index][0])
This is because the first index is associated with the outer list (the rows). matrix[index] returns an entire row, and then slicing it further returns elements from that row.
You should also change:
for index in range(len(matrix)):
to:
for index in range(len(matrix[0])):
for the same reason. Since it's a square matrix, it'll work out either way, but that's just luck. Actually, it'd be best to just do this instead for simplicity:
for i, e in enumerate(matrix[0]):
print(i, e)
On each iteration of the loop, enumerate() yields a tuple consisting of the index and the element together. If you don't need the index, you could further simplify to:
for e in matrix[0]:
print(e)
You typically only need to use range() if you aren't already starting with a list (or something else that's iterable).
This should work:
matrix = [[1, 2, 3, 4],
[3, 5, 7, 9],
[4, 6, 8, 10],
[5, 7, 9, 11]]
for index in range(len(matrix)):
print(index,matrix[0][index])
You were trying to get the first element from each individual list, the code above will get all elements from the first list.
Assuming your matrix is a list of rows:
print(matrix[0])
Or if you want one item per line:
for value in matrix[0]:
print(value)
And if you want the index:
for index, value in enumerate(matrix[0]):
print(index, value)
matrix = [
[1, 2, 3, 4],
[3, 5, 7, 9],
[4, 6, 8, 10],
[5, 7, 9, 11]
]
matrix[0] gives you the first row, just iterate over it like this:
# ONLY ITERATING OVER THE FIRST ROW
for item in matrix[0]:
print(item)
Say I have a matrix A = [a_1,a_2,...,a_n]. Each column a_i belongs to a class. All classes are from 1 to K. All n column's labels are stored in one n-dim vector b.
Now for each class i, I need to sum all vectors in class i together and put the result vector as the i-th column of a new matrix. So the new matrix has K columns, same number of rows as A.
I know nonzero() can help me get index corresponding to one same label. But I don't know how to write everything without loop. I'm actually working on a large matrix. So using any "for" loop will definitely ruin the efficiency.
Example as following:
A = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
label = [1,2,3,1,2,3]
New matrix = [[1+4,2+5,3+6],
[7+10,8+11,9+12]]
K=3, n=6
One way to avoid loops is to use map.
First create a func to reduce a list.
def red(l):
return list(map(lambda x: x[0] + x[1], zip(l[:3], l[3:])))
A = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
Then apply map to the list of lists.
>>> list(map(red, A))
[[5, 7, 9], [17, 19, 21]]

I do not know why you are not able to use loops but here is the solution
import numpy as np
def masksum(v,vmask,elemn):
vmask = list(map(lambda x: 1 if x == elemn else 0, vmask))
a = np.array(v)
return np.sum(a * vmask)
def mysum(v1,vmask):
norepeat = list(set(vmask))
return list(map(lambda elemn: masksum(v1,vmask,elemn),norepeat))
A = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
label = [1,2,3,1,2,3]
result = list(map(lambda vectori: mysum(vectori,label), A))
print(result)
label is like a mask you can try with [1,3,3,1,3,3] or [4,4,4,5,5,5] etc and the code will works
Does anybody know how to extract a column from a multi-dimensional array in Python?
>>> import numpy as np
>>> A = np.array([[1,2,3,4],[5,6,7,8]])
>>> A
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> A[:,2] # returns the third columm
array([3, 7])
See also: "numpy.arange" and "reshape" to allocate memory
Example: (Allocating a array with shaping of matrix (3x4))
nrows = 3
ncols = 4
my_array = numpy.arange(nrows*ncols, dtype='double')
my_array = my_array.reshape(nrows, ncols)
Could it be that you're using a NumPy array? Python has the array module, but that does not support multi-dimensional arrays. Normal Python lists are single-dimensional too.
However, if you have a simple two-dimensional list like this:
A = [[1,2,3,4],
[5,6,7,8]]
then you can extract a column like this:
def column(matrix, i):
return [row[i] for row in matrix]
Extracting the second column (index 1):
>>> column(A, 1)
[2, 6]
Or alternatively, simply:
>>> [row[1] for row in A]
[2, 6]
If you have an array like
a = [[1, 2], [2, 3], [3, 4]]
Then you extract the first column like that:
[row[0] for row in a]
So the result looks like this:
[1, 2, 3]
check it out!
a = [[1, 2], [2, 3], [3, 4]]
a2 = zip(*a)
a2[0]
it is the same thing as above except somehow it is neater
the zip does the work but requires single arrays as arguments, the *a syntax unpacks the multidimensional array into single array arguments
>>> x = arange(20).reshape(4,5)
>>> x array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
if you want the second column you can use
>>> x[:, 1]
array([ 1, 6, 11, 16])
If you have a two-dimensional array in Python (not numpy), you can extract all the columns like so,
data = [
['a', 1, 2],
['b', 3, 4],
['c', 5, 6]
]
columns = list(zip(*data))
print("column[0] = {}".format(columns[0]))
print("column[1] = {}".format(columns[1]))
print("column[2] = {}".format(columns[2]))
Executing this code will yield,
>>> print("column[0] = {}".format(columns[0]))
column[0] = ('a', 'b', 'c')
>>> print("column[1] = {}".format(columns[1]))
column[1] = (1, 3, 5)
>>> print("column[2] = {}".format(columns[2]))
column[2] = (2, 4, 6)
def get_col(arr, col):
return map(lambda x : x[col], arr)
a = [[1,2,3,4], [5,6,7,8], [9,10,11,12],[13,14,15,16]]
print get_col(a, 3)
map function in Python is another way to go.
array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
col1 = [val[1] for val in array]
col2 = [val[2] for val in array]
col3 = [val[3] for val in array]
col4 = [val[4] for val in array]
print(col1)
print(col2)
print(col3)
print(col4)
Output:
[1, 5, 9, 13]
[2, 6, 10, 14]
[3, 7, 11, 15]
[4, 8, 12, 16]
[matrix[i][column] for i in range(len(matrix))]
The itemgetter operator can help too, if you like map-reduce style python, rather than list comprehensions, for a little variety!
# tested in 2.4
from operator import itemgetter
def column(matrix,i):
f = itemgetter(i)
return map(f,matrix)
M = [range(x,x+5) for x in range(10)]
assert column(M,1) == range(1,11)
You can use this as well:
values = np.array([[1,2,3],[4,5,6]])
values[...,0] # first column
#[1,4]
Note: This is not working for built-in array and not aligned (e.g. np.array([[1,2,3],[4,5,6,7]]) )
let's say we have n X m matrix(n rows and m columns) say 5 rows and 4 columns
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
To extract the columns in python, we can use list comprehension like this
[ [row[i] for row in matrix] for in range(4) ]
You can replace 4 by whatever number of columns your matrix has.
The result is
[ [1,5,9,13,17],[2,10,14,18],[3,7,11,15,19],[4,8,12,16,20] ]
I think you want to extract a column from an array such as an array below
import numpy as np
A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
Now if you want to get the third column in the format
D=array[[3],
[7],
[11]]
Then you need to first make the array a matrix
B=np.asmatrix(A)
C=B[:,2]
D=asarray(C)
And now you can do element wise calculations much like you would do in excel.
One more way using matrices
>>> from numpy import matrix
>>> a = [ [1,2,3],[4,5,6],[7,8,9] ]
>>> matrix(a).transpose()[1].getA()[0]
array([2, 5, 8])
>>> matrix(a).transpose()[0].getA()[0]
array([1, 4, 7])
Just use transpose(), then you can get the columns as easy as you get rows
matrix=np.array(originalMatrix).transpose()
print matrix[NumberOfColumns]
Well a 'bit' late ...
In case performance matters and your data is shaped rectangular, you might also store it in one dimension and access the columns by regular slicing e.g. ...
A = [[1,2,3,4],[5,6,7,8]] #< assume this 4x2-matrix
B = reduce( operator.add, A ) #< get it one-dimensional
def column1d( matrix, dimX, colIdx ):
return matrix[colIdx::dimX]
def row1d( matrix, dimX, rowIdx ):
return matrix[rowIdx:rowIdx+dimX]
>>> column1d( B, 4, 1 )
[2, 6]
>>> row1d( B, 4, 1 )
[2, 3, 4, 5]
The neat thing is this is really fast. However, negative indexes don't work here! So you can't access the last column or row by index -1.
If you need negative indexing you can tune the accessor-functions a bit, e.g.
def column1d( matrix, dimX, colIdx ):
return matrix[colIdx % dimX::dimX]
def row1d( matrix, dimX, dimY, rowIdx ):
rowIdx = (rowIdx % dimY) * dimX
return matrix[rowIdx:rowIdx+dimX]
If you want to grab more than just one column just use slice:
a = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
print(a[:, [1, 2]])
[[2 3]
[5 6]
[8 9]]
Despite using zip(*iterable) to transpose a nested list, you can also use the following if the nested lists vary in length:
map(None, *[(1,2,3,), (4,5,), (6,)])
results in:
[(1, 4, 6), (2, 5, None), (3, None, None)]
The first column is thus:
map(None, *[(1,2,3,), (4,5,), (6,)])[0]
#>(1, 4, 6)
I prefer the next hint:
having the matrix named matrix_a and use column_number, for example:
import numpy as np
matrix_a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
column_number=2
# you can get the row from transposed matrix - it will be a column:
col=matrix_a.transpose()[column_number]
All columns from a matrix into a new list:
N = len(matrix)
column_list = [ [matrix[row][column] for row in range(N)] for column in range(N) ]