Given NxN dimensions, I'm traying to create a functions that returns a list of values that represent cells from the NxN matrix. for example:
a_3x3 = [ # 3x3 pixel window
[3,3,3],
[3,1,3],
[3,3,3]
]
a_3x3_lis = [3, 3, 3, 3, 1, 3, 3, 3, 3] # same window flattend
a_5x5 = [ # 5x5 pixel window
[5,5,5,5,5],
[5,3,3,3,5],
[5,3,1,3,5],
[5,3,3,3,5],
[5,5,5,5,5]
]
a_5x5_lis = [5, 5, 5, 5, 5, 5, 3, 3, 3, 5, 5, 3, 1, 3, 5, 5, 3, 3, 3, 5, 5, 5, 5, 5, 5] # same window flattened
I've just created the lists manually so far but its no good for large matrixes
near_win_3x3 = [3, 3, 3, 3, 1, 3, 3, 3, 3]
near_win_5x5 = [5, 5, 5, 5, 5, 5, 3, 3, 3, 5, 5, 3, 1, 3, 5, 5, 3, 3, 3, 5, 5, 5, 5, 5, 5]
near_win_7x7 = [7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 7, 7, 5, 3, 3, 3, 5, 7, 7, 5, 3, 1, 3, 5, 7, 7, 5, 3, 3, 3, 5, 7, 7, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,]
One way using numpy.minimum:
def reversed_pyramid(n):
a = np.arange(n)
m = np.minimum(a, a[::-1])
return n - np.minimum.outer(m, m) * 2
Output:
# reversed_pyramid(7)
array([[7, 7, 7, 7, 7, 7, 7],
[7, 5, 5, 5, 5, 5, 7],
[7, 5, 3, 3, 3, 5, 7],
[7, 5, 3, 1, 3, 5, 7],
[7, 5, 3, 3, 3, 5, 7],
[7, 5, 5, 5, 5, 5, 7],
[7, 7, 7, 7, 7, 7, 7]])
The values in your array are a function of their manhattan distance to the center. To be specific, it's: f(d) = 1 + 2 * d.
def make_window(N):
return 1 + 2 * abs(np.stack(np.mgrid[:N, :N]) - (N - 1) // 2).max(0)
N=7 produces:
[[7 7 7 7 7 7 7]
[7 5 5 5 5 5 7]
[7 5 3 3 3 5 7]
[7 5 3 1 3 5 7]
[7 5 3 3 3 5 7]
[7 5 5 5 5 5 7]
[7 7 7 7 7 7 7]]
my data
df = pd.DataFrame({"id":['1,2,3,4','1,2,3,6'], "sum": [6,7]})
mycode:
df['id']=df['id'].str.split(',')
df['nf']=df.apply(lambda x: set(range(1,x['sum']+1))-set(x['id']) , axis=1)
print(df)
i want output
id sum nf
0 [1, 2, 3, 4] 6 {5, 6}
1 [1, 2, 3, 6] 7 {4, 5, 7}
but it output
id sum nf
0 [1, 2, 3, 4] 6 {1, 2, 3, 4, 5, 6}
1 [1, 2, 3, 6] 7 {1, 2, 3, 4, 5, 6, 7}
i think the 'num' in the list is actually str
but i don't known how to easily modify it by pandas
Use map for convert values to integers:
df['nf']=df.apply(lambda x: set(range(1,x['sum']+1))-set(map(int, x['id'])) , axis=1)
print(df)
id sum nf
0 [1, 2, 3, 4] 6 {5, 6}
1 [1, 2, 3, 6] 7 {4, 5, 7}
I have this series called hours_by_analysis_date, where the index is datetimes, and the values are a list of ints. For example:
Index |
01-01-2000 | [1, 2, 3, 4, 5]
01-02-2000 | [2, 3, 4, 5, 6]
01-03-2000 | [1, 2, 3, 4, 5]
I want to return all the indices where the value is [1, 2, 3, 4, 5], so it should return 01-01-2000 and 01-03-2000
I tried hours_by_analysis_date.where(fh_by_analysis_date==[1, 2, 3, 4, 5]), but it gives me the error:
{ValueError} lengths must match to compare
It's confused between comparing two array-like objects and equality test for each element.
You can use apply:
hours_by_analysis_date.apply(lambda elem: elem == [1,2,3,4,5])
Say I have input data with variable length sequences loaded into memory:
sentences = [
[0, 1, 2, 3, 4, 5, 6, 7],
[0, 1, 2, 3, 4, 5, 6, 7],
[0, 1, 2, 3, 4, 5 ],
[0, 1, 2, 3, 4, 5 ]
]
How can I use this to fill a queue? E.g. something like:
padding_q = tf.PaddingFIFOQueue(
capacity=len(sentences),
dtypes=[tf.int32], shapes=[[None]])
qr = tf.train.QueueRunner(padding_q, [the_wanted_op])
How does the_wanted_op look like? It should enqueue one sentence yet four enqueues must have enqueued each sentence once.
Googled around a bit and couldn't seem to find anything on this.
Is there an option to access data in a pandas data frame using "not index"?
So something like
df_index = asdf = pandas.MultiIndex(levels=[
['2014-10-19', '2014-10-20', '2014-10-21', '2014-10-22', '2014-10-30'],
[u'after_work', u'all_day', u'breakfast', u'lunch', u'mid_evening']],
labels=[[0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4],
[4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 2, 0, 1, 3, 4]],
names=[u'start_date', u'time_group'])
And then I would like to be able to call the following to get everything not in df_index
df.ix[~df_index]
I know you can do it for logical indexing within pandas. Just curious if I could do it using an Index Object
you can use df.drop(df_index, errors="ignore").