iPython numpy - How to change value of an array slice with a map - arrays

I've got a 3-dim array [rows][cols][3] with values between 0 and X.
I need to manipulate a specific dimension in the array. So I've taken a slice of the part I want to manipulate
arr_slice = array[:,:,0]
now I can make some manipulations like arr_slice *= 3 and that will change the original array, as I intended.
However, I need to change values according to a map, which is an array with size X that maps the values of the slice (0-X) to new values. the map is called mapping
so I know mapping[arr_slice] will do what I want, but using it like this:
arr_slice = mapping[arr_slice]
will of course change only arr_slice and not the original array I have.
So, How can I perform this task to change the original array?
The array is actually an image, that I'm trying to manipulate it's Y values in YIQ format:
im_eq = np.copy(im_orig)
if (rgb):
im_eq = rgb2yiq(im_eq)
im = im_eq[:,:,0]
else:
im = im_eq
mapping = get_cumutative_histogram(im)
im = mapping[im.astype(int)] # the problematic line

You need to address the slice elements:
im[:] = mapping[im.astype(int)]
for example:
from pylab import *
a = rand(10)
sl = a[4:9]
print sl # ->: array([ 0.97278179, 0.7894741 , 0.38051133, 0.42684762, 0.82670638])
sl[:] = 1
print a #-> array([ 0.21125781, 0.4235981 , 0.81950229, 0.93937973, 1. , 1. , 1. , 1. , 1. , 0.39047808])

Related

Assign values to an array of indexes in Python

I have an array of size 300x5. In this the column with index 3 consists if some index and column with index 4 consists of corresponding values.
I have created new array in which I am trying to assign the values in index 4 at index 3 locations in this new array. I tried this but it throws an error.
new_arr[old_arr[:,3]] = old_arr[:,4]
One of the example related to what I want to do
new_arr = np.ones((200,1))
new_arr[[2,3,4]] = [22,44,11]
It throws an error
ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (3,1)
With this code : new_arr[old_arr[:,3]] you try to access new_arr that index come from values are in old_arr[:,3] and you got IndexError.
Is this help you?
new_arr = np.zeros((300, 5))
new_arr[:,3] = old_arr[:,4]
For edited question you need reshape:
new_arr = np.ones((200,1))
new_arr[[2,3,4]] = np.array([2,4,6]).reshape(3,1)
# OR
# new_arr[2:5] = np.array([22,44,11]).reshape(3,1)

Modifying a numpy array efficiently

I have a numpy array A of size 10 with values ranging from 0-4. I want to create a new 2-D array B from this with its ith column being a vector corresponding to the ith element of A.
For example, the value 1 as the first element of A would correspond to B having a column vector [0,1,0,0,0] as it's first column. A having 4 as its third element would correspond to B having it's 3rd column as [0,0,0,1,0]
I have the following code:
import numpy as np
A = np.random.randint(0,5,10)
B = np.ones((5,10))
iden = np.identity(5, dtype=np.float64)
for i in range(0,10):
a = A[i]
B[:,i:i+1] = iden[:,a:a+1]
print A
print B
The code is doing what it's supposed to be doing but I am sure there are more efficient ways of doing this. Can anyone please suggest some?
That could be solved by initializing an array of zeros and then integer-indexing into it with indices from A and assigning 1s, like so -
M,N = 5,10
A = np.random.randint(0,M,N)
B = np.zeros((M,N))
B[A,np.arange(len(A))] = 1

Trying to append content to numpy array

