Skip to content

Instantly share code, notes, and snippets.

@flyfir248
Created May 13, 2023 22:57
Show Gist options
  • Save flyfir248/8177c55f42386035b19317b3b8593aeb to your computer and use it in GitHub Desktop.
Save flyfir248/8177c55f42386035b19317b3b8593aeb to your computer and use it in GitHub Desktop.
Loss Function.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMV0zgKYtmdUfj3GUPgFMFZ",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/flyfir248/8177c55f42386035b19317b3b8593aeb/loss-function.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Wu0fk1jJUPlY",
"outputId": "9dcb22c6-33bb-4706-c4a1-0a9534c9d893"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"MSE: 2.9582283945787943e-31\n",
"MAE: 3.7007434154171886e-16\n",
"R2 Score: 1.0\n"
]
}
],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n",
"\n",
"# Sample data\n",
"X = [[0], [1], [2], [3], [4], [5]]\n",
"y = [1, 3, 5, 7, 9, 11]\n",
"\n",
"# Create instance of Linear Regression model with default loss function (least squares)\n",
"lr = LinearRegression()\n",
"lr.fit(X, y)\n",
"\n",
"# Calculate predictions using the model\n",
"y_pred = lr.predict(X)\n",
"\n",
"# Evaluate using different loss functions\n",
"mse = mean_squared_error(y, y_pred)\n",
"mae = mean_absolute_error(y, y_pred)\n",
"r2 = r2_score(y, y_pred)\n",
"\n",
"print(\"MSE: \", mse)\n",
"print(\"MAE: \", mae)\n",
"print(\"R2 Score: \", r2)"
]
},
{
"cell_type": "code",
"source": [
"from sklearn.linear_model import HuberRegressor\n",
"\n",
"# Create instance of Huber Regressor model with Huber loss\n",
"huber = HuberRegressor()\n",
"huber.fit(X, y)\n",
"\n",
"# Calculate predictions using the model\n",
"y_pred = huber.predict(X)\n",
"\n",
"# Evaluate using different loss functions\n",
"mse = mean_squared_error(y, y_pred)\n",
"mae = mean_absolute_error(y, y_pred)\n",
"r2 = r2_score(y, y_pred)\n",
"\n",
"print(\"MSE: \", mse)\n",
"print(\"MAE: \", mae)\n",
"print(\"R2 Score: \", r2)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SyU1Ua6oU6QD",
"outputId": "d08ea6ac-da74-4d76-abc4-f75210e7066e"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"MSE: 1.6773621058552543e-20\n",
"MAE: 1.0959936661928775e-10\n",
"R2 Score: 1.0\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "QgPJFSgMU8Xi"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment