<jupyter_start><jupyter_text>NumpyThese notebooks are used to check certain examples in the course notes, they are not intended as a teaching resource.<jupyter_code>import numpy as np
#Go up one directory as this is where the 'current' directory assumed in the course notes 
pwd
cd ..
filepath = "data/europe-seasonal.txt"
!head -5 $filepath
#data = np.loadtxt(filepath, converters=None, skiprows=120)
data = np.genfromtxt?
data = np.genfromtxt(filepath, skip_header=119)
data.dtype
np.loadtxt(filepath, converters=None, skiprows=119)
print('random value in data:', data[250, 2])
print(data[0:4, 0:10])
x = eval('[ "A","B","C" , " D"]')
x
%pylab inline
import matplotlib.pylab as plt
plt.plot(data[:,0], data[:,2])
plt.plot(data[:,0], data[:,4])
fig = plt.figure()

axes = fig.add_axes([0.2, 0.2, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)

axes.plot(data[:,0], data[:,2], 'r', label = 'MAM - Autumn')

axes.set_xlabel('year')
axes.set_ylabel('temperature')
axes.set_title('European Seasonal Temperature Reconstructions')

axes.legend()
fig.add_axes?
fig, axes = plt.subplots(figsize=(12,3)) #use the subplots axis layout managers in matplotlib.

axes.plot(data[:,0], data[:,2], 'r', label = 'MAM - Autumn')

axes.set_xlabel('year')
axes.set_ylabel('temperature')
axes.set_title('European Seasonal Temperature Reconstructions')

axes.legend()
v = np.array([1,2,3,4])

print(v)
M = array([[1, 2], [3, 4]])
print(M)
print(type(v), type(M))
type(v), type(M)
print(v.shape, M.shape)
x = np.arange(0, 10, 1) # arguments: start, stop, step
print(x)
x = np.linspace(0, 3, 20)
y = np.linspace(0, 9, 20)
plt.plot(x, y)       # line plot
plt.plot(x, y, 'o')  # dot plot
plt.show() 
x, y = mgrid[0:3, 0:3] # similar to meshgrid in MATLAB
print(x)
print(x , "\n\n",  y)
x, y = np.mgrid[0:5, 0:5]
x
z = np.zeros((3,3))
print(z)
Z = [[0,0,0,0,0,0],
         [0,0,0,1,0,0],
         [0,1,0,1,0,0],
         [0,0,1,1,0,0],
         [0,0,0,0,0,0],
         [0,0,0,0,0,0]]
def compute_neighbours(Z):
    shape = len(Z), len(Z[0])
    N  = [[0,]*(shape[0])  for i in range(shape[1])]
    for x in range(1,shape[0]-1):
        for y in range(1,shape[1]-1):
            N[x][y] = Z[x-1][y-1]+Z[x][y-1]+Z[x+1][y-1] \
                    + Z[x-1][y]            +Z[x+1][y]   \
                    + Z[x-1][y+1]+Z[x][y+1]+Z[x+1][y+1]
    return N
def iterate(Z):
    N = compute_neighbours(Z)
    shape = len(Z), len(Z[0])
    for x in range(1,shape[0]-1):
        for y in range(1,shape[1]-1):
            if Z[x][y] == 1 and (N[x][y] < 2 or N[x][y] > 3):
                 Z[x][y] = 0
            elif Z[x][y] == 0 and N[x][y] == 3:
                 Z[x][y] = 1
    return Z
plt.imshow(Z, interpolation=None)
iterate(Z)
import matplotlib.pyplot as plt
import numpy as np


fig, axes = plt.subplots(figsize=(3,3)) 
plt.imshow(Z, cmap='Greys',  interpolation='nearest')
plt.savefig('blkwht.png')

plt.show()
compute_neighbours
shape = len(Z), len(Z[0])
shape
shape = Z.shape
Z = np.array([[0,0,0,0,0,0],
              [0,0,0,1,0,0],
              [0,1,0,1,0,0],
              [0,0,1,1,0,0],
              [0,0,0,0,0,0],
              [0,0,0,0,0,0]])
Z = np.random.randint(2, size=(6, 6))
print(Z)
def iterate_2(Z):
    # Count neighbours
    N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
         Z[1:-1,0:-2]                + Z[1:-1,2:] +
         Z[2:  ,0:-2] + Z[2:  ,1:-1] + Z[2:  ,2:])
    return N
test = iterate_2(Z)
test
Z[0:-2,1:-1]
test 
Z
N = np.zeros(Z.shape, dtype=int)
N[1:-1,1:-1] += (Z[ :-2, :-2] + Z[ :-2,1:-1] + Z[ :-2,2:] +
                     Z[1:-1, :-2]                + Z[1:-1,2:] +
                     Z[2:  , :-2] + Z[2:  ,1:-1] + Z[2:  ,2:])
N
Z = np.random.random((10,2))
X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1])
D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
print(D)

Z
X = np.random.rand(5, 10)

# Recent versions of numpy
Y = X - X.mean(axis=1, keepdims=True)
Y
X.mean(axis=1)
Z = np.random.uniform(0,1,10)
z = 0.5
m = Z.flat[np.abs(Z - z).argmin()]
print(m)
np.abs(Z - z).argmin()

Z.argmin?

import matplotlib.image as mpimg

!wget https://github.com/matplotlib/sample_data/blob/master/stinkbug.png
img=mpimg.imread('stinkbug.png')
from scipy import ndimage
bug = ndimage.imread('stinkbug.png')
from scipy import misc
f = misc.face()
plt.imshow(f)
f.shape
random = np.random.rand(3,2)
print(random)
data = random
doubledata = data* 2.0
print('original:')
print(data)
print('doubledata:')
print(doubledata)
tripledata = doubledata + data
print('tripledata:')
print(tripledata)
print(data.mean())
print('maximum inflammation:', data.max())
print('minimum inflammation:', data.min())
print('standard deviation:', data.std())
ess_file = '../../data/essendon_airport_temps.csv'
data = np.genfromtxt(ess_file, delimiter=',')
ess_file = '../../data/essendon_airport_temps.csv'
array = np.genfromtxt(ess_file, delimiter=',')
data = array[:,2:]
data.shape
data[:,-1]
years = array[:,1]
years
years.astype(int)
print(data[0:5,]) 
doubledata = data * 2.0
print(doubledata[0:5,]) 
print(data.mean())

numpy.nanmean(data)
random.mean()
print('maximum monthly avg. temperature:', numpy.nanmax(data))
print('minimum monthly avg.temperature:', numpy.nanmin(data))
print('mean monthly avg. temp:', numpy.nanmean(data))
numpy.nanmax(data)
data

data = [[0, 1, numpy.nan], [8, 5, 1]]
data = numpy.array(data)
print(data)
print(nanmean(data))
print(nanmax(data))
print(nanmean(data, axis=0))
print(nanmean(data, axis=1))<jupyter_output>[[  0.   1.  nan]
 [  8.   5.   1.]]
3.0
8.0
[ 4.  3.  1.]
[ 0.5         4.66666667]
{'fraction_non_alphanumeric': 0.21592760180995474, 'fraction_numerical': 0.054841628959276016, 'mean_word_length': 3.4208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '227258', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2418, 'n_words': 504, 'path': 'examples/numpy.ipynb', 'repo_name': 'dansand/python', 'size': '5525'}