{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1,2,3],[4,5,6]])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "b = np.array([[2,3,4],[5,6,9]])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 3, 4],\n", " [5, 6, 9]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1, -1, -1],\n", " [-1, -1, -3]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a - b" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.7320508075688772" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argmin([np.linalg.norm(a[i] - b[i]) for i in range(len(a))])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "np.min?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "\n", "\"\"\"\n", "This is a module for IDW Spatial Interpolation\n", "\"\"\"\n", "import numpy as np\n", "import pandas as pd\n", "from copy import deepcopy\n", "class idw():\n", " \"\"\" A class that is declared for performing IDW Interpolation.\n", " For more information on how this method works, kindly refer to\n", " https://en.wikipedia.org/wiki/Inverse_distance_weighting\n", "\n", " Parameters\n", " ----------\n", " exponent : positive float, optional\n", " The rate of fall of values from source data points.\n", " Higher the exponent, lower is the value when we move\n", " across space. Default value is 2.\n", " resolution: str, optional\n", " Decides the smoothness of the interpolation. Note that\n", " interpolation is done over a grid. Higher the resolution\n", " means more grid cells and more time for interpolation.\n", " Default value is 'standard'\n", " coordinate_type: str, optional\n", " Decides the distance metric to be used, while performing\n", " interpolation. Euclidean by default. \n", " \"\"\"\n", " def __init__(self, exponent = 2, resolution = 'standard', coordinate_type='Euclidean'):\n", " \n", " self.exponent = exponent\n", " self.resolution = resolution\n", " self.coordinate_type = coordinate_type\n", " self.interpolated_values = None\n", " self.x_grid = None\n", " self.y_grid = None\n", "\n", " def make_grid(self, x, y, res, offset=0.2):\n", "\n", " \"\"\" This function returns the grid to perform interpolation on.\n", " This function is used inside the fit() attribute of the idw class.\n", " \n", " Parameters\n", " ----------\n", " x: array-like, shape(n_samples,)\n", " The first coordinate values of all points where\n", " ground truth is available\n", " y: array-like, shape(n_samples,)\n", " The second coordinate values of all points where\n", " ground truth is available\n", " res: int\n", " The resolution value\n", " offset: float, optional\n", " A value between 0 and 0.5 that specifies the extra interpolation to be done\n", " Default is 0.2\n", " \n", " Returns\n", " -------\n", " xx : {array-like, 2D}, shape (n_samples, n_samples)\n", " yy : {array-like, 2D}, shape (n_samples, n_samples)\n", " \"\"\"\n", " y_min = y.min() - offset\n", " y_max = y.max()+ offset\n", " x_min = x.min()-offset\n", " x_max = x.max()+offset\n", " x_arr = np.linspace(x_min,x_max,res)\n", " y_arr = np.linspace(y_min,y_max,res)\n", " xx,yy = np.meshgrid(x_arr,y_arr) \n", " return xx,yy\n", "\n", " \n", " def fit(self, X, y):\n", " \"\"\" The function call to fit the model on the given data. \n", " Parameters\n", " ----------\n", " X: {array-like, 2D matrix}, shape(n_samples, 2)\n", " The set of all coordinates, where we have ground truth\n", " values\n", " y: array-like, shape(n_samples,)\n", " The set of all the ground truth values using which\n", " we perform interpolation\n", "\n", " Returns\n", " -------\n", " self : object\n", " Returns self\n", " \"\"\"\n", "\n", "# if self.coordinate_type == 'latlong_small':\n", "# \t \t\"\"\"\n", "# \t \t\tUse the conversions and projections for small changes in LatLong\n", "# \t\t\"\"\"\n", "# \t \t print (\"To be done later\")\n", "# return self\n", "\n", "# if self.coordinate_type == 'latlong_large':\n", "# \"\"\"\n", "# Code to be written after understanding all the projections.\n", "# \"\"\"\n", "# print (\"To be done later\")\n", "# return self\n", "\n", " if self.coordinate_type==\"Euclidean\":\n", " \n", " X = deepcopy(np.c_[X,y])\n", "\n", " if self.resolution=='high':\n", " xx,yy = self.make_grid(X,y,1000)\n", " \n", " if self.resolution=='low':\n", " xx,yy = self.make_grid(X,y,10)\n", " \n", " if self.resolution=='standard':\n", " xx,yy = self.make_grid(X,y,100)\n", "\n", " new = []\n", " new_arr = deepcopy(X)\n", " for points in new_arr:\n", " min_dist = np.inf\n", " val = 0\n", " for j in range(len(yy)):\n", " temp = yy[j][0]\n", " for i in range(len(xx[0])):\n", " dist = np.linalg.norm(np.array([xx[0][i],temp]) - points[:2])\n", " if dist" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = idw()\n", "import pandas as pd\n", "df = pd.read_csv('../../testdata/30-03-18.csv')\n", "data = np.array(df[['longitude','latitude','value']])\n", "a.fit(data[:,:2],data[:,2])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[171.89189189, 171.89597641, 171.90813547, ..., 173.89050472,\n", " 173.89261459, 173.89466512],\n", " [171.77142857, 171.77625338, 171.79060316, ..., 173.89585441,\n", " 173.89787202, 173.89983245],\n", " [171.63636364, 171.64211895, 171.65921778, ..., 173.9012935 ,\n", " 173.90321551, 173.90508269],\n", " ...,\n", " [174.49681529, 174.49676176, 174.49660126, ..., 174.24671184,\n", " 174.24416446, 174.24164382],\n", " [174.49056604, 174.49051451, 174.49035999, ..., 174.24671343,\n", " 174.24419773, 174.2417078 ],\n", " [174.48447205, 174.48442242, 174.48427358, ..., 174.2466762 ,\n", " 174.24419219, 174.24173298]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.interpolated_values" ] } ], "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }