I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Checking back to P4.3a shows this is a similar result, and the aquifer width is still 4500 m. Now let's change the perimeter no-flow BCs to specified head.
Can you write Python code for it?

#In order to replace the no-flow BCs with specified head we'll need the Col 2 and Col 10 heads from headobj
#Extract HEAD along a column
MODFLOW_COL_2_HEADS = HEAD[0,:,1]  # recall Python uses zero indexing so MODFLOW's Col 1 = Python's Col 0
MODFLOW_COL_10_HEADS = HEAD[0,:,-2]  # Likewise, Python's zero indexing means MODFLOW's Col 10 = Python's Col 9
print "MODFLOW Column 2 Heads:"
print MODFLOW_COL_2_HEADS
print "MODFLOW Column 10 Heads:"
print MODFLOW_COL_10_HEADS

# we see that the problem has symmetry so MODFLOW's Col 2 heads = Col 9 heads., which agrees with the plot
# Recall though that MODFLOW's array structure does not = Python, and the Flopy tool is
# Python consistent, so we need to reverse the order that came from the MODFLOW head file (flip them).
# It is not really needed here because of the problem dimension/symmetry, but it is good practice so we'll flip them
print "Python Column 1 Heads:"
PYTHON_COL_1_HEADS = np.flipud(MODFLOW_COL_2_HEADS)
print PYTHON_COL_1_HEADS
print "Python Column 9 Heads:"
PYTHON_COL_9_HEADS = np.flipud(MODFLOW_COL_10_HEADS)
print PYTHON_COL_9_HEADS

#Now let's reformulate the IBOUND array to change side no-flow perimeter BCs to specified head
IBOUND[:, :, 0] = -1  #don't forget arrays are zero-based
IBOUND[:, :, -1] = -1  #-1 in the list is Python for last in array
print IBOUND

#Recall that MODFLOW uses starting head values to set specified head BCs, so we need to update the
#starting head array.
STRT[:, :, 0] = PYTHON_COL_1_HEADS      # set column 0 heads to the simulated heads from column 1
STRT[:, :, -1] = PYTHON_COL_9_HEADS     # likewise, set column 10 heads to the simulated heads from column 9
print STRT

#now we have to update MODFLOW's BAS Package for this new information
BAS_PACKAGE = flopy.modflow.ModflowBas(MF, ibound=IBOUND, strt=STRT)
#and delete the old MODFLOW input files
#delete these files to prevent us from reading old results
modelfiles = os.listdir(modelpath)
for filename in modelfiles:
    f = os.path.join(modelpath, filename)
    if...


I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Discretization Package
Create a flopy discretization package object: flopy.modflow.ModflowDis.
Can you write Python code for it?

# Create the discretization object
TOP = ZTOP * np.ones((NROW, NCOL),dtype=np.float)

DIS_PACKAGE = flopy.modflow.ModflowDis(MF, NLAY, NROW, NCOL, delr=DELR, delc=DELC,
                               top=TOP, botm=BOTM[1:], laycbd=0)
# print DIS_PACKAGE  #uncomment this on far left to see information about the flopy object


I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Layer Property Flow Package
Create a flopy layer property flow package object: flopy.modflow.ModflowLpf.
Can you write Python code for it?

LPF_PACKAGE = flopy.modflow.ModflowLpf(MF, laytyp=1, hk=HK, vka=VKA)  # we defined the K and anisotropy at top of file
# print LPF_PACKAGE  # uncomment this at far left to see the information about the flopy LPF object


I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Output Control
Create a flopy output control object: flopy.modflow.ModflowOc.
Can you write Python code for it?

OC_PACKAGE = flopy.modflow.ModflowOc(MF)   # we'll use the defaults for the model output
# print OC_PACKAGE  # uncomment this at far left to see the information about the flopy OC object


I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Recharge Package
Create a flopy pcg package object: flopy.modflow.ModflowRch.
Can you write Python code for it?

RCH_PACKAGE = flopy.modflow.ModflowRch(MF, rech=RCH)
# print RCH_PACKAGE  # uncomment this at far left to see the information about the flopy RCH object


I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Setup a New Directory and Change Paths
For this tutorial, we will work in a new subdirectory underneath the directory where the notebook is located. We can use some fancy Python tools to help us manage the directory creation. Note that if you encounter path problems with this workbook, you can stop and then restart the kernel and the paths will be reset.
Can you write Python code for it?

# Set the name of the path to the model working directory
dirname = "P4-6_Hubbertville"
datapath = os.getcwd()
modelpath = os.path.join(datapath, dirname)
print 'Name of model path: ', modelpath

# Now let's check if this directory exists.  If not, then we will create it.
if os.path.exists(modelpath):
    print 'Model working directory already exists.'
else:
    print 'Creating model working directory.'
    os.mkdir(modelpath)


I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Well Package
Create a flopy output control object: flopy.modflow.ModflowWel.
Can you write Python code for it?

WEL_PACKAGE = flopy.modflow.ModflowWel(MF, stress_period_data=[0,4,5,WELLQ])
#note that we had to account for the additional column when compared to the location specified in P4.4


I am working on the file "P4.6_Flopy_Hubbertville_no_flow_BC_test.ipynb".
The first task is:
Writing the MODFLOW Input Files
Before we create the model input datasets, we can do some directory cleanup to make sure that we don't accidently use old files.
Can you write Python code for it?

#Before writing input, destroy all files in folder to prevent reusing old files
#Here's the working directory
print modelpath
#Here's what's currently in the working directory
modelfiles = os.listdir(modelpath)
print modelfiles

#delete these files to prevent us from reading old results
modelfiles = os.listdir(modelpath)
for filename in modelfiles:
    f = os.path.join(modelpath, filename)
    if modelname in f:
        try:
            os.remove(f)
            print 'Deleted: ', filename
        except:
            print 'Unable to delete: ', filename

#Now write the model input files
MF.write_input()


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
2.3. Numpy methods that can be carried out along different dimensions
Compare the result of the following commands:
Can you write Python code for it?

print z_4_2.shape
print np.mean(z_4_2)
print np.mean(z_4_2,axis=0)
print np.mean(z_4_2,axis=1)


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
2.5. Slicing
Particular elements of numpy arrays (both unidimensional and multidimensional) can be accessed using standard python slicing. When working with multidimensional arrays, slicing can be carried out along the different dimensions at once
Can you write Python code for it?

# Keep last row of matrix X
X_sub1 = <FILL IN>

# Keep first column of the three first rows of X
X_sub2 = <FILL IN>

# Keep first two columns of the three first rows of X
X_sub3 = <FILL IN>

# Invert the order of the rows of X
X_sub4 = <FILL IN>

Test.assertEqualsHashed(X_sub1,'0bcf8043a3dd569b31245c2e991b26686305b93f','Wrong value for variable X_sub1')
Test.assertEqualsHashed(X_sub2,'7c43c1137480f3bfea7454458fcfa2bc042630ce','Wrong value for variable X_sub2')
Test.assertEqualsHashed(X_sub3,'3cddc950ea2abc256192461728ef19d9e1d59d4c','Wrong value for variable X_sub3')
Test.assertEqualsHashed(X_sub4,'33190dec8f3cbe3ebc9d775349665877d7b892dd','Wrong value for variable X_sub4')


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
2.6 Matrix inversion
Non singular matrices can be inverted with method np.linalg.inv(). Invert square matrices $X\cdot X^\top$ and $X^\top \cdot X$, and see what happens when trying to invert a singular matrix. The rank of a matrix can be studied with method numpy.linalg.matrix_rank().
Can you write Python code for it?

print X.shape
print X.dot(X.T)
print X.T.dot(X)

print np.linalg.inv(X.T.dot(X))
#print np.linalg.inv(X.dot(X.T))


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
2.7 Exercises
In this section, you will complete three exercises where you will carry out some common operations when working with data structures. For this exercise you will work with the 2-D numpy array X, assuming that it contains the values of two different variables for 8 data patterns. A first column of ones has already been introduced in a previous exercise:
$$X = \left[ \begin{array}{ccc} 1 & x_1^{(1)} & x_2^{(1)} \ 1 & x_1^{(2)} & x_2^{(2)} \ \vdots & \vdots & \vdots \ 1 & x_1^{(8)} & x_2^{(8)}\end{array}\right]$$
First of all, let us check that you are working with the right matrix
Can you write Python code for it?

Test.assertEqualsHashed(X,'426c2708350ac469bc2fc4b521e781b36194ba23','Wrong value for variable X')


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
2.7.1. Non-linear transformations
Create a new matrix Z, where additional features are created by carrying out the following non-linear transformations:
$$Z = \left[ \begin{array}{ccc} 1 & x_1^{(1)} & x_2^{(1)} & \log\left(x_1^{(1)}\right) & \log\left(x_2^{(1)}\right)\ 1 & x_1^{(2)} & x_2^{(2)} & \log\left(x_1^{(2)}\right) & \log\left(x_2^{(2)}\right) \ \vdots & \vdots & \vdots \ 1 & x_1^{(8)} & x_2^{(8)} & \log\left(x_1^{(8)}\right) & \log\left(x_2^{(8)}\right)\end{array}\right] = \left[ \begin{array}{ccc} 1 & z_1^{(1)} & z_2^{(1)} & z_3^{(1)} & z_4^{(1)}\ 1 & z_1^{(2)} & z_2^{(2)} & z_3^{(1)} & z_4^{(1)} \ \vdots & \vdots & \vdots \ 1 & z_1^{(8)} & z_2^{(8)} & z_3^{(1)} & z_4^{(1)} \end{array}\right]$$
In other words, we are calculating the logarightmic values of the two original variables. From now on, any function involving linear transformations of the variables in Z, will be in fact a non-linear function of the original variables.
Can you write Python code for it?

# Obtain matrix Z
Z = <FILL IN>

Test.assertEqualsHashed(Z,'d68d0394b57b4583ba95fc669c1c12aeec782410','Incorrect matrix Z')


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
2.7.3. Model evaluation
Finally, we can use previous data matrices Z and Z_poly to efficiently compute the output of the corresponding non-linear models over all the patterns in the data set. In this exercise, we consider the two following linear-in-the-parameters models to be evaluated:
$$f_\text{log}({\bf x}) = w_0 + w_1 \cdot x_1 + w_2 \cdot x_2 + w_3 \cdot \log(x_1) + w_4 \cdot \log(x_2)$$
$$f_\text{poly}({\bf x}) = w_0 + w_1 \cdot x_1 + w_2 \cdot x_1^2 + w_3 \cdot x_1^3$$
Compute the output of the two models for the particular weights that are defined in the code below. Your output variables f_log and f_poly should contain the outputs of the model for all eight patterns in the data set.
Can you write Python code for it?

w_log = np.array([3.3, 0.5, -2.4, 3.7, -2.9])
w_poly = np.array([3.2, 4.5, -3.2, 0.7])

f_log = <FILL IN>
f_poly = <FILL IN>

Test.assertEqualsHashed(f_log,'cf81496c5371a0b31931625040f460ed3481fb3d','Incorrect evaluation of the logarithmic model')
Test.assertEqualsHashed(f_poly,'05307e30124daa103c970044828f24ee8b1a0bb9','Incorrect evaluation of the polynomial model')


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
3.2. Labeled point

An associaation of a local vector and a label
The label is a double (also in classification)
Supervised MLlib methods rely on datasets of labeled points
In regression,the label can be any real number
In classification, labels are class indices starting from zero: 0, 1, 2, ...

Labeled point constructor takes two arguments: the labels, and a numpy array / DenseVector / SparseVector containing the features.
Can you write Python code for it?

# Create a labeled point with a positive label and a dense feature vector.
pos = LabeledPoint(1.0, [1.0, 0.0, 3.0])

# Create a labeled point with a negative label and a sparse feature vector.
neg = LabeledPoint(0.0, sparse_V)

# You can now easily access the label and features of the vector:

