# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="RWr_rx9cQM4e" colab_type="code" outputId="be96bf00-6a55-43ec-f56a-b2c63f1fcaab" colab={"base_uri": "https://localhost:8080/", "height": 459}
"""
This Jupyter notebook contains simple examples on how to manipulate numpy 
arrays.
"""

import numpy as np
import queue

buffer=[0,1]
print('buffer=', buffer)

new=2
print('new=', new)

buffer = np.array(buffer + [new]) # append a new item & create a numpy array
print('np.array(buffer + [new])=', buffer)



# numpy array slicing syntax
# buffer[start:stop:step]

print('buffer[1:]=', buffer[1:]) # starting from index 1
print('buffer[-1:]=', buffer[-1:]) # getting item in last index

print('buffer[:1]=', buffer[:1]) # stop at index 1 (exclusive), keep only 1st item
print('buffer[:-1]=', buffer[:-1]) # stop at last index (exclusive), discard item in last index

print('buffer[::-1]=', buffer[::-1]) # start from last index (reversal)

print('buffer[1::-1]=', buffer[1::-1]) # reverse starting from index 1

# Starting from index 1 will return [1 2], reversing will return [2,1]
print('buffer[1:][::-1]=', buffer[1:][::-1])



# np.newaxis = None
print('buffer[:, np.newaxis]=', buffer[:, np.newaxis])
print('buffer[:, None]=', buffer[:, None])
print('buffer[np.newaxis, :]=', buffer[np.newaxis, :])
print('buffer[None, :]=', buffer[None, :])



# stacking

a = [1,2,3]
b = [4,5,6]
c = [7,8,9]

r = np.hstack((a,b,c)) # horizontal stacking
print("r=", r)

QUEUE = queue.Queue()
QUEUE.put(a)
QUEUE.put(b)
QUEUE.put(c)

r = [QUEUE.get() for _ in range(QUEUE.qsize())]      # collect data from all workers
print(r)

r = np.vstack(r) # vertical stacking
print(r)
print(r[:, ::-1]) # col reversal


StarcoderdataJupyterScriptsDedupFiltered