Files
fenago f3b24b4b7f added
2021-02-07 15:16:01 +05:00

218 lines
4.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "CmmK_6k3zT77"
},
"source": [
"# Compute MAE of Second Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "_M12PDnMzT79"
},
"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.pipeline import Pipeline\n",
"from sklearn.metrics import mean_absolute_error\n",
"\n",
"#preprocessing\n",
"from sklearn.preprocessing import MinMaxScaler\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.preprocessing import PolynomialFeatures"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "wt7hxsA5zT8C"
},
"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": "xtpAg5ZBzT8H"
},
"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": "fhqgT4IfzT8L"
},
"outputs": [],
"source": [
"#create a pipeline and engineer quadratic features\n",
"steps = [\n",
" ('scaler', MinMaxScaler()),\n",
" ('poly', PolynomialFeatures(2)),\n",
" ('model', LinearRegression())\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "YJWIvVF6zT8O"
},
"outputs": [],
"source": [
"#create a Linear Regression model\n",
"model = Pipeline(steps)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 177
},
"colab_type": "code",
"id": "waWYDKlbzT8S",
"outputId": "cc63bd38-323f-42ca-d23f-47f23eb22267"
},
"outputs": [],
"source": [
"#train the model\n",
"model.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "63gN84ZvzT8W"
},
"outputs": [],
"source": [
"#predict on validation dataset\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": "vUSTLx8szT8b",
"outputId": "8c48d04e-5842-4e8a-a96e-0d4e177f06f7"
},
"outputs": [],
"source": [
"#compute MAE\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": "K-Y699PSzT8h",
"outputId": "22835320-717d-49d5-fab1-f784391bec5a"
},
"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": "Wa6PUbhnzT8l"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"name": "Exercise6_04.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
}