print 'The label of the first labeled point is', pos.label
print 'The features of the second labeled point are', neg.features


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
DenseVectors can be created from lists or from numpy arrays
SparseVector constructor requires three arguments: the length of the vector, an array with the indices of the non-zero coefficients, and the values of such positions (in the same order)
Can you write Python code for it?

# We create a sparse vector of length 900, with only 25 non-zero values
Z = np.eye(30, k=5).flatten()
print 'The dimension of array Z is ', Z.shape

# Create a DenseVector containing the elements of array Z
dense_V = <FILL IN>

#Create a SparseVector containing the elements of array Z
#Nonzero elements are indexed by the following variable idx_nonzero
idx_nonzero = np.nonzero(Z)[0]
sparse_V = <FILL IN>

#Standard matrix operations can be computed on DenseVectors and SparseVectors
#Calculate the square norm of vector sparse_V, by multiplying sparse_V by the transponse of dense_V
print 'The norm of vector Z is', sparse_V.dot(dense_V)

#print sparse_V
#print dense_V

Test.assertEqualsHashed(dense_V,'b331f43b23fda1ac19f5c29ee2c843fab6e34dfa', 'Incorrect vector dense_V')
Test.assertEqualsHashed(sparse_V,'954fe70f3f9acd720219fc55a30c7c303d02f05d', 'Incorrect vector sparse_V')
Test.assertEquals(type(dense_V),pyspark.mllib.linalg.DenseVector,'Incorrect type for dense_V')
Test.assertEquals(type(sparse_V),pyspark.mllib.linalg.SparseVector,'Incorrect type for sparse_V')


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
Numpy arrays can be defined directly using methods such as np.arange(), np.ones(), np.zeros(), as well as random number generators. Alternatively, you can easily generate them from python lists (or lists of lists) containing elements of numeric type.
You can easily check the shape of any numpy vector with the property .shape, and reshape it with the method reshape(). Note the difference between 1-D and N-D numpy arrays (ndarrays). You should also be aware of the existance of another numpy data type: Numpy matrices (http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.matrix.html) are inherently 2-D structures where operators * and ** have the meaning of matrix multiplication and matrix power.
In the code below, you can check the types and shapes of different numpy arrays. Complete also the exercise where you are asked to convert a unidimensional array into a vector of size $4\times2$.
Can you write Python code for it?

# Numpy arrays can be created from numeric lists or using different numpy methods
y = np.arange(8)+1
x = np.array(x_f)

# Check the different data types involved
print 'El tipo de la variable x_f es ', type(x_f)
print 'El tipo de la variable x es ', type(x)
print 'El tipo de la variable y es ', type(y)

# Print the shapes of the numpy arrays
print 'La variable y tiene dimensiones ', y.shape
print 'La variable x tiene dimensiones ', x.shape

#Complete the following exercises
# Convert x into a variable x_matrix, of type `numpy.matrixlib.defmatrix.matrix` using command
# np.matrix(). The resulting matrix should be of dimensions 4x1
x_matrix = <FILL IN>
# Convert x into a variable x_array, of type `ndarray`, and dimensions 4x2
x_array = <FILL IN>
# Reshape array y into a 4x2 matrix using command np.reshape()
y = <FILL IN>

Test.assertEquals(type(x_matrix),np.matrixlib.defmatrix.matrix,'x_matrix is not defined as a matrix')
Test.assertEqualsHashed(x_matrix,'f4239d385605dc62b73c9a6f8945fdc65e12e43b','Incorrect variable x_matrix')
Test.assertEquals(type(x_array),np.ndarray,'x_array is not defined as a numpy ndarray')
Test.assertEqualsHashed(x_array,'f4239d385605dc62b73c9a6f8945fdc65e12e43b','Incorrect variable x_array')
Test.assertEquals(type(y),np.ndarray,'y is not defined as a numpy ndarray')
Test.assertEqualsHashed(y,'66d90401cb8ed9e1b888b76b0f59c23c8776ea42','Incorrect variable y')


I am working on the file "P4.MLLib_DataTypes/Alumnos/numpy_exercises_alumnos.ipynb".
The first task is:
Other numpy methods where you can specify the axis along with a certain operation should be carried out are:

np.median()
np.std()
np.var()
np.percentile()
np.sort()
np.argsort()

If the axis argument is not provided, the array is flattened before carriying out the corresponding operation.
2.4. Concatenating matrices and vectors
Provided that the necessary dimensions fit, horizontal and vertical stacking of matrices can be carried out with methods np.hstack() and np.vstack().
Complete the following exercises to practice with matrix concatenation:
Can you write Python code for it?

# Previous check that you are working with the right matrices
Test.assertEqualsHashed(z_4_2,'607730d96899ee27af576ecc7a4f1105d5b2cfed','Wrong value for variable z_4_2')
Test.assertEqualsHashed(x_array,'f4239d385605dc62b73c9a6f8945fdc65e12e43b','Wrong value for variable x_array')

# Vertically stack matrix z_4_2 with itself
ex1_res = <FILL IN>

# Horizontally stack matrix z_4_2 and vector x_array
ex2_res = <FILL IN>

# Horizontally stack a column vector of ones with the result of the first exercise (variable ex1_res)
X = <FILL IN>

Test.assertEqualsHashed(ex1_res,'31e60c0fa3e3accedc7db24339452085975a6659','Wrong value for variable ex1_res')
Test.assertEqualsHashed(ex2_res,'189b90c5b2113d2415767915becb58c6525519b7','Wrong value for variable ex2_res')
Test.assertEqualsHashed(X,'426c2708350ac469bc2fc4b521e781b36194ba23','Wrong value for variable X')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
1. Objectives
This notebook reviews some of the Python modules that make it possible to work with data structures in an easy an efficient manner. We will start by reviewing Numpy arrays and matrices, and some of the common operations which are needed when working with these data structures in Machine Learning.  The second part of the notebook will present some of the data types inherent to MLlib, and explain the basics of distributing data sets for parallel optimization of models
2. Numpy exercises
2.1. Create numpy arrays of different types
The following code fragment defines variable x as a list of 4 integers, you can check that by printing the type of any element of x. Use python command map() to create a new list with the same elements as x, but where each element of the list is a float.
Can you write Python code for it?

x = [5, 4, 3, 4]
print type(x[0])

# Create a list of floats containing the same elements as in x
# x_f = <FILL IN>
x_f = map(float, x)

Test.assertTrue(np.all(x == x_f), 'Elements of both lists are not the same')
Test.assertTrue(((type(x[-2])==int) & (type(x_f[-2])==float)),'Type conversion incorrect')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
2.2. Products and powers of numpy arrays and matrices

* and ** when used with Numpy arrays implement elementwise product and exponentiation
* and ** when used with Numpy matrices implement matrix product and exponentiation
Method np.dot() implements matrix multiplication, and can be used both with numpy arrays and matrices.

So you have to be careful about the types you are using for each variable
Can you write Python code for it?

# Try to run the following command on variable x_matrix, and see what happens
print x_array**2

# Try to run the following command on variable x_matrix, and see what happens
print 'Remember that the shape of x_array is ', x_array.shape
print 'Remember that the shape of y is ', y.shape

# Complete the following exercises. You can print the partial results to visualize them

# Multiply the 2-D array `y` by 2
y_by2 = y * 2
#y_by2 = <FILL IN>

# Multiply each of the columns in `y` by the column vector x_array
z_4_2 = x_array * y
#z_4_2 = <FILL IN>

# Obtain the matrix product of the transpose of x_array and y
x_by_y = x_array.T.dot(y)
#x_by_y = <FILL IN>

# Repeat the previous calculation, this time using x_matrix (of type numpy matrix) instead of x_array
# Note that in this case you do not need to use method dot()
x_by_y2 = x_matrix.T * y
#x_by_y2 = <FILL IN>

# Multiply vector x_array by its transpose to obtain a 4 x 4 matrix
x_4_4 = x_array.dot(x_array.T)
#x_4_4 = <FILL IN>

# Multiply the transpose of vector x_array by vector x_array. The result is the squared-norm of the vector
x_norm2 = x_array.T.dot(x_array)
#x_norm2 = <FILL IN>

Test.assertEqualsHashed(y_by2,'120a3a46cdf65dc239cc9b128eb1336886c7c137','Incorrect result for variable y_by2')
Test.assertEqualsHashed(z_4_2,'607730d96899ee27af576ecc7a4f1105d5b2cfed','Incorrect result for variable z_4_2')
Test.assertEqualsHashed(x_by_y,'a3b24f229d1e02fa71e940adc0a4135779864358','Incorrect result for variable x_by_y')
Test.assertEqualsHashed(x_by_y2,'a3b24f229d1e02fa71e940adc0a4135779864358','Incorrect result for variable x_by_y2')
Test.assertEqualsHashed(x_4_4,'fff55c032faa93592e5d27bf13da9bb49c468687','Incorrect result for variable x_4_4')
Test.assertEqualsHashed(x_norm2,'6eacac8f346bae7b5c72bcc3381c7140eaa98b48','Incorrect result for variable x_norm2')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
2.3. Numpy methods that can be carried out along different dimensions
Compare the result of the following commands:
Can you write Python code for it?


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
2.6 Matrix inversion
Non singular matrices can be inverted with method np.linalg.inv(). Invert square matrices $X\cdot X^\top$ and $X^\top \cdot X$, and see what happens when trying to invert a singular matrix. The rank of a matrix can be studied with method numpy.linalg.matrix_rank().
Can you write Python code for it?


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
2.7.1. Non-linear transformations
Create a new matrix Z, where additional features are created by carrying out the following non-linear transformations:
$$Z = \left[ \begin{array}{ccc} 1 & x_1^{(1)} & x_2^{(1)} & \log\left(x_1^{(1)}\right) & \log\left(x_2^{(1)}\right)\ 1 & x_1^{(2)} & x_2^{(2)} & \log\left(x_1^{(2)}\right) & \log\left(x_2^{(2)}\right) \ \vdots & \vdots & \vdots \ 1 & x_1^{(8)} & x_2^{(8)} & \log\left(x_1^{(8)}\right) & \log\left(x_2^{(8)}\right)\end{array}\right] = \left[ \begin{array}{ccc} 1 & z_1^{(1)} & z_2^{(1)} & z_3^{(1)} & z_4^{(1)}\ 1 & z_1^{(2)} & z_2^{(2)} & z_3^{(1)} & z_4^{(1)} \ \vdots & \vdots & \vdots \ 1 & z_1^{(8)} & z_2^{(8)} & z_3^{(1)} & z_4^{(1)} \end{array}\right]$$
In other words, we are calculating the logarightmic values of the two original variables. From now on, any function involving linear transformations of the variables in Z, will be in fact a non-linear function of the original variables.
Can you write Python code for it?

# Obtain matrix Z
Z = np.hstack((X,np.log(X[:,1:])))
#Z = <FILL IN>

Test.assertEqualsHashed(Z,'d68d0394b57b4583ba95fc669c1c12aeec782410','Incorrect matrix Z')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
2.7.3. Model evaluation
Finally, we can use previous data matrices Z and Z_poly to efficiently compute the output of the corresponding non-linear models over all the patterns in the data set. In this exercise, we consider the two following linear-in-the-parameters models to be evaluated:
$$f_\text{log}({\bf x}) = w_0 + w_1 \cdot x_1 + w_2 \cdot x_2 + w_3 \cdot \log(x_1) + w_4 \cdot \log(x_2)$$
$$f_\text{poly}({\bf x}) = w_0 + w_1 \cdot x_1 + w_2 \cdot x_1^2 + w_3 \cdot x_1^3$$
Compute the output of the two models for the particular weights that are defined in the code below. Your output variables f_log and f_poly should contain the outputs of the model for all eight patterns in the data set.
Can you write Python code for it?

w_log = np.array([3.3, 0.5, -2.4, 3.7, -2.9])
w_poly = np.array([3.2, 4.5, -3.2, 0.7])

f_log = Z_lambda.dot(w_log)
f_poly = Z_poly.dot(w_poly)
#f_log = <FILL IN>
#f_poly = <FILL IN>

