<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Introduction to NumPy
Topics
Step1:  Indexing
Step2:  Play
with indexing and slicing...
Try a[12]...
Array functions
Step3:  Dimensions
Arrays in Python are not truely n-dimensional
Step4:  Indexing
Step5:  Play
with indexing and slicing...
Predict this
Step6:  ... have a look at y and predict what the next expression will output ...
Step7:  Array functions
Step9:  Play
Discover reductions over axes with functions similar to sum()
Step10:  Linear algebra
Step11:  Other linear algebra functions
Step12:  CSV data file example
Step13:  HDF5 data file example
<ASSISTANT_TASK:>
Python Code:
import numpy as np
a = np.arange(5)
a
a.size
a.shape
Explanation: Introduction to NumPy
Topics:
First contact
Dimensions
Linear algebra
Data files
Instructional material, unless otherwise indicated, by Markus van Dijk (SURFsara). Made available under CC0 https://creativecommons.org/publicdomain/zero/1.0/
First contact
Part of SciPy, see http://scipy.org
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
- a powerful N-dimensional array object
- useful linear algebra, Fourier transform, and random number capabilities
End of explanation
a[0]
a[-1]
a[2:4]
a[2:]
a[:4]
a[:]
a[1::2]
Explanation: Indexing
End of explanation
a.max()
a.min()
a.sum()
a + 3
a * 5
a + 3 * 5
a + 15
(a + 3) * 5
a * 5 + 3
a * (5 + 3)
%matplotlib inline
import matplotlib.pyplot as plt
a = np.linspace(-np.pi, np.pi, 100)
plt.plot(a)
plt.plot(a, np.cos(a))
plt.plot(a, np.arctan(a))
Explanation: Play
with indexing and slicing...
Try a[12]...
Array functions
End of explanation
a = np.random.random(15).reshape(3,5)
a
a.size
a.shape
Explanation: Dimensions
Arrays in Python are not truely n-dimensional: they can be a 1d-array of 1d-arrays (of ...)
NumPy arrays are truely n-dimensional.
End of explanation
a[0]
a[0,1]
a[0][1]
a[:,1]
a[1,1:3]
a[:,::2]
Explanation: Indexing
End of explanation
y = np.arange(35).reshape(5,7)
Explanation: Play
with indexing and slicing...
Predict this:
End of explanation
y[1:5:2,::3]
Explanation: ... have a look at y and predict what the next expression will output ...
End of explanation
a
a.sum()
a.sum(axis=1)
a.sum(axis=0)
a = np.random.random(24).reshape(2,3,4)
a
a.shape
a.sum(axis=2)
a.sum()
a *= 7
a, a.sum()
a += 3
a, a.sum()
a.sum(axis=(1,2))
a.sum(axis=(0,1))
a.sum(axis=(0,1)).sum()
Explanation: Array functions
End of explanation
# from numpy.org
def my_func(arr):
    Average first and last element of a 1-D array
    return (arr[0] + arr[-1]) * 0.5
b = np.array([[1,2,3], [4,5,6], [7,8,9]])
np.apply_along_axis(my_func, 0, b)
np.apply_along_axis(my_func, 1, b)
Explanation: Play
Discover reductions over axes with functions similar to sum(): min(), max(), avg(), mean().
Roll your own
Define a function that accepts a 1-d array as argument, predict the output.
End of explanation
a = np.arange(15).reshape(3,5)
a
a*a
a.transpose()
a.dot(a.transpose())
a.transpose().dot(a)
Explanation: Linear algebra
End of explanation
# from numpy.org
a = np.array([[1, 2, 3], [3, 4, 6.7], [5, 9.0, 5]])
b =  np.array([3, 2, 1])
np.linalg.solve(a, b)  # solve the equation ax = b
Explanation: Other linear algebra functions: see https://docs.scipy.org/doc/numpy-dev/reference/routines.linalg.html
Solve linear equation
End of explanation
data = np.genfromtxt('SCI_SICR_lv2_20031119_09000_0000_v1.0.dat')
data.shape
cols = 'lon0        lat0        lon1        lat1        lon2        lat2        lon3        lat3      lon_cent    lat_cent    dsr_time  int_time   sza    lza     H2O         HDO         N2O         CO          CH4         H2O_err     HDO_err     N2O_err     CO_err      CH4_err  red.chisq   dof eflag albedo   cl_fr mean_elev  st  px bs'.split()
for i in range(len(cols)):
    print(i, cols[i])
col_ix = cols.index('CH4')
col_ix
col_data = data.transpose()[col_ix]
print(col_data.shape)
for d in col_data[:10]:
    print(type(d), d)
plt.plot(col_data)
plt.plot(col_data[:100])
plt.plot([d for d in col_data if d < 4e19])
Explanation: CSV data file example
End of explanation
import h5py
f = h5py.File('SCI_RPRO_L2__CO____20031119T191154_20031119T194955_09000_01_070200_20030115T002650.nc', 'r')
for group_name, group in f.items():
    print(group_name, group)
    if len(group) < 20:
        for col in group:
            print('+', col, group[col].shape)
print(f['/main_product/CO_column'].shape)
for d in f['/main_product/CO_column'][:10]:
    print(d)
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(f['/main_product/CO_column'], color='green')
print(f['/instrument/time'].shape)
for d in f['/instrument/time'][0:9]:
    print(d)
from datetime import datetime
times = [datetime(*d) for d in f['/instrument/time']]
for t in times[0:9]:
    print(t)
plt.plot(times, f['/main_product/CO_column'], color='green')
plt.show()
ax1 = plt.axes()
ax1.plot(times, f['/main_product/CO_column'], color='green')
ax1.set_ylabel('CO')
ax2 = ax1.twinx()
ax2.plot(times, f['/side_product/H2O_column'], color='blue')
ax2.set_ylabel('H2O')
Explanation: HDF5 data file example
End of explanation
<END_TASK>