Accessing specific element of an array - python

I'm unsure of how to access an element in an array (of arrays?). Basically, I need to be able to assign random numbers to a series of arrays but I'm not sure how indexing works.
array_20 = np.zeros((5,10))
a = [[array_20]]*10
#This gives me 10 arrays of 5x10. I'd like to be able to then assign random
#numbers to all of the elements.

You could use numpy.random.rand like so:
import numpy as np
a = np.random.rand(10, 5, 10)
You can then index a like a python list. (i.e. a[1][2][0])

Related

How to merge values from each array to get one new array

I'm trying to combine two merge values from two arrays to get one whole new array. However, I have no idea how to do.
I want to get a random float number for two variables like 5 times because I want to store them for future use. Hence, I used math.random but it doesn't work as expected because it will replace the variables.
Hence, I tried to get a randomized number and put it into an array. Then, I want to combine them to get one array. Each random number from each array are together.
import numpy as np
np.random.seed(42)
randomReleaseAngle = np.empty(5)
randomVelocity = np.empty(5)
for i in range(5):
randomReleaseAngle[i] = np.random.uniform(20.0, 77.0 )
randomVelocity[i] = np.random.uniform(40.0, 60.0 )
print(randomReleaseAngle)
print(randomVelocity)
I wanted to get something like this:
[[41.34,51.72], [28.86,45.31], [54.26,44.23], [64.22,53.29], [72.27,52.13]]
You can specify a size of the output array when using np.random.uniform, no need for looping:
randomReleaseAngle = np.random.uniform(20.0, 77.0, size=(5, 2))
randomVelocity = np.random.uniform(40.0, 60.0, size=(5, 2))
array([[41.34878677, 74.19071547],
[61.72365468, 54.1235336 ],
[28.89306251, 28.89168766],
[23.31076589, 69.37204031],
[54.26355567, 60.36013693]])

python: how to create submatrices? Numpy

I have a matrix 1500X2, and I have to create 10 submatrices of 150 rows. How can i do this without for loop. I need a function, because with the [:] is too slow and complicated
You could use the numpy.take function to select a range of rows of your matrix. You can pass the indices you want to select and the axis over which you want to select your items.
import numpy as np
indices = list(range(0,3))
array = np.random.rand(5,2)
print(array)
res = np.take(array, indices, 0)
print(res)
You get something like this:
[[0.63680493 0.27066094]
[0.71182288 0.48258969]
[0.61321531 0.02215374]
[0.98148503 0.5669895 ]
[0.42720908 0.57326236]]
[[0.63680493 0.27066094]
[0.71182288 0.48258969]
[0.61321531 0.02215374]]

Selecting axis form multidimensional arrays with an array

I am trying to select a subset of a multidimensional array using another array, so for example, if I have:
a=np.linspace(1,30,30)
a=a.reshape(5,3,2)
I would like to take the subset [:,0,1], which I can do by saying
a_subset=a[:,0,1]
but, is there any way to define an array/list specifying that subset and then subtract it? The idea is to do something like:
b=[:,0,1]
a_subset=a[b]
which does not work as ":" is not accepted as item ("SyntaxError: invalid syntax")
You can do this using numpy.index_exp (docs) as follows:
import numpy as np
a = np.linspace(1, 30, 30)
a = a.reshape(5, 3, 2)
b = np.index_exp[:,0,1]
a_subset = a[b]

Create a numpy array when indexes of (fixed) elements are given

I have a numpy array of indexes e.g. [1,3,12]. I want to create another array from this such that at these indexes, I get a non-zero elements e.g. 1. So in this case, with input [1,3,12], I should get [0,1,0,1,0,0,0,0,0,0,0,0,1]. I can do it in a for loop, is there a short numpy function to achieve this?
With numpy you can index with lists directly:
a = [1,3,12]
vector = numpy.zeros(shape=max(a) + 1)
vector[a] = 1

Fastest way to get elements from a numpy array and create a new numpy array

I have numpy array called data of dimensions 150x4
I want to create a new numpy array called mean of dimensions 3x4 by choosing random elements from data.
My current implementation is:
cols = (data.shape[1])
K=3
mean = np.zeros((K,cols))
for row in range(K):
index = np.random.randint(data.shape[0])
for col in range(cols):
mean[row][col] = data[index][col]
Is there a faster way to do the same?
You can specify the number of random integers in numpy.randint (third argument). Also, you should be familiar with numpy.array's index notations. Here, you can access all the elements in one row by : specifier.
mean = data[np.random.randint(0,len(data),3),:]

Categories