Test.assertEqualsHashed(f_log,'cf81496c5371a0b31931625040f460ed3481fb3d','Incorrect evaluation of the logarithmic model')
Test.assertEqualsHashed(f_poly,'05307e30124daa103c970044828f24ee8b1a0bb9','Incorrect evaluation of the polynomial model')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
3. MLlib Data types
MLlib is Apache Spark's scalable machine learning library. It implements several machine learning methods that can work over data distributed by means of RDDs. The regression methods that are part of MLlib are:

linear least squares
Lasso
ridge regression
isotonic regression
random forests
gradient-boosted trees

We will just use the three first methods, and we will also work on an implementation of KNN regression over Spark, using the Data types provided by MLlib.
3.1. Local Vectors

Integer-typed and 0-based indices
Double-typed values
Stored on a single machine
Two kinds of vectors provided:
DenseVector: a double array with the entries values
SparseVector: backed up by two parallel arrays: indices and values

<img src="./figs/vector_representation.jpg" width="80%">
Can you write Python code for it?

# Import additional libraries for this part

from pyspark.mllib.linalg import DenseVector
from pyspark.mllib.linalg import SparseVector
from pyspark.mllib.regression import LabeledPoint


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
3.2. Labeled point

An associaation of a local vector and a label
The label is a double (also in classification)
Supervised MLlib methods rely on datasets of labeled points
In regression,the label can be any real number
In classification, labels are class indices starting from zero: 0, 1, 2, ...

Labeled point constructor takes two arguments: the labels, and a numpy array / DenseVector / SparseVector containing the features.
Can you write Python code for it?


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
DenseVectors can be created from lists or from numpy arrays
SparseVector constructor requires three arguments: the length of the vector, an array with the indices of the non-zero coefficients, and the values of such positions (in the same order)
Can you write Python code for it?

# We create a sparse vector of length 900, with only 25 non-zero values
Z = np.eye(30, k=5).flatten()
print 'The dimension of array Z is ', Z.shape

# Create a DenseVector containing the elements of array Z
dense_V = DenseVector(Z)
#dense_V = <FILL IN>

#Create a SparseVector containing the elements of array Z
#Nonzero elements are indexed by the following variable idx_nonzero
idx_nonzero = np.nonzero(Z)[0]
sparse_V = SparseVector(Z.shape[0], idx_nonzero, Z[idx_nonzero])
#sparse_V = <FILL IN>

#Standard matrix operations can be computed on DenseVectors and SparseVectors
#Calculate the square norm of vector sparse_V, by multiplying sparse_V by the transponse of dense_V
print 'The norm of vector Z is', sparse_V.dot(dense_V)

#print sparse_V
#print dense_V

Test.assertEqualsHashed(dense_V,'b331f43b23fda1ac19f5c29ee2c843fab6e34dfa', 'Incorrect vector dense_V')
Test.assertEqualsHashed(sparse_V,'954fe70f3f9acd720219fc55a30c7c303d02f05d', 'Incorrect vector sparse_V')
Test.assertEquals(type(dense_V),pyspark.mllib.linalg.DenseVector,'Incorrect type for dense_V')
Test.assertEquals(type(sparse_V),pyspark.mllib.linalg.SparseVector,'Incorrect type for sparse_V')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
If you did not do that, repeat the previous exercise, this time using the map() method together with function log_transform():
Can you write Python code for it?

def log_transform(x):
    return np.hstack((x,np.log(x[1]),np.log(x[2])))
    #return <FILL IN>

Z_map = np.array(map(log_transform,X))

Test.assertEqualsHashed(Z_map,'d68d0394b57b4583ba95fc669c1c12aeec782410','Incorrect matrix Z')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
Numpy arrays can be defined directly using methods such as np.arange(), np.ones(), np.zeros(), as well as random number generators. Alternatively, you can easily generate them from python lists (or lists of lists) containing elements of numeric type.
You can easily check the shape of any numpy vector with the property .shape, and reshape it with the method reshape(). Note the difference between 1-D and N-D numpy arrays (ndarrays). You should also be aware of the existance of another numpy data type: Numpy matrices (http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.matrix.html) are inherently 2-D structures where operators * and ** have the meaning of matrix multiplication and matrix power.
In the code below, you can check the types and shapes of different numpy arrays. Complete also the exercise where you are asked to convert a unidimensional array into a vector of size $4\times2$.
Can you write Python code for it?

# Numpy arrays can be created from numeric lists or using different numpy methods
y = np.arange(8)+1
x = np.array(x_f)

# Check the different data types involved
print 'El tipo de la variable x_f es ', type(x_f)
print 'El tipo de la variable x es ', type(x)
print 'El tipo de la variable y es ', type(y)

# Print the shapes of the numpy arrays
print 'La variable y tiene dimensiones ', y.shape
print 'La variable x tiene dimensiones ', x.shape

#Complete the following exercises
# Convert x into a variable x_matrix, of type `numpy.matrixlib.defmatrix.matrix` using command
# np.matrix(). The resulting matrix should be of dimensions 4x1
x_matrix = np.matrix(x).T
#x_matrix = <FILL IN>
# Convert x into a variable x_array, of type `ndarray`, and dimensions 4x2
x_array = x[:,np.newaxis]
#x_array = <FILL IN>
# Reshape array y into a 4x2 matrix using command np.reshape()
y = y.reshape((4,2))
#y = <FILL IN>

Test.assertEquals(type(x_matrix),np.matrixlib.defmatrix.matrix,'x_matrix is not defined as a matrix')
Test.assertEqualsHashed(x_matrix,'f4239d385605dc62b73c9a6f8945fdc65e12e43b','Incorrect variable x_matrix')
Test.assertEquals(type(x_array),np.ndarray,'x_array is not defined as a numpy ndarray')
Test.assertEqualsHashed(x_array,'f4239d385605dc62b73c9a6f8945fdc65e12e43b','Incorrect variable x_array')
Test.assertEquals(type(y),np.ndarray,'y is not defined as a numpy ndarray')
Test.assertEqualsHashed(y,'66d90401cb8ed9e1b888b76b0f59c23c8776ea42','Incorrect variable y')


I am working on the file "P4.MLLib_DataTypes/Profesor/numpy_exercises.ipynb".
The first task is:
Repeat the previous exercise once again using a lambda function:
Can you write Python code for it?

Z_lambda = np.array(map(lambda x: np.hstack((x,np.log(x[1]),np.log(x[2]))),X))
#Z_lambda = np.array(map(lambda x: <FILL IN>,X))

Test.assertEqualsHashed(Z_lambda,'d68d0394b57b4583ba95fc669c1c12aeec782410','Incorrect matrix Z')


I am working on the file "P4B - Capitolo 1.1.ipynb".
The first task is:
DIZIONARI: Operazioni e metodi
Can you write Python code for it?

# -*- coding: utf-8 -*-

# definizione
d = {"nome": "Luca",
     "cognome": "Ferroni",
     "classe": 1980,
     2: "figli",
     (2017, 1, 23): "corso python",
    "classe": 1979}

print(d)
# aggiornamento
d.update({
    "professioni" : ["docente", "lavoratore autonomo"]
})

d["pranzo"] = "ritrovo degli artisti"

# recupero valore per chiave certa (__getitem__)
print(u"Il nome del personaggio è {}".format(d["nome"]))

# sfrutto il mini-formato di template per le stringhe
# https://docs.python.org/2.7/library/string.html#formatspec
print(u"Il personaggio è {nome} {cognome} nato nel {classe}".format(**d))

# Recupero di un valore per una chiave opzionale
print(u"'nome' è una chiave che esiste con valore = {}, 'codiceiban' invece non esiste = {}".format(
    d.get('nome'), d.get('codiceiban')))

print(u"Se avessi usato la __getitem__ avrei avuto un KeyError")

# rimozione di una chiave dal dizionario
print(u"Rimuovo il nome dal dizionario con d.pop('nome')")
d.pop('nome')

print(u"'nome' ora non esiste con valore = {}, come 'codiceiban' = {}".format(
    d.get('nome'), d.get('codiceiban')))

print(u"Allora, se non trovi la chiave 'nome' allora dimmi 'Pippo'. Cosa dici?")
print(d.get('nome', 'Pippo'))


I am working on the file "P4B - Capitolo 1.1.ipynb".
The first task is:
DYI: Anagrafica e Fibonacci
Sapendo che l'input all'utente si chiede con la funzione raw_input(<richiesta>), scrivere una funzione che richieda nome, città, stipendio, e generazione di Fibonacci dei propri conigli.
Can you write Python code for it?

def main():
    # Step 1. Finché l'utente non scrive STOP - si fa tutto in un while True con un break quando occorre
    # Step 2. L'utente inserisce il nome
    #         Usa raw_input("Inserisci ...") per chiedere le info all'utente
    # Step 3. L'utente inserisce la città
    # Step 4. L'utente inserisce lo stipendio
    # Step 5. Inserisci il dizionario con chiavi
    #   'name', 'city', 'salary', 'genfibo'
    #   nella lista PEOPLE = []
    PEOPLE.append(person_d)
    # Step 6. Stampa a video PEOPLE nel modo che ti piace
    # Step 7. Riinizia da Step 1
    # FINE

    # ---- BONUS ----
    # Step 8. Quando l'utente smette -> scrivi i dati in un file
    #   se vuoi Step 8.1 in formato json
    #   se vuoi Step 8.2 in formato csv
    #   se vuoi Step 8.3 in formato xml
    # Step 9. Fallo anche se l'utente preme CTRL+C o CTRL+Z


I am working on the file "P4B - Capitolo 1.1.ipynb".
The first task is:
DYI: Fibonacci optimized
Salvare i calcoli intermedi della funzione di Fibonacci in un dizionario da usare come cache.
Eseguire i test per essere sicuri di non aver rotto l'algoritmo
Caratteristiche del modello dati di Python
Tipi di dato "mutable" e "immutable"
Python Data Model
Ogni oggetto ha:
   * identità -> non cambia mai e si può pensare come l'indirizzo in memoria
   * tipo -> non cambia mai e rappresenta le operazioni che l'oggetto supporta
   * valore -> può cambiare se il tipo è mutable, non se è immutable
Tipi di dato immutable sono:

interi
stringhe
tuple
set

Tipi di dato mutable sono:

liste
dizionari

Tipizzazione forte e dinamica
Da http://stackoverflow.com/questions/11328920/is-python-strongly-typed/11328980#11328980 (v. anche i commenti)
Python is strongly, dynamically typed.

Strong typing means that the type of a value doesn't suddenly change. A string containing only digits doesn't magically become a number, as may happen in Perl. Every change of type requires an explicit conversion.
Dynamic typing means that runtime objects (values) have a type, as opposed to static typing where variables have a type.

As for example
bob = 1
bob = "bob"
This works because the variable does not have a type; it can name any object. After bob=1, you'll find that type(bob) returns int, but after bob="bob", it returns str. (Note that type is a regular function, so it evaluates its argument, then returns the type of the value.)
Passaggio di parametro per valore o riferimento?
Nessuno dei due! V. https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/
Call by object, or call by object reference.
Concetto base: in python una variabile è solo un nome per un oggetto (= la tripla id,tipo,valore)
In sostanza il comportamento dipende dal fatto che gli oggetti nominati dalle variabili sono mutable o immutable.
Seguono esempi:
Can you write Python code for it?

def foo(bar):
    bar.append(41)
    print(bar)

    # >> [41]

answer_list = []
foo(answer_list)
print(answer_list)
# >> [41]

def foo(bar):
    bar = 'new value'
    print (bar)
    # >> 'new value'

answer_list = 'old value'
foo(answer_list)
print(answer_list)
# >> 'old value'


I am working on the file "P4B - Capitolo 1.1.ipynb".
The first task is:
Funzioni con parametri posizionali, nominali, e arbitrari
Una funzione si definisce con def <nomefunzione>([parametri]) dove i parametri possono essere:

