{ "cells": [ { "cell_type": "markdown", "id": "d24d3c2e", "metadata": {}, "source": [ "## Import Necessary Libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "5d7cbeb2", "metadata": {}, "outputs": [], "source": [ "from sklearn.metrics import mean_squared_error\n", "from sklearn.neural_network import MLPRegressor\n", "from sklearn.model_selection import RandomizedSearchCV\n", "from sklearn.datasets import make_regression\n", "from sklearn.model_selection import train_test_split\n", "from scipy.stats import randint as sp_randint\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "7e2197d2", "metadata": {}, "source": [ "## Pull in Texture and Mesri Estimated Friction Angles from Excel Sheet" ] }, { "cell_type": "code", "execution_count": 2, "id": "2c20df52", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\dmfr224.AD\\Anaconda3\\lib\\site-packages\\openpyxl\\worksheet\\header_footer.py:48: UserWarning: Cannot parse header or footer so it will be ignored\n", " warn(\"\"\"Cannot parse header or footer so it will be ignored\"\"\")\n" ] } ], "source": [ "Regress = pd.read_excel(r'F:\\Research\\Journal Paper #2\\Machine Learning Friction Angles\\Site Texture and Mesri Friction Angles at KYTC Boreholes.xlsx')" ] }, { "cell_type": "markdown", "id": "b56f7abb", "metadata": {}, "source": [ "## Assign Training and Response Variables" ] }, { "cell_type": "code", "execution_count": 3, "id": "a6f2b699", "metadata": {}, "outputs": [], "source": [ "X = Regress[['Percent Sand', 'Percent Silt', 'Percent Clay']] #Removed PI and LL\n", "Response = Regress[['Friction Angle']] #Terzaghi Friction Angle from Plasticity Index\n", "X_train, X_test, y_train, y_test = train_test_split(X, Response, test_size=0.2, random_state=0)" ] }, { "cell_type": "markdown", "id": "4c2ae198", "metadata": {}, "source": [ "## Use RandomizedSearchCV to Find Best Tuning for Training" ] }, { "cell_type": "code", "execution_count": null, "id": "2ba394d0", "metadata": {}, "outputs": [], "source": [ "# specify parameters and distributions to sample from\n", "param_dist = {\"random_state\": (0,1),\n", " \"activation\": ['identity'], \n", " \"solver\": ['adam'],\n", " \"max_iter\": sp_randint(200, 100000),\n", " \"hidden_layer_sizes\": sp_randint(1100, 1150),\n", " \"learning_rate\":['adaptive']}\n", "random_search = RandomizedSearchCV(estimator = regr, param_distributions=param_dist,\n", " n_iter=10000, cv=5, verbose = 3, random_state=42, n_jobs = -1)\n", "random_search.fit(X_train, y_train.values.ravel());\n", "\n", "print(random_search.best_score_)" ] }, { "cell_type": "markdown", "id": "96c15837", "metadata": {}, "source": [ "## Show Best Parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "aa0433db", "metadata": {}, "outputs": [], "source": [ "print(random_search.best_params_)" ] }, { "cell_type": "markdown", "id": "a7024283", "metadata": {}, "source": [ "## Construct MLP Regressive Model Based on RandomizedSearchCV Parameters\n", "May differ due to randomization" ] }, { "cell_type": "code", "execution_count": 4, "id": "7db0386d", "metadata": {}, "outputs": [], "source": [ "regr = MLPRegressor(random_state=1, activation='identity', hidden_layer_sizes=1104, learning_rate='adaptive', max_iter=95635, solver='adam', ).fit(X_train, y_train.values.ravel())" ] }, { "cell_type": "markdown", "id": "491ad994", "metadata": {}, "source": [ "## Predict Friction Angle using Trained Model and Test Data" ] }, { "cell_type": "code", "execution_count": 5, "id": "c957712c", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([29.08695336, 28.63646272, 33.34170334, 31.56994005, 32.30337337,\n", " 28.39146397, 28.91747906, 32.30337337, 31.09844273, 28.3024598 ,\n", " 28.66782231, 31.77309641, 28.83174369, 33.96006796, 31.93156479,\n", " 34.44198829, 27.52909642, 31.80464005, 29.31804079, 32.47641638])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Predicted = regr.predict(X_test)" ] }, { "cell_type": "markdown", "id": "cd9b55b1", "metadata": {}, "source": [ "## Retrieve RMSE of Predicted vs True Data" ] }, { "cell_type": "code", "execution_count": 7, "id": "e4992980", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8604795387351668\n" ] } ], "source": [ "rms = mean_squared_error(y_test, Predicted, squared=False)\n", "print(rms)" ] }, { "cell_type": "markdown", "id": "8e1200a5", "metadata": {}, "source": [ "The trained model can be seen to perform well (RMSE = 0.86) and is considered valid for use in predicting friction angles based on soil texture data over unseen data" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }