Files
mlessentials/Lab06/Exercise6.03/Exercise6_03.ipynb
T
fenago f3b24b4b7f added
2021-02-07 15:16:01 +05:00

194 lines
4.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "eDVT1fgawPQi"
},
"source": [
"# Compute Mean Absolute Error"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "G_S8aKAuwPQk"
},
"outputs": [],
"source": [
"# import libraries\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import mean_absolute_error"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "7q-Lino2wPQu"
},
"outputs": [],
"source": [
"# import the data\n",
"# column headers\n",
"_headers = ['CIC0', 'SM1', 'GATS1i', 'NdsCH', 'Ndssc', 'MLOGP', 'response']\n",
"\n",
"# read in data\n",
"df = pd.read_csv('../Dataset/qsar_fish_toxicity.csv', names=_headers, sep=';')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "NppmnQCtwPQ3"
},
"outputs": [],
"source": [
"# Let's split our data\n",
"features = df.drop('response', axis=1).values\n",
"labels = df[['response']].values\n",
"\n",
"X_train, X_eval, y_train, y_eval = train_test_split(features, labels, test_size=0.2, random_state=0)\n",
"X_val, X_test, y_val, y_test = train_test_split(X_eval, y_eval, random_state=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "ZFlnPasswPRC"
},
"outputs": [],
"source": [
"# create a simple Linear Regression model\n",
"model = LinearRegression()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "N0wGdeauwPRL",
"outputId": "e8813bd4-fa92-49c4-88ea-dfbe6c91bece"
},
"outputs": [],
"source": [
"# train the model\n",
"model.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "60z-AiurwPRW"
},
"outputs": [],
"source": [
"# let's use our model to predict on our validation datast\n",
"y_pred = model.predict(X_val)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "FwNWlwdlwPRc",
"outputId": "0f691e8b-f35a-4c3e-d3a9-5cea904b1786"
},
"outputs": [],
"source": [
"# Let's compute our MEAN ABSOLUTE ERROR\n",
"mae = mean_absolute_error(y_val, y_pred)\n",
"print('MAE: {}'.format(mae))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "Y_N2YAtowPRl",
"outputId": "b81871b9-c764-4d08-9399-05d944c52d2c"
},
"outputs": [],
"source": [
"#Let's get the R2 score\n",
"r2 = model.score(X_val, y_val)\n",
"print('R^2 score: {}'.format(r2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "uK2HIBvgwPRo"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"name": "Exercise6_03.ipynb",
"provenance": []
},
"file_extension": ".py",
"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.8.6"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"nbformat": 4,
"nbformat_minor": 1
}