posizionali. Ad es: def hello(who)
nominali. Ad es: def hello(who='') o who=None o who='default'
entrambi, ma i nominali devono essere messi dopo i posizionali. Ad es: def hello(who, say="How are you?")
arbitrari sia posizionali con il simbolo * o nominali con **. Come convenzione si utilizzano i nomi args e kw o kwargs. Ad es: def hello(who, say="How are you?", *args, **kw)

I simboli * e ** indicano rispettivamente la rappresentazione di una lista come una sequenza di elementi, e di un dizionario come una sequenza di parametri <chiave>=<valore>
Scope delle variabili
http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/
e ricordatevi che:
for i in [1,2,3]:
      print(i)
print("Sono fuori dal ciclo e posso vedere che i={}".format(i))
Namespace
I namespace in python sono raccoglitori di nomi e posson essere impliciti o espliciti. Sono impliciti lo spazio dei nomi __builtin__ e __main__. Sono espliciti, le classi, gli oggetti, le funzioni e in particolare i moduli.
Posso importare un modulo che mi va a costituire un namespace con import <nomemodulo> e accedere a tutti i simboli top-level inseriti nel modulo come <nomemodulo>.<simbolo>.
L'importazione di simboli singoli all'interno di un modulo in un altro namespace si può fare con from <nomemodulo> import <simbolo>. Quello che non si dovrebbe fare è importare tutti i simboli di un modulo dentro un altro nella forma: from <nomemodulo> import *. Non fatelo, a meno che non strettamente necessario.
Stack delle eccezioni e loro gestione
Lo stack delle eccezioni builtin, ossia già comprese nel linguaggio python sono al link: https://docs.python.org/2/library/exceptions.html#exception-hierarchy
Derivando da essere facilmente se ne possono definire di proprie.
La gestione delle eccezioni avviene in blocchi:
try:
    ...
