I have managed to load images in a folder using the command line sklearn: load_sample_images()
I would now like to convert it to a numpy.ndarray format with float32 datatype
I was able to convert it to np.ndarray using : np.array(X), however np.array(X, dtype=np.float32) and np.asarray(X).astype('float32') give me the error:
ValueError: setting an array element with a sequence.
Is there a way to work around this?
from sklearn_theano.datasets import load_sample_images
import numpy as np
kinect_images = load_sample_images()
X = kinect_images.images
X_new = np.array(X) # works
X_new = np.array(X[1], dtype=np.float32) # works
X_new = np.array(X, dtype=np.float32) # does not work
If you have a list of lists, you only needed to use ...
import numpy as np
...
npa = np.asarray(someListOfLists, dtype=np.float32)
per this LINK in the scipy / numpy documentation. You just needed to define dtype inside the call to asarray.
If you're converting a list into an array, you'll need to make a new copy anyway, so
arr = np.array(my_list, dtype='float32')
also works.
One use case is when my_list is not actually a list but some other list-like object; in which case explicitly casting to list beforehand might be helpful.
arr = np.array(list(my_list), dtype='float32')
For example,
my_list = pd.Series([[1], [2], [3]]).values
np.array(my_list) # jagged array; not OK
np.array(list(my_list)) # OK
np.array(my_list.tolist()) # OK
my_list = {'a': [1], 'b': [2]}.values()
np.array(my_list) # jagged array; not OK
np.array(list(my_list)) # OK
To cast nested list into an array, the shapes of the sublists must match. If they don't maybe you want to concatenate the sublists along some axis. Try np.concatenate/np.r_/np.c_ etc. instead.
Related
This question already has answers here:
Good ways to "expand" a numpy ndarray?
(6 answers)
Closed 1 year ago.
I have a numpy array with dimensions (1316, 21) and I need to increase it to (1329, 21). It doesn't matter what values are stored in the added space at the end. I tried to do:
x = np.append(x, np.zeros(13))
But that changes the dimensions of the array to (27649,) which shows that it is converting it into a one dimensional array then adding the zeros to the end.
How do I append empty 2 dimensional values to an array like this?
Use np.concatenate or np.vstack
np.concatenate([x, np.zeros((13, x.shape[1]))], axis=0)
# or
np.vstack([x, np.zeros((13, x.shape[1]))])
Ummm...there is no converting the dimensions of a numpy array in python. A numpy array is simply a section of your RAM. You can't append to it in the sense of literally adding bytes to the end of the array, but you can create another array and copy over all the data (which is what np.append(), or np.vstack(), or np.concatenate(), etc.). In general, the dimensions of your array is simply a few variables that python keeps track of to present the data in the array to you, same thing as it's dtype.
For example,
X = np.array([1,2,3,4,5],dtype='int32')
print(X)
X.dtype = 'int16' #The data is not converted it is simply displayed differently now.
print(X) #Displays the data for the array.
X.shape = (5,2) #Does not convert the data or touch it.
print(X) #Displays the data for you using the parameter set in .shape.
For your data, you can simply update the .shape when you append more data.
x = np.append(x, np.zeros((13,21)))
x.shape = (1329, 21)
May be like this:
import numpy as np
x = np.array([[1, 2, 3,4], [4, 5, 6,7]])
x = np.append(x, [np.zeros(4) for _ in range(13)] , axis=0)
print(x.shape)
print(x)
Below is the matrix of (3*3)
a_matrix=np.array([[2,3,5],[3,2,7],[1,4,2]])
and i want to change it to (9*1) which is
[[2],[3],[5],[3],[2],[7],[1],[4],[2]]
The problem is that i need to do this without using reshape method in numpy. BTW, below is what i did which is kind of wrong. Could anyone help me with that? BTW, **i can't use those pre-defined methods to do it, i have to implement it by my own method. **.Any help is appreciated!!!
import numpy as np
a_list=[]
a_matrix=np.array([[2,3,5],[3,2,7],[1,4,2]]) #3*3 matrix
for i in range(3):
a_list.extend(a_matrix[i,:])
a_list=np.asarray(a_list) #To convert the list to numpy array
print(a_list.T.shape) #To print the shape of transpose
--->(9,) # I want (9,1) not (9,)
Flatten it and use a list comprehension
result = np.array([[x] for x in a_matrix.ravel()])
You could use np.ravel and add a dummy axis afterwards.
l_list = a_matrix.ravel()[:,None]
EDIT:
If you want a numpy-free method:
l_list = []
for i in range(3):
for j in range(3):
# replace [i][j] with [i,j] if a_matrix
# is allowed to be a numpy array
l_list.append([a_matrix[i][j]])
If you want to have the result as a numpy array without using ravel or reshape, you could create the output array in advance
l_list = np.empty((9,1))
for i in range(3):
for j in range(3):
# replace [i][j] with [i,j] if a_matrix
# is allowed to be a numpy array
l_list[i*3 + j] = a_matrix[i][j]
A pure list operation:
In [122]: alist = [[2,3,5],[3,2,7],[1,4,2]]
In [123]: [[i] for x in alist for i in x]
Out[123]: [[2], [3], [5], [3], [2], [7], [1], [4], [2]]
Forget about the np.array stuff before or after. If you can't use reshape there's no point in talking about numpy. shape is an integral part of a numpy array, and changing it with reshape is a fundamental operation.
Here is an answer where you code the solution yourself (as per your assignments requirements).
import numpy as np
a_matrix = np.array([[2,3,5],[3,2,7],[1,4,2]])
a_list = [[elem] for row in a_matrix for elem in row]
a_list = np.asarray(a_list)
print(a_list.T.shape)
Output should be as expected.
I've been running into a TypeError: list indices must be integers, not tuple. However, I can't figure out how to fix it, as I'm apparently misunderstanding where the tuple is (wasn't even aware there would be one from what I understand). Shouldn't my index and the values that I'm passing in all be integers?
def videoVolume(images):
""" Create a video volume from the image list.
Note: Simple function to convert a list to a 4D numpy array.
Args:
images (list): A list of frames. Each element of the list contains a
numpy array of a colored image. You may assume that each
frame has the same shape, (rows, cols, 3).
Returns:
output (numpy.ndarray): A 4D numpy array. This array should have
dimensions (num_frames, rows, cols, 3) and
dtype np.uint8.
"""
output = np.zeros((len(images), images[0].shape[0], images[0].shape[1],
images[0].shape[2]), dtype=np.uint8)
# WRITE YOUR CODE HERE.
for x in range(len(images)):
output[:,:,:,:] = [x, images[x,:,3], images[:,x,3], 3]
# END OF FUNCTION.
return output
The tuple referred to in the error message is the x,:,3 in the index here:
images[x,:,3]
The reason this is happening is that images is passed in as a list of frames (each a 3d numpy array), but you are trying to access it as though it is itself a numpy array. (Try doing lst = [1, 2, 3]; lst[:,:] and you'll see you get the same error message).
Instead, you meant to access it as something like images[x][:,:,:], for instance
for x in range(len(images)):
output[x,:,:,:] = images[x][:,:,:]
I want to take an array with shape (N,), and dtype=object, of arrays that all have the same shape, shape, and create an array with shape == (N,) + shape. I was wondering if anyone knew the best way to do this. Here's an example.
import numpy as np
array = np.empty(4, dtype=object)
array[:] = [np.ones([3, 2])]
array = np.array(array.tolist())
print array.dtype
# float64
print array.shape
# (4, 3, 2)
If you already know the shape of your inner arrays (here, (3,2)), you could simplify the whole process as
subshape = (3,2)
a = np.empty(tuple([N,]+list(subshape)), dtype=object)
a[:] = np.ones(subshape)
That will let you avoid unnecessary conversions to/from lists.
Now, assuming you have a (N,) object array a where each element is a subshape float array, you could do:
a = np.vstack(a)
a.shape = [N,] + list(subshape)
or more simply:
a = np.array(a.tolist(), dtype=float)
the .tolist conversion might not be very efficient, though.
I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this:
> import numpy as np
> A = np.array([1,2,3,4,5,6])
> B = vec2matrix(A,ncol=2)
> B
array([[1, 2],
[3, 4],
[5, 6]])
Does numpy have a function that works like my made-up function "vec2matrix"? (I understand that you can index a 1D array like a 2D array, but that isn't an option in the code I have - I need to make this conversion.)
You want to reshape the array.
B = np.reshape(A, (-1, 2))
where -1 infers the size of the new dimension from the size of the input array.
You have two options:
If you no longer want the original shape, the easiest is just to assign a new shape to the array
a.shape = (a.size//ncols, ncols)
You can switch the a.size//ncols by -1 to compute the proper shape automatically. Make sure that a.shape[0]*a.shape[1]=a.size, else you'll run into some problem.
You can get a new array with the np.reshape function, that works mostly like the version presented above
new = np.reshape(a, (-1, ncols))
When it's possible, new will be just a view of the initial array a, meaning that the data are shared. In some cases, though, new array will be acopy instead. Note that np.reshape also accepts an optional keyword order that lets you switch from row-major C order to column-major Fortran order. np.reshape is the function version of the a.reshape method.
If you can't respect the requirement a.shape[0]*a.shape[1]=a.size, you're stuck with having to create a new array. You can use the np.resize function and mixing it with np.reshape, such as
>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)
Try something like:
B = np.reshape(A,(-1,ncols))
You'll need to make sure that you can divide the number of elements in your array by ncols though. You can also play with the order in which the numbers are pulled into B using the order keyword.
If your sole purpose is to convert a 1d array X to a 2d array just do:
X = np.reshape(X,(1, X.size))
convert a 1-dimensional array into a 2-dimensional array by adding new axis.
a=np.array([10,20,30,40,50,60])
b=a[:,np.newaxis]--it will convert it to two dimension.
There is a simple way as well, we can use the reshape function in a different way:
A_reshape = A.reshape(No_of_rows, No_of_columns)
You can useflatten() from the numpy package.
import numpy as np
a = np.array([[1, 2],
[3, 4],
[5, 6]])
a_flat = a.flatten()
print(f"original array: {a} \nflattened array = {a_flat}")
Output:
original array: [[1 2]
[3 4]
[5 6]]
flattened array = [1 2 3 4 5 6]
some_array.shape = (1,)+some_array.shape
or get a new one
another_array = numpy.reshape(some_array, (1,)+some_array.shape)
This will make dimensions +1, equals to adding a bracket on the outermost
Change 1D array into 2D array without using Numpy.
l = [i for i in range(1,21)]
part = 3
new = []
start, end = 0, part
while end <= len(l):
temp = []
for i in range(start, end):
temp.append(l[i])
new.append(temp)
start += part
end += part
print("new values: ", new)
# for uneven cases
temp = []
while start < len(l):
temp.append(l[start])
start += 1
new.append(temp)
print("new values for uneven cases: ", new)
import numpy as np
array = np.arange(8)
print("Original array : \n", array)
array = np.arange(8).reshape(2, 4)
print("New array : \n", array)