<jupyter_start><jupyter_text>Python 2  Numpy Topics In this section we will cover intermediate and advanced topics in numpy. However, since Numpy has so many details, we will not cover the topic in its totality.<jupyter_code># First we need to import the basic libraries we will use
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
#%matplotlib inline
print 'Numpy version: {}'.format(np.__version__)
print 'Matplotlib version: {}'.format(matplotlib.__version__)<jupyter_output>Numpy version: 1.11.0
Matplotlib version: 1.5.1<jupyter_text>Advanced indexing and slicing<jupyter_code># How can we flip image using only numpy indexing? Let's see...
from scipy.misc import ascent
img = ascent()

# Plot the original array
plt.subplot(221)
plt.imshow(img, plt.cm.gray)

# plot the flipped array
plt.subplot(222)
plt.imshow(img[:,::-1], plt.cm.gray) #try to play with the -1

# plot a slice array
plt.subplot(223)
plt.imshow(img[:img.shape[0]/2, :img.shape[1]/2], plt.cm.gray)

# Apply a mask
mask = img % 2 == 0
masked_img = img.copy()
masked_img[mask] = 0
plt.subplot(224)
plt.imshow(masked_img, plt.cm.gray)
plt.show()
# Some more fancy indexing

# This script demonstrates fancy indexing by setting values on the diagonal to 0

# Get data
img = ascent()
xmax = img.shape[0]
ymax = img.shape[1]

# Fancy Indexing
# Set values on diagonal to 0
img[range(xmax), range(ymax)] = 0

# Set values on other diagonal to 0
img[range(xmax-1,-1,-1), range(ymax)] = 0

# Plot image with digonal lines set to 0
plt.imshow(img, plt.cm.gray)
plt.show()<jupyter_output><empty_output><jupyter_text>Manipulating arrays<jupyter_code># Now, let's try to shuffle an array

# Get data 
img = ascent()
xmax = img.shape[0]
ymax = img.shape[1]

def shuffle_indices(size):
    arr = np.arange(size)
    np.random.shuffle(arr)
    return arr

xindices = shuffle_indices(xmax)
np.testing.assert_equal(len(xindices), xmax)
yindices = shuffle_indices(ymax)
np.testing.assert_equal(len(yindices), ymax)

#Plot lena
plt.imshow(img[np.ix_(xindices, yindices)], plt.cm.gray)
plt.show()
# Indexing with booleans

def get_indices(size):
    """
    return indexes that are multipleof 4
    """
    arr = np.arange(size)
    return arr % 4 == 0

# Plot image
img_ = img.copy()
xindices = get_indices(img.shape[0])
yindices = get_indices(img.shape[1])
img_[xindices, yindices] = 0
plt.subplot(211)
plt.imshow(img_, plt.cm.gray)

img__ = img.copy()
# Between quarter and 3 quarters of the max value
img__[(img > img.max()/4) & (img < 3 * img.max()/4)] = 0
plt.subplot(212)
plt.imshow(img__, plt.cm.gray)
plt.show()<jupyter_output><empty_output><jupyter_text>Broadcasting<jupyter_code># Time to see how Numpy can be used to work with audio
import scipy.io.wavfile
import urllib2

# First we need to download a sample audio file
response = urllib2.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav')
print response.info()
WAV_FILE = 'smashingbaby.wav'
filehandle = open(WAV_FILE, 'w')
filehandle.write(response.read())
filehandle.close()

# Now with the WAV file, time to import it as a numpy array
sample_rate, data = scipy.io.wavfile.read(WAV_FILE) # sample_rate = samples/sec
print "Data type", data.dtype, "Shape", data.shape

# Let's see how the original data looks like
plt.subplot(2, 1, 1)
plt.title("Original")
plt.plot(data)

# Now let's make the audio a bit quieter by multipling the audio by 0.2
newdata = data * 0.2 #This will be float
newdata = newdata.astype(np.uint8)
print "Data type", newdata.dtype, "Shape", newdata.shape

scipy.io.wavfile.write("quiet.wav", sample_rate, newdata) #let's try sample rate = 2

# Let's see how the data looks like now
plt.subplot(2, 1, 2)
plt.title("Quiet")
plt.plot(newdata)
plt.tight_layout()
plt.show()<jupyter_output>Content-Type: audio/wav
Last-Modified: Tue, 12 Aug 2014 15:53:38 GMT
Accept-Ranges: bytes
ETag: "ac20ba9445b6cf1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 04 May 2017 15:58:06 GMT
Connection: close
Content-Length: 43642

Data type uint8 Shape (43584L,)
Data type uint8 Shape (43584L,)<jupyter_text>Out of memory process<jupyter_code>N = 10000

NSQUARES = 50

# Initialize
img = np.zeros((N, N), np.uint8)
centers = np.random.random_integers(0, N, size=(NSQUARES, 2))
radii = np.random.randint(0, N/9, size=NSQUARES)
colors = np.random.randint(100, 255, size=NSQUARES)

# Generate squares
for i in xrange(NSQUARES):
   xindices = range(centers[i][0] - radii[i], centers[i][0] + radii[i])
   xindices = np.clip(xindices, 0, N - 1)
   yindices = range(centers[i][1] - radii[i], centers[i][1] + radii[i])
   yindices = np.clip(yindices, 0, N - 1)

   if len(xindices) == 0 or len(yindices) == 0:
      continue

   coordinates = np.meshgrid(xindices, yindices)
   img[coordinates] = colors[i]

# Load into memory map
img.tofile('random_squares0.raw')
img_memmap = np.memmap('random_squares0.raw', shape=img.shape)

# Display image
plt.imshow(img_memmap)
plt.axis('off')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13129893595663522, 'fraction_numerical': 0.03111824934752058, 'mean_word_length': 4.902843601895735, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '321551', 'license': 'MIT', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1856, 'n_words': 513, 'path': 'felis_python2/lectures/02_Numpy.ipynb', 'repo_name': 'lordblaupause/felis_python2', 'size': '4981'}