except [eccezione] [as...
Can you write Python code for it?

# -*- coding: utf-8 -*-

# This is hello_who_3.py
import sys                            # <-- importo un modulo

def compose_hello(who, force=False):   # <-- valore di default
    """
    Get the hello message.
    """
    try:                                     # <-- gestione eccezioni `Duck Typing`
        message = "Hello " + who + "!"
    except TypeError:                       # <-- eccezione specifica
    # except TypeError as e:                       # <-- eccezione specifica su parametro e
        print("[WARNING] Il parametro `who` dovrebbe essere una stringa")
        if force:                            # <-- controllo "if"
            message = "Hello {}!".format(who)
        else:
            raise                            # <-- solleva eccezione originale
    except Exception:
        print("Verificatasi eccezione non prevista")
    else:
        print("nessuna eccezione")
    finally:
        print("Bye")

    return message

def hello(who='world'):               # <-- valore di default
    print(compose_hello(who))

if __name__ == "__main__":
    hello("mamma")

hello("pippo")

hello(1)

ret = compose_hello(1, force=True)
print("Ha composto {}".format(ret))

try:
    hello(1)
except TypeError as e:
    print("{}: {}".format(type(e).__name__, e))
    print("Riprova")


I am working on the file "P4B - Capitolo 1.1.ipynb".
The first task is:
Iterazione nei dizionari
ATTENZIONE: Il contenuto del dizionario non è ordinato! Non c'è alcuna garanzia sull'ordinamento. Per avere garanzia bisogna usare la classe collections.OrderedDict
Can you write Python code for it?

print("\n***PER ITERARE SU TUTTI GLI ELEMENTI DI UN DIZIONARIO SI USA .iteritems()***\n")

for key, value in d.iteritems():
    print("Alla chiave {} corrisponde il valore {}".format(key,value))

print("\n***DIZIONARI E ORDINAMENTO***\n")
data_input = [('a', 1), ('b', 2), ('l', 10), ('c', 3)]
d1 = dict(data_input)

import collections

d2_ord = collections.OrderedDict(data_input)

print("input = {}".format(data_input))
print("dizionario non ordinato = {}".format(d1))
print("dizionario ordinato = {}".format(d2_ord))
print("lista di coppie da diz NON ordinato = {}".format(d1.items()))
print("lista di coppie da diz ordinato = {}".format(d2_ord.items()))


I am working on the file "P4B - Capitolo 1.1.ipynb".
The first task is:
Iterazione nelle liste e cicli for su indice
Can you write Python code for it?

print("***PER FARE UN CICLO FOR CON INDICE INCREMENTALE SI USA XRANGE!")

for el in xrange(1,21):
    print("numero {}".format(el))

print("***PER NUMERARE GLI ELEMENTI DI UNA LISTA SI USA ENUMERATE!")

for i, el in enumerate(l, start=10):  # numero partendo da 10, se start non specificato parto da 0
    print("Il contenuto {} si trova all'indice {}".format(el, i))


I am working on the file "P4B - Capitolo 1.ipynb".
The first task is:
IPython compagno di sviluppo
$ ipython

Tip: scopri lo Zen di Python PEP 20
Can you write Python code for it?

# This is hello_who.py                # <-- i commenti iniziano con `#`
                                      #     possono essere all'inizio o a fine riga
def hello(who):                       # <-- la funzione di definisce con `def`
    print("Hello {}!".format(who))    # <-- la stampa con `print` e le stringhe con `format`

if __name__ == "__main__":            # <-- [verifica di esecuzione e non di import](https://docs.python.org/2/library/__main__.html)
    hello("mamma")                    # <-- invoco la funzione con il valore


# This is hello_who.py                # <-- i commenti iniziano con `#`
                                      #     possono essere all'inizio o a fine riga
def hello(who):                       # <-- la funzione di definisce con `def`
    print("Hello {}!".format(who))    # <-- la stampa con `print` e le stringhe con `format`
    print("Hello %s!" % who)    # <-- la stampa con `print` e le stringhe con `format`

if __name__ == "__main__":            # <-- [verifica di esecuzione e non di import](https://docs.python.org/2/library/__main__.html)
    hello("mamma")                    # <-- invoco la funzione con il valore


I am working on the file "P4B - Capitolo 1.ipynb".
The first task is:
La leggibilità conta

Le linee guida di stile sono nel PEP 8
ogni azienda può averne di differenti

Le classiche sono:

Indentazione con 4 spazi, e non <TAB>: settate il vostro editor!
Lunghezza della riga <= 79 caratteri
Spazi intorno agli operatori

Le convenzioni per le docstring sono descritte nel PEP 257
in particolare:

Se monolinea scrivere tutto su una riga
Se multilinea il separatore finale (""") deve essere su una linea separata.

Mie convenzioni:

classi CamelCase e funzioni + variabili minuscole con _
il codice è scritto in inglese, in particolare i nomi delle variabili

I tipi di dato

numeri
stringhe
tuple: (1, 2*"a", "prova", u"ciaàà")
set: { 2, 4, 6,"a", 123 }
liste: [1,2,3,10,"a", -12.333]
dizionari: { "nome": "Luca", "cognome": "Ferroni"} -> è una tabella chiave:valore

Cicli e strutture di controllo
Can you write Python code for it?

import random

def check_value_and_multiply(n, m):
    """
    Verifica il valore di n in un intervallo
    e lo moltiplica per m.

    Se n > 30 allora restituisce un codice di errore in un numero negativo.

    ATTENZIONE: non è bene usare codici di errore nello stesso canale dove
    sarebbero arrivati i dati corretti, si useranno in futuro le eccezioni.
    Ora ci serve come esempio.
    """
    if n < 10:
        print("n minore di 10")
    elif n > 30:  # elif per gestire else+if, questo sostiuisce anche lo switch/case
        print("n troppo grande")
        n = -1
    else:
        print("un buon valore di n = {}".format(n))

    return n*m

def main():
    while True:
        print("stiamo sorteggiando n")
        n = random.randint(1, 50)
        rv = check_value_and_multiply(n, 3)
        print("Il risultato è = {}".format(rv))
        if rv < 0:
            print("Codice di errore {} rilevato".format(rv))
            break

if __name__ == "__main___":
# ATTENZIONE disabilito l'if perché nel notebook web non funziona ma voi lo dovete mettere
   main()


I am working on the file "P4B - Capitolo 1.ipynb".
The first task is:
Py.test: un'altra suite per il testing
Can you write Python code for it?

import pytest
from myprogram import fib

def test_fib_ok_small():
    assert fib(0) == 0
    assert fib(1) == 1
    assert fib(2) == 1
    assert fib(3) == 2

def test_fib_raise_if_string():
    with pytest.raises(TypeError):
        fib("a")

def test_fib_raises_lt_zero():
    with pytest.raises(ValueError):
        fib(-1)


I am working on the file "P4B - Capitolo 1.ipynb".
The first task is:
Unittest: codice che testa la funzione per noi
Can you write Python code for it?

import unittest

from fibo import calc_fibo

class TestMyFibo(unittest.TestCase):

    def test_base(self):
        self.assertEqual(calc_fibo(0), 0)
        self.assertEqual(calc_fibo(1), 1)

    def test_fibo_small(self):
        self.assertEqual(calc_fibo(6), 8)
        self.assertEqual(calc_fibo(8), 21)

    def test_fibo_big(self):
        self.assertEqual(calc_fibo(20), 6765)
        self.assertEqual(calc_fibo(30), 832040)

    def test_negative(self):
        with self.assertRaises(ValueError):
            calc_fibo(-1)

    def test_string(self):
        with self.assertRaises(TypeError):
            calc_fibo("ciao")
            calc_fibo("12")

if __name__ == '__main__':
    unittest.main()


I am working on the file "P4B - Capitolo 2 - Persistenza, Unicode, OOP e DB.ipynb".
The first task is:
DB-API
Can you write Python code for it?

# [PEP 249](https://www.python.org/dev/peps/pep-0249/)
# v. gestionale/managers02/db.py

class SqliteDBManager(object):

    def _do_export(self, rows):

        cu = self.conn.cursor()

        # KO: Never do this -- insecure!
        # KO: for row in rows:
        # KO:     c.execute("INSERT INTO people VALUES ('{name}','{city}','{salary}')".format(**row))

        # Do this instead
        for row in rows:
            t = (row["name"], row["city"], row["salary"])
            cu.execute('INSERT INTO people VALUES (?,?,?)', t)
        self.conn.commit()


I am working on the file "P4B - Capitolo 2 - Persistenza, Unicode, OOP e DB.ipynb".
The first task is:
Esercizio DB-API

Fai in modo che il tuo gestionale:
    a. inizializzi un tabella PEOPLE su un database sqlite3
    b. vi esporti i dati di PEOPLE
    c. inoltre possa registrare in un'altra tabella i dettagli "name" e "age" dei conigli posseduti
BONUS: Importa un file json (o altro) all'avvio del tuo programma in modo da precaricare un set di dati in PEOPLE
Can you write Python code for it?

import sqlite3 as db

def create_db(dsn):
    conn = db.connect(dsn)
    cu = conn.cursor()
    cu.execute("CREATE TABLE PEOPLE (name VARCHAR(32), city VARCHAR(32), salary INTEGER)")
    conn.close()

def save_db(fname, rows):
    conn = db.connect(fname)
    conn.row_factory = db.Row  # cursore indicizzato anche per chiave
        # è possibile definire una propria classe factory
        # https://docs.python.org/2.7/library/sqlite3.html?highlight=sqlite3#sqlite3.Connection.row_factory
    cu = conn.cursor()
    for row in rows:
        t = list(row[k] for k in ("name", "city", "salary"))
        cu.execute("INSERT INTO PEOPLE VALUES (?,?,?)", t)
    conn.commit()
    conn.close()

# create_db("mydatabase.db")


I am working on the file "P4B - Capitolo 2 - Persistenza, Unicode, OOP e DB.ipynb".
The first task is:
References python 2.7

IL Tutorial
Coding Style

Le funzioni builtins (su ipython >>> help(__builtins__))
Gli HowTo (tra cui "Unicode HowTo" e "Idioms and Anti-Idioms in Python")
The absolute minimum every developer must know about unicode and character sets no excuses!
Pragmatic Unicode
Tipi, operatori e comparazioni
String format mini-language
DB-API 2.0 (PEP 249)
Scope delle variabili (blog)
Test con PyTest

Feedback sono graditi

privati via luca@befair.it , cell: 3289639660
via telegram (sempre con il numero di cell)
se volete farli pubblici su www.befair.it
Can you write Python code for it?

phrase = "happy-python-hacking!"
the_end = u" ".join([s.capitalize() for s in phrase.split("-")])


I am working on the file "P4B - Capitolo 2 - Persistenza, Unicode, OOP e DB.ipynb".
The first task is:
Unicode in breve
mettete sempre la u prima delle stringhe (u"")
v. unicode HowTO nelle references sotto
Can you write Python code for it?


I am working on the file "P5 machine_learning/report_p5.ipynb".
The first task is:
- We can clearly see that the performance values are increasing which indicates these would be a good predictor
-  From the above we can observe the ranking of various features, on adding these features their is a certain increase in accuracy, precision and recall
- SelectKBest was used to rank and select the features .
- GridSearchCV was used to select the appropriate value of k in SelectKBest to give the maximum F1 score for the classifier.
Q3
What algorithm did you end up using? What other one(s) did you try? How did model performance differ between algorithms?
Can you write Python code for it?

### Task 4: Try a varity of classifiers
### Please name your classifier clf for easy export below.
### Note that if you want to do PCA or other multi-stage operations,
### you'll need to use Pipelines.
# Provided to give you a starting point. Try a variety of classifiers.
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif, chi2
from sklearn.feature_selection import RFE
from sklearn import tree
from sklearn.svm import SVC, SVR
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.cross_validation import train_test_split, StratifiedKFold
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
#from sklearn.model_selection import RandomizedSearchCV

parameters={}
parameters["DecisionTreeClassifier"] = [{'min_samples_split': [2,3], 'criterion': [ 'entropy']}]
parameters["GaussianNB"] = [{ 'selection__k':[9,10,11], 'pca__n_components': [2,3,4,5] }]

parameters["SVC"]  = [{'selection__k':[11], 'svc__kernel': ['rbf',"sigmoid"], 'svc__C': [x/1.0 for x in range(1, 100,10)]
                        ,'svc__gamma':[0.1**(x) for x in range(1,9)]}]
parameters["AdaBoostClassifier"] = [{ "base_estimator":[DecisionTreeClassifier(min_samples_split= 2, criterion= 'entropy')],'learning_rate' : [x/30.0 for x in range(1, 30)],'n_estimators' : range(1,100,20),\
                    'algorithm': ['SAMME','SAMME.R'] }]
parameters["KNeighborsClassifier"] = [{'selection__k': [10,11], "knn__p":range(3,4),'pca__n_components': [2,3,4,5],"knn__n_neighbors": range(1,10), 'knn__weights': ['uniform','distance'] ,'knn__algorithm':...


I am working on the file "P5 machine_learning/report_p5.ipynb".
The first task is:
From th above information about dataset we can conlcude that any point with less than 73 non-null  will be having more than 50%
of missing data.
And the features seem to be in more than 50% null group
|Feature|No.of non-null out of 146|
|---|---|
|deferral_payments        |    39 non-null|
|restricted_stock_deferred   | 18 non-null|
|loan_advances          |      4 non-null|
|director_fees            |    17 non-null |
|deferred_income          |    49 non-null |
|long_term_incentive      |    66 non-null |
Can you write Python code for it?

#now checking these features for poi to conclude how much of data is missing for a poi .
missing = ['loan_advances', 'director_fees', 'restricted_stock_deferred',\
           'deferral_payments', 'deferred_income', 'long_term_incentive']
enron_poi=enron[enron['poi']==1][missing]
enron_poi.info()

# its better to remove these with less non null values
removing = ['loan_advances', 'director_fees', 'restricted_stock_deferred']
for x in removing:
    if x in features_list:
        features_list.remove(x)
features_list

### Task 2: Remove outliers
#visualising the outlier

import matplotlib.pyplot

e = enron[(enron.total_payments != np.nan) & (enron.total_stock_value != np.nan)]

matplotlib.pyplot.scatter(x="total_payments", y="total_stock_value", data=e)

matplotlib.pyplot.xlabel("total_payments")
matplotlib.pyplot.ylabel("total_stock_value")
matplotlib.pyplot.show()

# removing outlier
enron.total_payments.idxmax()

#droping total it must be a spreadsheet mistake
enron=enron.drop("TOTAL")

#data_dict.pop( 'TOTAL', 0 )

e = enron[(enron.total_payments != np.nan) & (enron.total_stock_value != np.nan)]

matplotlib.pyplot.scatter(x="total_payments", y="total_stock_value", data=e)
matplotlib.pyplot.xlabel("total_payments")
matplotlib.pyplot.ylabel("total_stock_value")
matplotlib.pyplot.show()
enron.total_payments.idxmax()


I am working on the file "P5 machine_learning/report_p5.ipynb".
The first task is:
I tried to make features of ratios :
$$\frac{from-this-person-to-poi}{from-messages}$$
$$\frac{from-poi-to-this-person}{from-messages}$$
and
$$total-poi-interaction = (shared-receipt-with-poi) + (from-this-person-to-poi) + (from-poi-to-this-person) $$
These features were selected since , a poi is often tend to be in contact of another poi.Which may lead to higher importance of features.And the total  interaction with poi gives a higher chance of finding a poi
Can you write Python code for it?

#Selectkbest used to rank the features
data = featureFormat(my_dataset, features_new_list, sort_keys = True)
labels, features = targetFeatureSplit(data)
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif, chi2
selector = SelectKBest(k='all').fit(features, labels)
results = pd.DataFrame(selector.scores_,
                          index=features_new_list[1:])
results.columns = ['Importances']
results = results.sort(['Importances'], ascending=False)
results

from tester import test_classifier
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier()
if "email_address" in features_new_list:
    features_new_list.remove("email_address")
    feat=features_new_list
else :
    feat=features_list
test_classifier(clf, my_dataset, features_new_list)


I am working on the file "P5 machine_learning/report_p5.ipynb".
The first task is:
LAY KENNETH L  is the next outlier but it is a valid point
Can you write Python code for it?

#After observing insiderpay.pdf file, I got to know it is not a person so we have to remove THE TRAVEL AGENCY IN THE PARK.
enron=enron.drop("THE TRAVEL AGENCY IN THE PARK")

enron[enron[financial_features].isnull().all(axis=1)].index

#There is 1 person without any financial data that will also need to be removed.
enron=enron.drop( 'LOCKHART EUGENE E')

enron = enron.replace(np.nan, 'NaN') # since to use tester code, i needed to convert back to "NaN"
data_dict = enron[features_list].to_dict(orient = 'index')

from tester import test_classifier
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier()
if "email_address" in features_list:
    features_list.remove("email_address")
    feat=features_list
else :
    feat=features_list
test_classifier(clf, data_dict, feat)


I am working on the file "P5 machine_learning/report_p5.ipynb".
The first task is:
Q2
What features did you end up using in your POI identifier, and what selection process did you use to pick them? Did you have to do any scaling? Why or why not? As part of the assignment, you should attempt to engineer your own feature that does not come ready-made in the dataset -- explain what feature you tried to make, and the rationale behind it. (You do not necessarily have to use it in the final analysis, only engineer and test it.) In your feature selection step, if you used an algorithm like a decision tree, please also give the feature importances of the features that you use, and if you used an automated feature selection function like SelectKBest, please report the feature scores and reasons for your choice of parameter values.
Can you write Python code for it?

### Task 3: Create new feature(s)
### Store to my_dataset for easy export below.
my_dataset = data_dict

#Adding three new features
for key, value in my_dataset.items():
    if value['from_messages'] == 'NaN' or value['from_this_person_to_poi'] == 'NaN':
        value['person_to_poi/total_msgs'] = 0.0
    else:
        value['person_to_poi/total_msgs'] = value['from_this_person_to_poi'] / (1.0*value['from_messages'])

    if value['to_messages'] == 'NaN' or value['from_poi_to_this_person'] == 'NaN':
        value['poi_to_person/to_msgs'] = 0.0
    else:
        value['poi_to_person/to_msgs'] = value['from_poi_to_this_person'] / (1.0*value['to_messages'])
    if value['shared_receipt_with_poi'] == 'NaN' or value['from_poi_to_this_person'] == 'NaN' \
        or value['from_this_person_to_poi'] == 'NaN':
        value['total_poi_interaction'] = 0.0
    else:
        value['total_poi_interaction'] = value['shared_receipt_with_poi'] + \
                                      value['from_this_person_to_poi'] + \
                                      value['from_poi_to_this_person']
features_new_list=features_list+['person_to_poi/total_msgs','poi_to_person/to_msgs','total_poi_interaction']


I am working on the file "P5 machine_learning/report_p5.ipynb".
The first task is:
Several different classifiers are deployed:

gaussian naive bayes
k-nearest neighbors
support vector machine
gaussianNB
adaboost

There was not much change in accuracy.
  Highest accuracy was for Knearest,Highest precision is for Knearest,Highest recall is for decision tree ,I ended up with really high precision scores for k-nearest neighbors. Unfortunately the recall scores for these weren't as high andalmost all were equal.  The F1 score was highest for the KNN classifier.
Can you write Python code for it?

### Task 6: Dump your classifier, dataset, and features_list so anyone can
### check your results. You do not need to change anything below, but make sure
### that the version of poi_id.py that you submit can be run on its own and
### generates the necessary .pkl files for validating your results.

dump_classifier_and_data(clf, my_dataset, features_list)


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Data Exploration Findings

145 people
20 features
18 persons of interest
1352 NaN entries
director_fees, loan_advances, and restricted_stock_deferred are the top 3 columns with the most NaN entries

Since there were so many 'NaN' entries, I went back to the pdf that the data was derived from and noticed that entries with '-' were being interpreted as NaN. To fix this I will be replacing the 'NaN's with the value zero.
Can you write Python code for it?


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Do POI's have a bigger bonus to salary ratio?
Can you write Python code for it?

enron_df['bonus_ratio'] = enron_df['bonus'] / enron_df['salary']

enron_df[['poi','bonus_ratio']]


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Do POI's write more emails to other POI's compared to non POI's?
Can you write Python code for it?

enron_df['to_poi_ratio'] = enron_df['from_this_person_to_poi'] / enron_df['to_messages']
enron_df.fillna(0, inplace=True)


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Email Features
Can you write Python code for it?

enron_df.loc[:, ['to_messages',
                 'email_address',
                 'from_poi_to_this_person',
                 'from_messages',
                 'from_this_person_to_poi',
                 'shared_receipt_with_poi']].describe()


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Feature Engineering Conclusion:

Three features were created:
The ratio of (emails from poi's sent to the person) to (from received)
The ratio of (emails from this person sent to poi's) to (emails sent)
The ratio of bonus to salary
In the next step feature selection will be done with selectKBest, PCA, and inherently with the feature importance in various tree classifier algorithms.

Classifier Pipelines
Can you write Python code for it?

# Separate labels and features
enron_df_labels = enron_df['poi']
enron_df_features = enron_df[enron_df.columns.difference(['poi'])]


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Features in the dataset:
In the dataset there are 146 employees with 20 features and 'poi' label for each employee. To further summarize the features in the dataset, we will convert the dictionary to a pandas DataFrame.
Can you write Python code for it?

# Replace "Nan" with NaN
for columns in enron_data.itervalues():
    for k,v in columns.iteritems():
        if type(v) is str and v.lower() == "nan":
            columns[k] = np.nan

enron_df = pd.DataFrame.from_dict(enron_data, orient="index")
enron_df


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Financial Features
Can you write Python code for it?

enron_df.loc[:, ['salary',
                 'deferral_payments',
                 'total_payments',
                 'loan_advances',
                 'bonus',
                 'restricted_stock_deferred',
                 'deferred_income',]].describe()

enron_df.loc[:, ['total_stock_value',
                 'expenses',
                 'exercised_stock_options',
                 'other',
                 'long_term_incentive',
                 'restricted_stock',
                 'director_fees']].describe()


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Indexes Removed
* TOTAL
* THE TRAVEL AGENCY IN THE PARK
* LOCKHART EUGENE E
The 'email_address' column was also removed since it is irrelevant data.
Optimize Feature Selection/Engineering
Create new features
Do POI's receive more emails from other POI's compared to non POI's?
Can you write Python code for it?

enron_df['from_poi_ratio'] = enron_df['from_poi_to_this_person'] / enron_df['from_messages']
enron_df.fillna(0, inplace=True)


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Number of NaN's:
Can you write Python code for it?


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
Outlier Investigation
One outlier that was removed earlier was the "Total" index, which was represented the total sums of each columns. There were two entries that were invalid data points. One was "THE TRAVEL AGENCY IN THE PARK", which can not be a person and definitely not a person of interest. The other invalid entry was "LOCKHART EUGENE E", which had NaN values for all the features.
Can you write Python code for it?

# Drop email_address column
enron_df.drop('email_address', axis=1, inplace=True)

enron_df.drop("THE TRAVEL AGENCY IN THE PARK", inplace=True)
enron_df.drop("LOCKHART EUGENE E", inplace=True)


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
This file contains a list of 35 people who were a person of interest in the Enron scandal. A POI is defined as someone who was:
* indicted
* settled without admitting guilt
* testified in exchange for immunity
Summary of Dataset
Can you write Python code for it?

enron_data = pickle.load(open("./final_project_dataset.pkl"))

enron_data.iteritems().next()


I am working on the file "P5 sklearn ML/analysis.ipynb".
The first task is:
With PCA
Can you write Python code for it?

pipeline = Pipeline([
        ("scale", preprocessing.StandardScaler()),
        ('pca', PCA()),
        ('dt', DecisionTreeClassifier())])
folds = 100
cv = StratifiedShuffleSplit(enron_df_labels, n_iter= folds, random_state = 42, test_size=0.20)
parameters = {"pca__n_components": [2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19],
              'dt__max_features': [None, 'auto', 'log2'],
              'dt__criterion': ['gini', 'entropy']}
clf = GridSearchCV(pipeline, param_grid=parameters, cv=cv, scoring='f1')
clf.fit(enron_df_features, enron_df_labels)

pca = clf.best_estimator_.steps[1][1]
pca.n_components

pca_dt = Pipeline([
        ("scale", preprocessing.StandardScaler()),
        ('pca', PCA(n_components=pca.n_components)),
        ('dt', clf.best_estimator_.steps[2][1])])

dump_classifier_and_data(pca_dt, enron_df.transpose().to_dict(), features_list)
tester.main()


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Basic Package
Create a flopy basic package object: flopy.modflow.ModflowBas.
Can you write Python code for it?

# Variables for the BAS package
IBOUND = np.ones((NLAY, NROW, NCOL), dtype=np.int32)  # all nodes are active (IBOUND = 1)

# make the top of the profile specified head by setting the IBOUND = -1
IBOUND[:, 0, :] = -1  #don't forget arrays are zero-based! Sets first row
IBOUND[:, :, 0] = -1  # Sets first column
print IBOUND

STRT = 1 * np.ones((NLAY, NROW, NCOL), dtype=np.float32)  # set starting head to 1` through out model domain
STRT[:, 0, :] = 0.       # top row ocean elevation for setting constant head
STRT[:, :, 0] = 0.     # first column ocean elevation for setting constant head
print STRT

BAS_PACKAGE = flopy.modflow.ModflowBas(MF, ibound=IBOUND, strt=STRT)
# print BAS_PACKAGE  # uncomment this at far left to see the information about the flopy BAS object


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Create the MODFLOW Model Object
Create a flopy MODFLOW object: flopy.modflow.Modflow.
Can you write Python code for it?

# Assign name and create modflow model object
modelname = 'P5-1'
#exe_name = os.path.join(datapath, 'mf2005.exe') # for Windows OS
exe_name = os.path.join(datapath, 'mf2005')     # for Mac OS
print 'Model executable: ', exe_name
MF = flopy.modflow.Modflow(modelname, exe_name=exe_name, model_ws=modelpath)


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Define the Model Extent, Grid Resolution, and Characteristics
It is normally good practice to group things that you might want to change into a single code block.  This makes it easier to make changes and rerun the code.
Can you write Python code for it?

# model domain and grid definition
# for clarity, user entered variables are all caps; python syntax are lower case or mixed case
# This is an areal 2D model that uses island symmetry to reduce the grid size.
LX = 16000.   # half width of island + one node for constant head boundary condition
LY = 28000.   # half height of island + one node for constant head boundary condition
ZTOP = 0.  # the system is confined
ZBOT = -50.
NLAY = 1
NROW = 7
NCOL = 4
DELR = LX / NCOL  # recall that MODFLOW convention is DELR is along a row, thus has items = NCOL; see page XXX in AW&H (2015)
DELC = LY / NROW  # recall that MODFLOW convention is DELC is along a column, thus has items = NROW; see page XXX in AW&H (2015)
DELV = (ZTOP - ZBOT) / NLAY
BOTM = np.linspace(ZTOP, ZBOT, NLAY + 1)
HK = 200.
VKA = 1.
RCH = 0.00305
# WELLQ = 0.   #not needed for Problem 5.1
print "DELR =", DELR, "  DELC =", DELC, '  DELV =', DELV
print "BOTM =", BOTM
print "Recharge =", RCH
#print "Pumping well rate =", WELLQ


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Discretization Package
Create a flopy discretization package object: flopy.modflow.ModflowDis.
Can you write Python code for it?

# Create the discretization object
TOP = ZTOP * np.ones((NROW, NCOL),dtype=np.float)

DIS_PACKAGE = flopy.modflow.ModflowDis(MF, NLAY, NROW, NCOL, delr=DELR, delc=DELC,
                               top=TOP, botm=BOTM[1:], laycbd=0)
# print DIS_PACKAGE  #uncomment this on far left to see information about the flopy object


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Look at the bottom of the MODFLOW output file (ending with a *.list) and write down the water balance reported to compare to the less coarse grid size done in the next part of the problem.
Changing the grid size and rerunning/plotting
Recall that the second part of Part b is to redo the problem with a finer grid spacing:  a 13 x 25 array of nodes (delta x = delta y= 1000 ft).
Can you write Python code for it?

LX = 13000.   # same as before - half width of island + one node for constant head boundary condition
LY = 25000.   # same as before - half height of island + one node for constant head boundary condition
NLAY = 1
NROW = 25
NCOL = 13
DELR = LX / NCOL  # recall that MODFLOW convention is DELR is along a row, thus has items = NCOL; see page XXX in AW&H (2015)
DELC = LY / NROW  # recall that MODFLOW convention is DELC is along a column, thus has items = NROW; see page XXX in AW&H (2015)
DELV = (ZTOP - ZBOT) / NLAY
BOTM = np.linspace(ZTOP, ZBOT, NLAY + 1)
HK = 200.
VKA = 1.
RCH = 0.00305
# WELLQ = 0.   #not needed for Problem 5.1
print "DELR =", DELR, "  DELC =", DELC, '  DELV =', DELV
print "BOTM =", BOTM
print "Recharge =", RCH

# Create the discretization object
TOP = ZTOP * np.ones((NROW, NCOL),dtype=np.float)
DIS_PACKAGE = flopy.modflow.ModflowDis(MF, NLAY, NROW, NCOL, delr=DELR, delc=DELC,
                               top=TOP, botm=BOTM[1:], laycbd=0)

# Variables for the BAS package
IBOUND = np.ones((NLAY, NROW, NCOL), dtype=np.int32)  # all nodes are active (IBOUND = 1)

# make the top of the profile specified head by setting the IBOUND = -1
IBOUND[:, 0, :] = -1  #don't forget arrays are zero-based! Sets first row
IBOUND[:, :, 0] = -1  # Sets first column
print IBOUND

STRT = 1 * np.ones((NLAY, NROW, NCOL), dtype=np.float32)  # set starting head to 1` through out model domain
STRT[:, 0, :] = 0.       # top row ocean elevation for setting constant head
STRT[:, :, 0] = 0.     # first column ocean elevation for setting constant head
print STRT

BAS_PACKAGE = flopy.modflow.ModflowBas(MF, ibound=IBOUND, strt=STRT)
# print BAS_PACKAGE  # uncomment this at far left to see the information about the flopy BAS object

#delete earlier files to prevent us from reading old results
modelfiles = os.listdir(modelpath)
for filename in modelfiles:
    f = os.path.join(modelpath, filename)
    if modelname in f:
        try:
            os.remove(f)
            print 'Deleted: ', filename
        except:
           ...


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Note that the max head is again getting closer to the analytical solution of 20 ft.
Can you write Python code for it?

#Create a contour plot of heads
FIG = plt.figure(figsize=(12,10))

#setup contour levels and plot extent
LEVELS = np.arange(0., 26., 5.)
EXTENT = (DELR/2., LX - DELR/2., DELC/2., LY - DELC/2.)
print 'Contour Levels: ', LEVELS
print 'Extent of domain: ', EXTENT

#Make a contour plot on the first axis
AX1 = FIG.add_subplot(1, 2, 1, aspect='equal')
AX1.set_xlabel("x")
AX1.set_ylabel("y")
YTICKS = np.arange(0, 28000, 4000)
AX1.set_yticks(YTICKS)
AX1.set_title("P5.1 Island Recharge Problem")
AX1.text(220, 10000, r"side ocean boundary condition", fontsize=10, color="blue", rotation='vertical')
AX1.text(4000, 23800, r"top ocean boundary condition", fontsize=10, color="blue")
AX1.contour(np.flipud(HEAD[0, :, :]), levels=LEVELS, extent=EXTENT)

#Make a color flood on the second axis
AX2 = FIG.add_subplot(1, 2, 2, aspect='equal')
AX2.set_xlabel("x")
AX2.set_ylabel("y")
AX2.set_yticks(YTICKS)
AX2.set_title("P5.1 color flood")
AX2.text(4000, 23800, r"top ocean boundary condition", fontsize=10, color="white")
AX2.text(200, 10000, r"side ocean boundary condition", fontsize=10, color="white",rotation='vertical')
cax = AX2.imshow(HEAD[0, :, :], extent=EXTENT, interpolation='nearest')
cbar = FIG.colorbar(cax, orientation='vertical', shrink=0.45)


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Output Control
Create a flopy output control object: flopy.modflow.ModflowOc.
Can you write Python code for it?

OC_PACKAGE = flopy.modflow.ModflowOc(MF)   # we'll use the defaults for the model output
# print OC_PACKAGE  # uncomment this at far left to see the information about the flopy OC object


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Post Processing the Results
To read heads from the MODFLOW binary output file, we can use the flopy.utils.binaryfile module. Specifically, we can use the HeadFile object from that module to extract head data arrays.
Can you write Python code for it?

#imports for plotting and reading the MODFLOW binary output file
import matplotlib.pyplot as plt
import flopy.utils.binaryfile as bf

#Create the headfile object and grab the results for last time.
headfile = os.path.join(modelpath, modelname + '.hds')
headfileobj = bf.HeadFile(headfile)

#Get a list of times that are contained in the model
times = headfileobj.get_times()
print 'Headfile (' + modelname + '.hds' + ') contains the following list of times: ', times

#Get a numpy array of heads for totim = 1.0
#The get_data method will extract head data from the binary file.
HEAD = headfileobj.get_data(totim=1.0)

#Print statistics on the head
print 'Head statistics'
print '  min: ', HEAD.min()
print '  max: ', HEAD.max()
print '  std: ', HEAD.std()

#Create a contour plot of heads
FIG = plt.figure(figsize=(12,10))

#setup contour levels and plot extent
LEVELS = np.arange(0., 26., 5.)
EXTENT = (DELR/2., LX - DELR/2., DELC/2., LY - DELC/2.)
print 'Contour Levels: ', LEVELS
print 'Extent of domain: ', EXTENT

#Make a contour plot on the first axis
AX1 = FIG.add_subplot(1, 2, 1, aspect='equal')
AX1.set_xlabel("x")
AX1.set_ylabel("y")
YTICKS = np.arange(0, 28000, 4000)
AX1.set_yticks(YTICKS)
AX1.set_title("P5.1 Island Recharge Problem")
AX1.text(2250, 10000, r"side ocean boundary condition", fontsize=10, color="blue", rotation='vertical')
AX1.text(5500, 25000, r"top ocean boundary condition", fontsize=10, color="blue")
AX1.contour(np.flipud(HEAD[0, :, :]), levels=LEVELS, extent=EXTENT)

#Make a color flood on the second axis
AX2 = FIG.add_subplot(1, 2, 2, aspect='equal')
AX2.set_xlabel("x")
AX2.set_ylabel("y")
AX2.set_yticks(YTICKS)
AX2.set_title("P5.1 color flood")
AX2.text(5500, 25000, r"top ocean boundary condition", fontsize=10, color="white")
AX2.text(2500, 10500, r"side ocean boundary condition", fontsize=10, color="white", rotation='vertical')
cax = AX2.imshow(HEAD[0, :, :], extent=EXTENT, interpolation='nearest')
cbar = FIG.colorbar(cax, orientation='vertical', shrink=0.45)

print HEAD


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Preconditioned Conjugate Gradient Solver
Create a flopy pcg package object: flopy.modflow.ModflowPcg.
Can you write Python code for it?

PCG_PACKAGE = flopy.modflow.ModflowPcg(MF, mxiter=500, iter1=100, hclose=1e-04, rclose=1e-03, relax=0.98, damp=0.5)
# print PCG_PACKAGE  # uncomment this at far left to see the information about the flopy PCG object


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Recharge Package
Create a flopy pcg package object: flopy.modflow.ModflowRch.
Can you write Python code for it?

RCH_PACKAGE = flopy.modflow.ModflowRch(MF, rech=RCH)
# print RCH_PACKAGE  # uncomment this at far left to see the information about the flopy RCH object


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Setup a New Directory and Change Paths
For this tutorial, we will work in a new subdirectory underneath the directory where the notebook is located. We can use some fancy Python tools to help us manage the directory creation. Note that if you encounter path problems with this workbook, you can stop and then restart the kernel and the paths will be reset.
Can you write Python code for it?

# Set the name of the path to the model working directory
dirname = "P5-1_Island_recharge"
datapath = os.getcwd()
modelpath = os.path.join(datapath, dirname)
print 'Name of model path: ', modelpath

# Now let's check if this directory exists.  If not, then we will create it.
if os.path.exists(modelpath):
    print 'Model working directory already exists.'
else:
    print 'Creating model working directory.'
    os.mkdir(modelpath)


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
The model datasets are written using a single command (mf.write_input).
Check in the model working directory and verify that the input files have been created.  Or if you might just add another cell, right after this one, that prints a list of all the files in our model directory.  The path we are working in is returned from this next block.
Can you write Python code for it?

# return current working directory
print "You can check the newly created files in", modelpath


I am working on the file "P5.1_Flopy_Island_Recharge_grid_sensitivity.ipynb".
The first task is:
Write down the new mass balance information for this 1000 ft grid size run (or rename the *.list file).  Compare your previous result obtained with the 4000 foot grid size.
P5.1 Part e.
Run your model with a nodal spacing of 500 ft and again with a nodal spacing of 250 ft. Compare head values and the volumetric discharge rate at the shoreline for all four nodal networks. Do you think a nodal spacing of 1000 ft is adequate for this problem? Justify your answer.
Can you write Python code for it?

# Same as above but now 500 foot nodal spacing
LX = 12500.   # same as before - half width of island + one node for constant head boundary condition
LY = 24500.   # same as before - half height of island + one node for constant head boundary condition
NLAY = 1
NROW = 49
NCOL =25
DELR = LX / NCOL  # recall that MODFLOW convention is DELR is along a row, thus has items = NCOL; see page XXX in AW&H (2015)
DELC = LY / NROW  # recall that MODFLOW convention is DELC is along a column, thus has items = NROW; see page XXX in AW&H (2015)
DELV = (ZTOP - ZBOT) / NLAY
BOTM = np.linspace(ZTOP, ZBOT, NLAY + 1)
HK = 200.
VKA = 1.
RCH = 0.00305
# WELLQ = 0.   #not needed for Problem 5.1
print "DELR =", DELR, "  DELC =", DELC, '  DELV =', DELV
print "BOTM =", BOTM
print "Recharge =", RCH

# Create the discretization object
TOP = ZTOP * np.ones((NROW, NCOL),dtype=np.float)
DIS_PACKAGE = flopy.modflow.ModflowDis(MF, NLAY, NROW, NCOL, delr=DELR, delc=DELC,
                               top=TOP, botm=BOTM[1:], laycbd=0)
# Variables for the BAS package
IBOUND = np.ones((NLAY, NROW, NCOL), dtype=np.int32)  # all nodes are active (IBOUND = 1)

# make the top of the profile specified head by setting the IBOUND = -1
IBOUND[:, 0, :] = -1  #don't forget arrays are zero-based! Sets first row
IBOUND[:, :, 0] = -1  # Sets first column
print IBOUND

STRT = 1 * np.ones((NLAY, NROW, NCOL), dtype=np.float32)  # set starting head to 1` through out model domain
STRT[:, 0, :] = 0.       # top row ocean elevation for setting constant head
STRT[:, :, 0] = 0.     # first column ocean elevation for setting constant head
print STRT

BAS_PACKAGE = flopy.modflow.ModflowBas(MF, ibound=IBOUND, strt=STRT)
# print BAS_PACKAGE  # uncomment this at far left to see the information about the flopy BAS object
#delete earlier files to prevent us from reading old results
modelfiles = os.listdir(modelpath)
for filename in modelfiles:
    f = os.path.join(modelpath, filename)
    if modelname in f:
        try:
            os.remove(f)
            print 'Deleted:...


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Discretization Package
Create a flopy discretization package object: flopy.modflow.ModflowDis.
Can you write Python code for it?


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Examine your results and answer the following:
i. Explain why the results of the 2D model are different from the 3D model. What are the main factors that control the height of the water table mound under the pond? Discuss whether the 2D model is appropriate for this problem.
ii. Is it likely that the water table intersects the land surface away from the pond? If so, use shading on a map of the land surface in the vicinity of the pond to show the area affected by leakage.
P5.3c
When the modeling report is sent out for review, reviewers question whether
the large nodal spacing of 900 m sufficiently captures the head gradient that
defines the mound. They say that the surface area affected by the mound is
underestimated. Use the three-layer model developed in (b) to assess the effect
of nodal spacing on the solution. Reduce the nodal spacing uniformly over
the grid/mesh to 300 m, or construct an irregular FD grid, unstructured FD
grid, or FE mesh with fine nodal spacing in the vicinity of the pond. Run the
model and generate equipotential maps for each layer (use a 1-m contour interval)
and a cross section that passes through the pond and constant head boundaries.
If the mound intersects the land surface, show the area impacted by shading
on a map of the land surface. Compare and contrast results with those of parts
(a) and (b).
Can you write Python code for it?

# let's redefine the grid
LX = 12300.  # aquifer width of 11700 m + 2 300m nodes for constant head boundary condition on each end
LY = 9000.   # height of aquifer of 9000 m, no flow boundaries do not require explicit cells in MODFLOW
ZTOP = 130.  # the system is unconfined
NROW = 30  #these two lines are the only things we need to change to refine
NCOL = 41  # the grid
DELR = LX / NCOL  # recall that MODFLOW convention is DELR is along a row, thus has items = NCOL; see page XXX in AW&H (2015)
DELC = LY / NROW  # recall that MODFLOW convention is DELC is along a column, thus has items = NROW; see page XXX in AW&H (2015)
POND_SEEP=0.2
print "DELR =", DELR, "  DELC =", DELC
print "Pond Seepage =", POND_SEEP, "m/d"

#repeat the modeling building steps from P5.3b to make sure all model definition is current
NLAY = 3
ZBOT = 0.
BOTM = np.zeros((NLAY, NROW, NCOL), dtype=np.float)
BOTM[0,:,:] = 80.
BOTM[1,:,:] = 40.
BOTM[2,:,:] = 0.
#print BOTM
# Create the discretization object
TOP = ZTOP * np.ones((NROW, NCOL),dtype=np.float)
DIS_PACKAGE = flopy.modflow.ModflowDis(MF, NLAY, NROW, NCOL, delr=DELR, delc=DELC,
                               top=TOP, botm=BOTM, laycbd=0)
IBOUND = np.ones((NLAY, NROW, NCOL), dtype=np.int)
IBOUND[:,:,0] = -1
IBOUND[:,:,-1] = -1
#print IBOUND
STRT = 130. * np.ones((NLAY, NROW, NCOL), dtype=np.float)
STRT[:,:,0] = 90.
STRT[:,:,-1] = 120.
#print STRT
BAS_PACKAGE = flopy.modflow.ModflowBas(MF, ibound=IBOUND, strt=STRT)
# print BAS_PACKAGE  # uncomment this at far left to see the information about the flopy BAS object
#Now we can assign the Kh for each layer from Figure P5.2
LAY1Kh=44.
LAY2Kh=0.13
LAY3Kh=113.
KH_ARRAY = np.zeros((NLAY, NROW, NCOL), dtype=np.float)
KH_ARRAY[0,:,:] = LAY1Kh
KH_ARRAY[1,:,:] = LAY2Kh
KH_ARRAY[2,:,:] = LAY3Kh
#print KH_ARRAY
#Now we can assign the Kv for each layer from Figure P5.2
LAY1Kv=4.4
LAY2Kv=0.013
LAY3Kv=11.3
KV_ARRAY = np.zeros((NLAY, NROW, NCOL), dtype=np.float)
KV_ARRAY[0,:,:] = LAY1Kv
KV_ARRAY[1,:,:] = LAY2Kv
KV_ARRAY[2,:,:] = LAY3Kv
#print...


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Land surface elevation = 130 m, which is significantly higher than the maximum head in the model.
Can you write Python code for it?

#Create a contour plot of heads
FIG = plt.figure(figsize=(12,10))

#setup contour levels and plot extent
LEVELS = np.arange(90., 126., 1.)
EXTENT = (DELR/2., LX - DELR/2., DELC/2., LY - DELC/2.)
print 'Contour Levels: ', LEVELS
print 'Extent of domain: ', EXTENT

#Make a contour plot on the first axis
AX1 = FIG.add_subplot(1, 2, 1, aspect='equal')
AX1.set_xlabel("x")
AX1.set_ylabel("y")
YTICKS = np.arange(0, 9000, 1000)
AX1.set_yticks(YTICKS)
AX1.set_title("P5.3 1-layer Industrial Pond Problem")
AX1.text(6000, 5500, r"pond", fontsize=10, color="blue")
AX1.contour(np.flipud(HEAD[0, :, :]), levels=LEVELS, extent=EXTENT)

#Make a color flood on the second axis
AX2 = FIG.add_subplot(1, 2, 2, aspect='equal')
AX2.set_xlabel("x")
AX2.set_ylabel("y")
AX2.set_yticks(YTICKS)
AX2.set_title("P5.3 color flood")
AX2.text(6000, 5500, r"pond", fontsize=10, color="white")
cax = AX2.imshow(HEAD[0, :, :], extent=EXTENT, interpolation='nearest')
cbar = FIG.colorbar(cax, orientation='vertical', shrink=0.45)

#Using code from P4.4, let's plot a cross section of head in row = 4 from headobj, recall that Python is zero based
#so that MODFLOW row 5 with the pond is equal to Python row 4
#define Y as HEAD along the row and then print; ROW is a variable that allows us to change the row plotted easily
ROW = 4
Y = HEAD[0,ROW,:]
print Y

#in order to plot the cross section we'll need to create X-coordinates to match with heads at the node centers
XCOORD = np.arange(0, 13500, 900) + 450
print XCOORD

fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(1, 1, 1)
TITLE =  '1-layer model 900m x 900m grid: cross section of head along Row = ({0})'.format(ROW) #this allows the title to be updated as ROW changes
ax.set_title(TITLE)
ax.set_xlabel('X')
ax.set_ylabel('head')
ax.set_xlim(0, 13500.)
ax.set_ylim(0.,150.)
ax.text(6400,125, r"pond", fontsize=12, color="black")
ax.text(2030,132, r"land surface", fontsize=15, color="green")
ax.text(13200, 80, r"constant head = 120m", fontsize=10, color="blue",rotation='vertical')
ax.text(150, 80,...


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Note the new maximum head value (recall land surface elevation = 130).
Can you write Python code for it?

#Create a contour plot of heads
FIG = plt.figure(figsize=(12,10))

#setup contour levels and plot extent
LEVELS = np.arange(90., 146., 1.)
EXTENT = (DELR/2., LX - DELR/2., DELC/2., LY - DELC/2.)
print 'Contour Levels: ', LEVELS
print 'Extent of domain: ', EXTENT

#Make a contour plot on the first axis
AX1 = FIG.add_subplot(1, 2, 1, aspect='equal')
AX1.set_xlabel("x")
AX1.set_ylabel("y")
YTICKS = np.arange(0, 9000, 1000)
AX1.set_yticks(YTICKS)
AX1.set_title("3-layer P5.3 Industrial Pond Problem 900x900m grid")
AX1.text(6000, 5500, r"pond", fontsize=10, color="blue")
AX1.contour(np.flipud(HEAD[0, :, :]), levels=LEVELS, extent=EXTENT)

#Make a color flood on the second axis
AX2 = FIG.add_subplot(1, 2, 2, aspect='equal')
AX2.set_xlabel("x")
AX2.set_ylabel("y")
AX2.set_yticks(YTICKS)
AX2.set_title("P5.3 color flood")
AX2.text(6000, 5500, r"pond", fontsize=10, color="white")
cax = AX2.imshow(HEAD[0, :, :], extent=EXTENT, interpolation='nearest')
cbar = FIG.colorbar(cax, orientation='vertical', shrink=0.45)

#Using code from P4.4, let's again plot a cross section of head in row = 4 from headobj, recall that Python is zero based
#so that MODFLOW row 5 with the pond is equal to Python row 4
#define Y as HEAD along the row and then print; ROW is a variable that allows us to change the row plotted easily
ROW = 4
Y = HEAD[0,ROW,:]
print Y

#in order to plot the cross section we'll need to create X-coordinates to match with heads at the node centers
#(we could have just used the XCOORD calculated above as the X-Y spacing did not change, but we'll do it again
#to make sure we have the correct X coordinates)
XCOORD = np.arange(0, 13500, 900) + 450
print XCOORD

fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(1, 1, 1)
TITLE =  '3-layer model 900m x 900m grid: cross section of head along Row = ({0})'.format(ROW) #this allows the title to be updated as ROW changes
ax.set_title(TITLE)
ax.set_xlabel('X')
ax.set_ylabel('head')
ax.set_xlim(0, 13500.)
ax.set_ylim(0.,150.)
ax.text(6150,125, r"pond", fontsize=12,...


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Now we must do the same for MODFLOW's Basic Package
Can you write Python code for it?

IBOUND = np.ones((NLAY, NROW, NCOL), dtype=np.int)
IBOUND[:,:,0] = -1
IBOUND[:,:,-1] = -1
print IBOUND

STRT = 130. * np.ones((NLAY, NROW, NCOL), dtype=np.float)
STRT[:,:,0] = 90.
STRT[:,:,-1] = 120.
print STRT

BAS_PACKAGE = flopy.modflow.ModflowBas(MF, ibound=IBOUND, strt=STRT)
# print BAS_PACKAGE  # uncomment this at far left to see the information about the flopy BAS object


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Now we must do the same for hydraulic conductivity.
Can you write Python code for it?

#Now we can assign the Kh for each layer from Figure P5.2
LAY1Kh=44.
LAY2Kh=0.13
LAY3Kh=113.

KH_ARRAY = np.zeros((NLAY, NROW, NCOL), dtype=np.float)
KH_ARRAY[0,:,:] = LAY1Kh
KH_ARRAY[1,:,:] = LAY2Kh
KH_ARRAY[2,:,:] = LAY3Kh
print KH_ARRAY

#Now we can assign the Kv for each layer from Figure P5.2
LAY1Kv=4.4
LAY2Kv=0.013
LAY3Kv=11.3

KV_ARRAY = np.zeros((NLAY, NROW, NCOL), dtype=np.float)
KV_ARRAY[0,:,:] = LAY1Kv
KV_ARRAY[1,:,:] = LAY2Kv
KV_ARRAY[2,:,:] = LAY3Kv
print KV_ARRAY

LPF_PACKAGE = flopy.modflow.ModflowLpf(MF, laytyp=1, hk=KH_ARRAY, vka=KV_ARRAY)  # we defined the K and anisotropy at top of file
# print LPF_PACKAGE  # uncomment this at far left to see the information about the flopy LPF object

#Before writing input, destroy all files in folder to prevent reusing old files
#Here's the working directory
print modelpath
#Here's what's currently in the working directory
modelfiles = os.listdir(modelpath)
print modelfiles
#delete these files to prevent us from reading old results
modelfiles = os.listdir(modelpath)
for filename in modelfiles:
    f = os.path.join(modelpath, filename)
    if modelname in f:
        try:
            os.remove(f)
            print 'Deleted: ', filename
        except:
            print 'Unable to delete: ', filename

#Now write the model input files and run MODFLOW
MF.write_input()
silent = False  #Print model output to screen?
pause = False   #Require user to hit enter? Doesn't mean much in Ipython notebook
report = True   #Store the output from the model in buff
success, buff = MF.run_model(silent=silent, pause=pause, report=report)

#imports for plotting and reading the MODFLOW binary output file
import matplotlib.pyplot as plt
import flopy.utils.binaryfile as bf

#Create the headfile object and grab the results for last time.
headfile = os.path.join(modelpath, modelname + '.hds')
headfileobj = bf.HeadFile(headfile)

#Get a list of times that are contained in the model
times = headfileobj.get_times()
print 'Headfile (' + modelname + '.hds' + ') contains the following...


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Output Control
Create a flopy output control object: flopy.modflow.ModflowOc.
Can you write Python code for it?


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
P5.3b
The state regulatory agency insists that a 3D model be developed to examine how vertical flow and anisotropy influence the height of the groundwater mound. They point out that the low hydraulic conductivity of layer 2 andthe vertical anisotropy present in the layered sequence of units might cause the mound to rise to the surface. Construct a three- layer steady-state model based on the information in Fig. P5.2. Specified head and no flow boundaries
extend to all layers. Generate an equipotential surface (using 1-m contour intervals) for each layer. Also show the head distribution in a cross section that passes through the specified head boundaries and the pond.
Can you write Python code for it?

#we have to redefine the layering
ZTOP = 130.  # the system is unconfined so set the top above land surface so that the water table never > layer top
ZBOT = 0.
NLAY = 3
BOTM = np.zeros((NLAY, NROW, NCOL), dtype=np.float)
BOTM[0,:,:] = 80.
BOTM[1,:,:] = 40.
BOTM[2,:,:] = 0.
print BOTM

# Create the discretization object
TOP = ZTOP * np.ones((NROW, NCOL),dtype=np.float)
DIS_PACKAGE = flopy.modflow.ModflowDis(MF, NLAY, NROW, NCOL, delr=DELR, delc=DELC,
                               top=TOP, botm=BOTM, laycbd=0)


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Post Processing the Results
To read heads from the MODFLOW binary output file, we can use the flopy.utils.binaryfile module. Specifically, we can use the HeadFile object from that module to extract head data arrays.
Can you write Python code for it?

#imports for plotting and reading the MODFLOW binary output file
import matplotlib.pyplot as plt
import flopy.utils.binaryfile as bf

#Create the headfile object and grab the results for last time.
headfile = os.path.join(modelpath, modelname + '.hds')
headfileobj = bf.HeadFile(headfile)

#Get a list of times that are contained in the model
times = headfileobj.get_times()
print 'Headfile (' + modelname + '.hds' + ') contains the following list of times: ', times

#Get a numpy array of heads for totim = 1.0
#The get_data method will extract head data from the binary file.
HEAD = headfileobj.get_data(totim=1.0)

#Print statistics on the head
print 'Head statistics'
print '  min: ', HEAD.min()
print '  max: ', HEAD.max()
print '  std: ', HEAD.std()


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Preconditioned Conjugate Gradient Solver
Create a flopy pcg package object: flopy.modflow.ModflowPcg.
Can you write Python code for it?


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Recharge Package
Because the pond seepage is given as a flux over the cell (L/T) rather than a volumetric flow rate (L3/T), the Recharge Package is the easier to use than MODFLOW's Well Package.  However, we only want to add pond seepage to the cells that represent the pond.  Therefore, we'll need to make an array like we do for starting heads.
Can you write Python code for it?

SEEP_ARRAY = 0 * np.ones((NROW, NCOL), dtype=np.float32)  # set seepage to 0 over model grid
SEEP_ARRAY[4, 7] = POND_SEEP  # add Pond seepage at the pond location
print SEEP_ARRAY


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Setup a New Directory and Change Paths
For this tutorial, we will work in a new subdirectory underneath the directory where the notebook is located. We can use some fancy Python tools to help us manage the directory creation. Note that if you encounter path problems with this workbook, you can stop and then restart the kernel and the paths will be reset.
Can you write Python code for it?

# Set the name of the path to the model working directory
dirname = "P5-3_Industrial_pond"
datapath = os.getcwd()
modelpath = os.path.join(datapath, dirname)
print 'Name of model path: ', modelpath

# Now let's check if this directory exists.  If not, then we will create it.
if os.path.exists(modelpath):
    print 'Model working directory already exists.'
else:
    print 'Creating model working directory.'
    os.mkdir(modelpath)


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
This agrees with the value given in the caption in figure P5.2
Can you write Python code for it?

LAY2b = 80.-40
LAY3b = 40.-0
print "Layer 2 thickness =", LAY2b, "m"
print "Layer 3 thickness =", LAY3b, "m"

#In equation B5.3.2 we also need the total thickess of the aquifer B
TOT_THICK = LAY1b+LAY2b+LAY3b
print "The total aquifer thickness B =", TOT_THICK, "m"

#Now we can assign the Kh for each layer from Figure P5.2
LAY1Kh=44.
LAY2Kh=0.13
LAY3Kh=113.
#now we can calculate the equivalent Kh from Box 5.3's equation B5.3.2
EQUIV_Kh=(LAY1Kh*LAY1b/TOT_THICK)+(LAY2Kh*LAY2b/TOT_THICK)+(LAY3Kh*LAY3b/TOT_THICK)
print "The 1-layer equivlant Kh =", EQUIV_Kh, "m/d"


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Well Package
This is not needed for Problem P5.3
Can you write Python code for it?

#WEL_PACKAGE = flopy.modflow.ModflowWel(MF, stress_period_data=[0,0,0,WELLQ])  # remember python 0 index, layer 0 = layer 1 in MF
#print WEL_PACKAGE  # uncomment this at far left to see the information about the flopy WEL object


I am working on the file "P5.3_Flopy_Industrial_pond.ipynb".
The first task is:
Writing the MODFLOW Input Files
Before we create the model input datasets, we can do some directory cleanup to make sure that we don't accidently use old files.
Can you write Python code for it?

#Before writing input, destroy all files in folder to prevent reusing old files
#Here's the working directory
print modelpath
#Here's what's currently in the working directory
modelfiles = os.listdir(modelpath)
print modelfiles

#delete these files to prevent us from reading old results
modelfiles = os.listdir(modelpath)
for filename in modelfiles:
    f = os.path.join(modelpath, filename)
    if modelname in f:
        try:
            os.remove(f)
            print 'Deleted: ', filename
        except:
            print 'Unable to delete: ', filename

#Now write the model input files
MF.write_input()