I'm trying to create a 2d list with shape of [n,784] (the same shape as the MNIST image batches) using multiple [1,784] lists.
mylist.append(element) doesn't give me what I'm looking for, where mylist is the 2d [n,784] list and element is the [1,784] lists. It would return a list with shape [n,1,784].
I've also tried mylist[index].append(element), and I got a [784] 1d list instead.
Any idea how to solve my problem?
Thanks a lot
import numpy as np
myarray = np.array(mylist)
newarray = np.concatenate((myarray, element))
And if you want to turn it back into a list:
newlist = newarray.tolist()
a = [[1,1],[2,2]]
b = np.concatenate([a, a], axis=1).tolist()
The output will be:
[[1, 1, 1, 1], [2, 2, 2, 2]]
Related
How can I write a numpy function that outputs the minimum of value of the multiple arrays within a and b. What could I add to the list comprehension below to be able to get the expected output.
import numpy as np
a= np.array([[12,2,1,3,45],[23,1,2],[5]], dtype=object)
b= np.array([[12,1,1,2334],[11,121,12]], dtype=object)
MaxDraw = np.array([min(draw) for draw in [a, b]])
Expected Output:
[[1], [1], [5]]
[[1], [11]]
If you use np.array for a list which its elements size are not equal, it will be like:
>>> np.array([[12,2,1,3,45],[23,1,2],[5]])
array([list([12, 2, 1, 3, 45]), list([23, 1, 2]), list([5])], dtype=object)
So basically, numpy function will not work with your array. For your problem, I think a simple solution is enough.
>>> a = [[12,2,1,3,45],[23,1,2],[5]]
>>> [min(mini_list) for mini_list in a]
[1, 1, 5]
As you are using an object dtype (to hold lists of differents sizes), using numpy doesn't really make sense as you will lose the benefit of numpy vector functions.
Anyway, you need:
np.array([[[min(draw)] for draw in l] for l in [a, b]])
output:
array([list([[1], [1], [5]]), list([[1], [11]])], dtype=object)
Is it possible to transform a 1D tensor to a list ?
thank you
l = tf.Variable([0, 1, 2, 3]).numpy().tolist()
type(l)
>>> list
l
>>> [0, 1, 2, 3]
This method lets convert to list not only 1D tensor, but also any other shape
I am trying to append a new row to an existing numpy array in a loop. I have tried the methods involving append, concatenate and also vstack none of them end up giving me the result I want.
I have tried the following:
for _ in col_change:
if (item + 2 < len(col_change)):
arr=[col_change[item], col_change[item + 1], col_change[item + 2]]
array=np.concatenate((array,arr),axis=0)
item+=1
I have also tried it in the most basic format and it still gives me an empty array.
array=np.array([])
newrow = [1, 2, 3]
newrow1 = [4, 5, 6]
np.concatenate((array,newrow), axis=0)
np.concatenate((array,newrow1), axis=0)
print(array)
I want the output to be [[1,2,3][4,5,6]...]
The correct way to build an array incrementally is to not start with an array:
alist = []
alist.append([1, 2, 3])
alist.append([4, 5, 6])
arr = np.array(alist)
This is essentially the same as
arr = np.array([ [1,2,3], [4,5,6] ])
the most common way of making a small (or large) sample array.
Even if you have good reason to use some version of concatenate (hstack, vstack, etc), it is better to collect the components in a list, and perform the concatante once.
If you want [[1,2,3],[4,5,6]] I could present you an alternative without append: np.arange and then reshape it:
>>> import numpy as np
>>> np.arange(1,7).reshape(2, 3)
array([[1, 2, 3],
[4, 5, 6]])
Or create a big array and fill it manually (or in a loop):
>>> array = np.empty((2, 3), int)
>>> array[0] = [1,2,3]
>>> array[1] = [4,5,6]
>>> array
array([[1, 2, 3],
[4, 5, 6]])
A note on your examples:
In the second one you forgot to save the result, make it array = np.concatenate((array,newrow1), axis=0) and it works (not exactly like you want it but the array is not empty anymore). The first example seems badly indented and without know the variables and/or the problem there it's hard to debug.
I want to keep adding numpy arrays to another array in python.
let's say I have the following arrays:
arraytotal = np.array([])
array1 = np.array([1,1,1,1,1])
array2 = np.array([2,2,2,2,2])
and I want to append array1 and array2 into arraytotal. However, when I use:
arraytotal.append[array1]
it tells me:
'numpy.ndarray' object has no attribute 'append'
how can I append array1 and array2 into arraytotal?
Unfortunately, there is no way to manipulate arrays quite like that. Instead, make a list with the same name, and append the two arrays and change it to a numpy array like so:
arraytotal[]
array1 = np.array([1,1,1,1,1])
arraytotal.append[array1]
np.array(arraytotal)
You could use np.concatenate() like this :
arraytotal = np.concatenate(([array1], [array2]))
This results in the following 2D array.
array([[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2]])
Hope this is what you were looking for.
You should append the arrays onto a regular python list and then convert the list to a numpy array at the end:
import numpy as np
total = []
for i in range(5,15):
thisArray = np.arange(i)
total.append(thisArray)
total = np.asarray(total)
That loop makes a 2D array; you'd nest loops to produce higher dimensional arrays.
I'll try to be as clear as possible, and I'll start by explaining why I want to transform two arrays into a matrix.
To plot the performance of a portfolio vs an market index I need a data structure like in this format:
[[portfolio_value1, index_value1]
[portfolio_value2, index_value2]]
But I have the the data as two separate 1-D arrays:
portfolio = [portfolio_value1, portfolio_value2, ...]
index = [index_value1, index_value2, ...]
So how do I transform the second scenario into the first. I've tried np.insert to add the second array to a test matrix I had in a python shell, my problem was to transpose the first array into a single column matrix.
Any help on how to achieve this without an imperative loop would be great.
The standard numpy function for what you want is np.column_stack:
>>> np.column_stack(([1, 2, 3], [4, 5, 6]))
array([[1, 4],
[2, 5],
[3, 6]])
So with your portfolio and index arrays, doing
np.column_stack((portfolio, index))
would yield something like:
[[portfolio_value1, index_value1],
[portfolio_value2, index_value2],
[portfolio_value3, index_value3],
...]
Assuming lengths of portfolio and index are the same:
matrix = []
for i in range(len(portfolio)):
matrix.append([portfolio[i], index[i]])
Or a one-liner using list comprehension:
matrix2 = [[portfolio[i], index[i]] for i in range(len(portfolio))]
You can use np.c_
np.c_[[1,2,3], [4,5,6]]
It will give you:
np.array([[1,4], [2,5], [3,6]])
Simple you can try this
a=list(zip(portfolio, index))
You can try the below incase you cant use numpy
Zip wont work for lists of diff length and it returns a tuple and not a list
class Matrix:
def __init__(self, list1, list2):
self.list1 = list1
self.list2 = list2
def get_mix(self,list1,list2):
matrix = []
for elem_one in list1:
for elem_two in list2 :
if elem_two:
last_elem = elem_two
matrix.append([elem_one,elem_two])
else :
matrix.append([elem_one,last_elem])
return matrix
def get_matrix(self):
returnlist = []
if len(self.list1) == len(self.list2):
for elem_one in self.list1:
for elem_two in self.list2:
returnlist.append([elem_one,elem_two])
return returnlist
elif len(self.list1) > len(self.list2):
return self.get_mix(self.list1,self.list2)
elif len(self.list1) < len(self.topplist2ings):
return self.get_mix(self.list2,self.list1)