i have a 3d array in this form (12457,8,6) i wand to divide it into 2 equal numpy arrays like (12457,3,8)
In fact that the first one containt the first 3 bands and the second one contains the remaind bands: In other words I want my array1 contains the bands 1,2,3 and my array2 contains the bands 4,5,6
I tried with that but it doesnt work
array1=data[:,:,3]
array1.shape
(12457,8)
You can use np.split -
X = np.random.random((1200,6,8))
print(X.shape)
X1, X2 = np.split(X, 2, axis=1) #Array, num of splits, axis for splitting
print(X1.shape, X2.shape)
(1200, 6, 8)
(1200, 3, 8) (1200, 3, 8)
split or array_split should be able to help you out.
import numpy as np
arr = np.array([[1,2,3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
newarr = np.split(arr, 2)
print(newarr)
Prints:
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]])]
Related
i would like to get an multidimentional array in arr1.shape = (x,y)
which would be filled with values like from np.arange(z), where z is number of spaces in arr1.
it is known that, that i could make
arr2 = np.random.randn(x,y)
but then the values would be random...
Is there any straight way, which allows me not to iterate through the array?
You could use numpy.reshape to take the result of numpy.arange and reshape into (x,y) dimensions
>>> import numpy as np
>>> x = 5
>>> y = 3
>>> np.reshape(np.arange(x*y), (x,y))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
The situation is the following:
Lets say I have two arrays, x and y:
Input:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
y = [2,6,9,13]
Expected output:
arr1 = [2,3,4,5]
arr2 = [6,7,8]
arr3 = [9,10,11,12]
I would like to create a python script that can let me split up array x into multiple arrays based on the values of array y as the end points.
So x will split up between 2 and 6, then between 6 and 9, then between 9 and 13 in this example.
I am not sure how to get started on this, I am a beginner. I would appreciate the help and I would love to know how you broke down the problem to solve it? Thank you!
Find the index of x based on value in y and then use indexing
arr = []
for i in range(len(y)-1):
arr.append(x[x.index(y[i]):x.index(y[i+1])])
arr
[[2, 3, 4, 5], [6, 7, 8], [9, 10, 11, 12]]
This works if there are duplicates in the array,
x = [2,3,4,5,6,7,7,8,9,9,10,11,11,12,13,14,15]
y = [2,6,9,13]
arr = []
for i in range(len(y)-1):
arr.append(x[x.index(y[i]):x.index(y[i+1])])
[[2, 3, 4, 5], [6, 7, 7, 8], [9, 9, 10, 11, 11, 12]]
For x sorted, we can use np.searchsorted with np.split to split x using the indices where y is contained in x:
import numpy as np
i = np.searchsorted(x, y)
np.split(x,i+1)[1:-1]
# [array([3, 4, 5, 6]), array([7, 8, 9]), array([10, 11, 12, 13])]
You are somewhat inconsistent in your expected output, or I don`t get the pattern behind it, but this should get you close to what you want
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
y = [2,6,9,13]
m = []
for i in range(len(y) - 1):
m += [x[y[i] - 1:y[i+1] - 1]]
print(m)
Output
[[2, 3, 4, 5], [6, 7, 8], [9, 10, 11, 12]]
if i have an array
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
output would be a =[1,2,3],[4,5,6],[7,8,9]
using slice [start:endindex:stepindex],
how could i retrieve 3 and 7?
is it possible?
I have tried
a[:3:2]
this gave me 1rst row and third row
In [928]: a = np.array([[1,2,3],[4,5,6],[7,8,9]])
In [929]: a
Out[929]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
[3,7] isn't regular pattern in this 2d array. But its flattened view:
In [931]: a.ravel()
Out[931]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [932]: a.ravel()[2::4]
Out[932]: array([3, 7])
In [933]: a.flat[2::4]
Out[933]: array([3, 7])
Now guarantee that it can be extended for larger arrays and selections.
Suppose you have a matrix:
a = np.arange(9).reshape(3,3)
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
and I want get or set over the values 1, 5, and 6, how would I do that.
For example I thought doing
# getting
b = a[:, np.array([1,2,0])]
# want b = [1,5,6]
# setting
a[:, np.array([1,2,0])] = np.array([9, 10, 11])
# want:
# a = array([[0, 9, 2],
# [3, 4, 10],
# [11, 7, 8]])
would do it, but that is not the case. Any thoughts on this?
Only a small tweak makes this work:
import numpy as np
a = np.arange(9).reshape(3,3)
# getting
b = a[range(a.shape[0]), np.array([1,2,0])]
# setting
a[range(a.shape[0]), np.array([1,2,0])] = np.array([9, 10, 11])
The reason why your code didn't work as expected is because you were indexing the x-axis with slices instead of indices. Slices mean take all rows, but specifying the index directly will get you the row you want for each index value.
How can I find the dimensions of a matrix in Python. Len(A) returns only one variable.
Edit:
close = dataobj.get_data(timestamps, symbols, closefield)
Is (I assume) generating a matrix of integers (less likely strings). I need to find the size of that matrix, so I can run some tests without having to iterate through all of the elements. As far as the data type goes, I assume it's an array of arrays (or list of lists).
The number of rows of a list of lists would be: len(A) and the number of columns len(A[0]) given that all rows have the same number of columns, i.e. all lists in each index are of the same size.
If you are using NumPy arrays, shape can be used.
For example
>>> a = numpy.array([[[1,2,3],[1,2,3]],[[12,3,4],[2,1,3]]])
>>> a
array([[[ 1, 2, 3],
[ 1, 2, 3]],
[[12, 3, 4],
[ 2, 1, 3]]])
>>> a.shape
(2, 2, 3)
As Ayman farhat mentioned
you can use the simple method len(matrix) to get the length of rows and get the length of the first row to get the no. of columns using len(matrix[0]) :
>>> a=[[1,5,6,8],[1,2,5,9],[7,5,6,2]]
>>> len(a)
3
>>> len(a[0])
4
Also you can use a library that helps you with matrices "numpy":
>>> import numpy
>>> numpy.shape(a)
(3,4)
To get just a correct number of dimensions in NumPy:
len(a.shape)
In the first case:
import numpy as np
a = np.array([[[1,2,3],[1,2,3]],[[12,3,4],[2,1,3]]])
print("shape = ",np.shape(a))
print("dimensions = ",len(a.shape))
The output will be:
shape = (2, 2, 3)
dimensions = 3
m = [[1, 1, 1, 0],[0, 5, 0, 1],[2, 1, 3, 10]]
print(len(m),len(m[0]))
Output
(3 4)
The correct answer is the following:
import numpy
numpy.shape(a)
Suppose you have a which is an array. to get the dimensions of an array you should use shape.
import numpy as np
a = np.array([[3,20,99],[-13,4.5,26],[0,-1,20],[5,78,-19]])
a.shape
The output of this will be
(4,3)
You may use as following to get Height and Weight of an Numpy array:
int height = arr.shape[0]
int width = arr.shape[1]
If your array has multiple dimensions, you can increase the index to access them.
You simply can find a matrix dimension by using Numpy:
import numpy as np
x = np.arange(24).reshape((6, 4))
x.ndim
output will be:
2
It means this matrix is a 2 dimensional matrix.
x.shape
Will show you the size of each dimension. The shape for x is equal to:
(6, 4)
A simple way I look at it:
example:
h=np.array([[[[1,2,3],[3,4,5]],[[5,6,7],[7,8,9]],[[9,10,11],[12,13,14]]]])
h.ndim
4
h
array([[[[ 1, 2, 3],
[ 3, 4, 5]],
[[ 5, 6, 7],
[ 7, 8, 9]],
[[ 9, 10, 11],
[12, 13, 14]]]])
If you closely observe, the number of opening square brackets at the beginning is what defines the dimension of the array.
In the above array to access 7, the below indexing is used,
h[0,1,1,0]
However if we change the array to 3 dimensions as below,
h=np.array([[[1,2,3],[3,4,5]],[[5,6,7],[7,8,9]],[[9,10,11],[12,13,14]]])
h.ndim
3
h
array([[[ 1, 2, 3],
[ 3, 4, 5]],
[[ 5, 6, 7],
[ 7, 8, 9]],
[[ 9, 10, 11],
[12, 13, 14]]])
To access element 7 in the above array, the index is h[1,1,0]