{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example: ML regression on hyperspectral dataset with Python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.linear_model import LinearRegression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load data\n", "\n", "Repository: https://github.com/felixriese/hyperspectral-soilmoisture-dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load dataframe\n", "path = \"https://raw.githubusercontent.com/felixriese/hyperspectral-soilmoisture-dataset/master/soilmoisture_dataset.csv\"\n", "df = pd.read_csv(path, index_col=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get hyperspectral bands:\n", "hypbands = []\n", "for col in df.columns:\n", " try:\n", " int(col)\n", " except Exception:\n", " continue\n", " hypbands.append(col)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# split dataset\n", "X_train, X_test, y_train, y_test = train_test_split(\n", " df[hypbands], df[\"soil_moisture\"],\n", " test_size=0.5, random_state=42, shuffle=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Regression" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lg = LinearRegression()\n", "lg.fit(X_train, y_train)\n", "lg.score(X_test, y_test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }