{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from sklearn.metrics import accuracy_score, mean_squared_error\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.datasets import make_classification, make_regression\n", "from sklearn.linear_model import LogisticRegression, LinearRegression" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 0.90\n" ] } ], "source": [ "\n", "# Example of using Accuracy in a classification task\n", "# Creating a synthetic dataset for a binary classification\n", "X_class, y_class = make_classification(n_samples=1000, n_features=2, n_redundant=0, n_clusters_per_class=1, weights=[0.5], flip_y=0, random_state=1)\n", "\n", "# Splitting dataset into training and testing sets\n", "X_train_class, X_test_class, y_train_class, y_test_class = train_test_split(X_class, y_class, test_size=0.2, random_state=42)\n", "\n", "# Training a logistic regression classifier\n", "classifier = LogisticRegression()\n", "lr = classifier.fit(X_train_class, y_train_class)\n", "\n", "# Predicting the test set results\n", "y_pred_class = classifier.predict(X_test_class)\n", "\n", "# Calculating accuracy\n", "accuracy = accuracy_score(y_test_class, y_pred_class)\n", "print(f\"Accuracy: {accuracy:.2f}\")\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mean Squared Error: 0.01\n" ] } ], "source": [ "\n", "# Example of using Mean Squared Error in a regression task\n", "# Creating a synthetic dataset for regression\n", "X_reg, y_reg = make_regression(n_samples=100, n_features=1, noise=0.1, random_state=1)\n", "\n", "# Splitting dataset into training and testing sets\n", "X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X_reg, y_reg, test_size=0.2, random_state=42)\n", "\n", "# Training a linear regression model\n", "regressor = LinearRegression()\n", "regressor.fit(X_train_reg, y_train_reg)\n", "\n", "# Predicting the test set results\n", "y_pred_reg = regressor.predict(X_test_reg)\n", "\n", "# Calculating mean squared error\n", "mse = mean_squared_error(y_test_reg, y_pred_reg)\n", "print(f\"Mean Squared Error: {mse:.2f}\")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }