Spaces:
Running
Running
File size: 3,099 Bytes
d64a508 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
{
"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
}
|