I have a script that searches Twitter for a certain term and then prints out a number of attributes for the returned results.
I'm trying to Just a blank array is returned. Any ideas why?
public_tweets = api.search("Trump")
tweets_array = np.empty((0,3))
for tweet in public_tweets:
userid = api.get_user(tweet.user.id)
username = userid.screen_name
location = tweet.user.location
tweetText = tweet.text
analysis = TextBlob(tweet.text)
polarity = analysis.sentiment.polarity
np.append(tweets_array, [[username, location, tweetText]], axis=0)
print(tweets_array)
The behavior I am trying to achieve is something like..
array = []
array.append([item1, item2, item3])
array.append([item4,item5, item6])
array is now [item1, item2, item3],[item4, item5, item6].
But in Numpy :)
np.append doesn't modify the array, you need to assign the result back:
tweets_array = np.append(tweets_array, [[username, location, tweetText]], axis=0)
Check help(np.append):
Note that
append does not occur in-place: a new array is allocated and
filled.
In the second example, you are calling list's append method which happens in place; This is different from np.append.
Here's the source code for np.append
In [178]: np.source(np.append)
In file: /usr/local/lib/python3.5/dist-packages/numpy/lib/function_base.py
def append(arr, values, axis=None):
....docs
arr = asanyarray(arr)
if axis is None:
.... special case, ravels
return concatenate((arr, values), axis=axis)
In your case arr is an array, starting with shape (0,3). values is a 3 element list. The is just a call to concatenate. So append call is just:
np.concateante([tweets_array, [[username, location, tweetText]]], axis=0)
But concatenate works with many items
alist = []
for ....:
alist.append([[username, location, tweetText]])
arr = np.concatenate(alist, axis=0)
should work just as well; better because list append is quicker. Or remove a level of nesting and let np.array stack them on a new axis, just as it does with np.array([[1,2,3],[4,5,6],[7,8,9]]):
alist = []
for ....:
alist.append([username, location, tweetText])
arr = np.array(alist) # or np.stack()
np.append has multiple problems. Wrong name. Doesn't act inplace. Hides concatenate. Flattens without much warning. Limits you to 2 inputs at a time. etc.

Numerical Iteration Python

for the following code:
from array import *
x=[]
x.append(0.232)
print (x)
for i in range(25):
x[i+1]=(1/(i+1))-5*x[i]
I have this error:
x[i+1]=(1/(i+1))-5*x[i]
IndexError: list assignment index out of range
This may be happening because I have defined x to be an empty array. But how do I define the array and perform the same operation otherwise?
list is not designed for efficient mathematical operations and therefore its better to use numpy arrays for doing mathematical operations. However, if you want to use list, you may define a list initialized with n zero's using
x=[0]*n
x[0] = 0.232
x[1] = ....
....
Remember, that a multidimensional list created using above approach will refer to same element in the array! For example:
l = [0,0,0]*5
will be creating five same list's inside another list not separate list's. So its a bad idea to create multidimensional array like this!
A better way would be to create arrays using numpy using following code:
from numpy import empty, zeros
x = empty(n) # or # x = zeros(n)
x[0] = 0.232
x[1] = ....
....
and
l = empty((3,5)) # or # l = zeros((3,5))
for a array with 3 rows and 5 columns.

Accessing n-dimensional array in R using a function of vector of indexes

my program in R creates an n-dimensional array.
PVALUES = array(0, dim=dimensions)
where dimensions = c(x,y,z, ... )
The dimensions will depend on a particular input. So, I want to create a general-purpose code that will:
Store a particular element in the array
Read a particular element from the array
From reading this site I learned how to do #2 - read an element from the array
ll=list(x,y,z, ...)
element_xyz = do.call(`[`, c(list(PVALUES), ll))
Please help me solving #1, that is storing an element to the n-dimensional array.
Let me rephrase my question
Suppose I have a 4-dimensional array. I can store a value and read a value from this array:
PVALUES[1,1,1,1] = 43 #set a value
data = PVALUES[1,1,1,1] #use a value
How can I perform the same operations using a function of a vector of indexes:
indexes = c(1,1,1,1)
set(PVALUES, indexes) = 43
data = get(PVALUES, indexes) ?
Thank you
Thanks for helpful response.
I will use the following solution:
PVALUES = array(0, dim=dimensions) #Create an n-dimensional array
dimensions = c(x,y,z,...,n)
Set a value to PVALUES[x,y,z,...,n]:
y=c(x,y,z,...,n)
PVALUES[t(y)]=26
Reading a value from PVALUES[x,y,z,...,n]:
y=c(x,y,z,...,n)
data=PVALUES[t(y)]
The indexing of arrays can be done with matrices having the same number of columns as there are dimensions:
# Assignment with "[<-"
newvals <- matrix( c( x,y,z,vals), ncol=4)
PVALUES[ newvals[ ,-4] ] <- vals
# Reading values with "["
PVALUES[ newvals[ ,-4] ]

Resources