{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. The Aral Sea in 1973\n", "- The Aral Sea was a freshwater lake in central Asia, located between Kazakhstan and Uzbekistan. It was once the 4th largest lake in the world, but due to increased usage of water upstream for agricultural use, it has dried up and split into separate bodies. Today we will look at Landsat imagery from the northern part of the Aral sea.\n", "\n", "- The file 'Datasets/Aral_Sea_1973' contains Landsat-1 imagery for an area in the northern part of the Aral Sea in 1973. The pixels in this image have 120m resolution. Read in all 4 bands as image files. \n", "\n", "- Plot the Landsat Data as a color image like we did in the lecture. Use band 4 for red, band 3 for green and band 2 for blue.\n", "\n", "- Now let's do the classification, we're going to find the area of the image that is land, and the area of the image that is water. Use the function **create_training_data** given below to create the training data and labels for classification. Use a list of the bands e.g. `[B1,B2,B3,B4]` for the 'input_features' argument, a list of your labels ('land' and 'water') for the 'input_labels' argument. The last 2 arguments want the y and x boundaries of your squares, they each take an array where the first axis determines which square we're in (i.e. the land square or the sea square) and the second axis are the start and end x or y coordinates. You can choose your own ranges if you want but a good set to start with would be `[[850,950],[415,515]]` for y and `[[900,1000],[900,1000]]` for x. (This is assuming that the first label is 'land')\n", "\n", "- Train the **sklearn.neighbors.KNeighborClassifier( )** algorithm on your training dataset. See the lecture for more information on how to do this.\n", "\n", "- Make the total dataset readable by scikit-learn using the **image_to_sklearn( )** function given below. It takes the same argument as 'input_features' above.\n", "\n", "- Predict labels for the whole image. What percentage of the pixels are water?\n", "\n", "## 2. The Aral Sea in 2009\n", "\n", "- Another landsat image showing roughly the same area, was made in may 2009. Go into the folder 'Datasets/Aral_Sea_2009' and import the 7 TIF files. \n", "\n", "- Plot the Landsat data as a color image using band 5 for red, band 4 for green and band 3 for blue like we did in the lecture.\n", "\n", "- Calculate the percentage of pixels that are water like we did in question 1. Good squares to use for the training data are `[[500,600],[150,250]]` for y and `[[250,350][700,800]]` for x.\n", "\n", "- If each pixel in these images is 120x120m, how much water has dried up from 1973-2009?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-05-27T00:41:08.961230Z", "start_time": "2020-05-27T00:41:08.950993Z" } }, "outputs": [], "source": [ "def create_training_data(input_features,input_labels,ranges_y,ranges_x):\n", " \"\"\"\n", " Creates a training dataset readable by scikit-learn from a set of arrays that have 'features'\n", " \"\"\"\n", " training_data=np.empty((0,len(input_features)))\n", " training_labels=np.array([])\n", " for i in range(len(input_labels)):\n", " data_list=[]\n", " #Add the pixels in the image/array to the training data list\n", " for j in range(len(input_features)):\n", " data_list.append(input_features[j][ranges_y[i][0]:ranges_y[i][1],ranges_x[i][0]:ranges_x[i][1]].flatten())\n", " \n", " data_list=np.array(data_list).T\n", " training_data=np.append(training_data,data_list,axis=0)\n", " training_labels=np.append(training_labels,np.full(len(data_list),input_labels[i]))\n", " return(training_data,training_labels)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-05-27T00:41:09.174433Z", "start_time": "2020-05-27T00:41:09.168761Z" } }, "outputs": [], "source": [ "def image_to_sklearn(input_features):\n", " \"\"\"\n", " Creates a scikitlearn dataset from a 2D image\n", " \"\"\"\n", " return(np.array([feature.flatten() for feature in input_features]).T)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }