Skip to content

Commit

Permalink
评价分类结果
Browse files Browse the repository at this point in the history
  • Loading branch information
cr-mao committed Sep 17, 2024
1 parent 75e0081 commit 447b1e2
Show file tree
Hide file tree
Showing 15 changed files with 2,688 additions and 0 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## F1 Score"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.255143Z",
"start_time": "2024-09-16T01:49:01.213811Z"
}
},
"source": [
"import numpy as np"
],
"outputs": [],
"execution_count": 1
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.259777Z",
"start_time": "2024-09-16T01:49:01.256712Z"
}
},
"source": [
"def f1_score(precision, recall):\n",
" try:\n",
" return 2 * precision * recall / (precision + recall)\n",
" except:\n",
" return 0.0"
],
"outputs": [],
"execution_count": 2
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.265857Z",
"start_time": "2024-09-16T01:49:01.261297Z"
}
},
"source": [
"precision = 0.5\n",
"recall = 0.5\n",
"f1_score(precision, recall)"
],
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 3
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.271443Z",
"start_time": "2024-09-16T01:49:01.267247Z"
}
},
"source": [
"precision = 0.1\n",
"recall = 0.9\n",
"f1_score(precision, recall)"
],
"outputs": [
{
"data": {
"text/plain": [
"0.18000000000000002"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 4
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.277461Z",
"start_time": "2024-09-16T01:49:01.274094Z"
}
},
"source": [
"precision = 0.0\n",
"recall = 1.0\n",
"f1_score(precision, recall)"
],
"outputs": [
{
"data": {
"text/plain": [
"0.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 5
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.737431Z",
"start_time": "2024-09-16T01:49:01.278368Z"
}
},
"source": [
"from sklearn import datasets\n",
"\n",
"digits = datasets.load_digits()\n",
"X = digits.data\n",
"y = digits.target.copy()\n",
"\n",
"y[digits.target==9] = 1\n",
"y[digits.target!=9] = 0"
],
"outputs": [],
"execution_count": 6
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.778433Z",
"start_time": "2024-09-16T01:49:01.738455Z"
}
},
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)"
],
"outputs": [],
"execution_count": 7
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.838414Z",
"start_time": "2024-09-16T01:49:01.779238Z"
}
},
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"\n",
"log_reg = LogisticRegression()\n",
"log_reg.fit(X_train, y_train)\n",
"# 准确率\n",
"log_reg.score(X_test, y_test)"
],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/anaconda3/envs/myenv3.10/lib/python3.10/site-packages/sklearn/linear_model/_logistic.py:469: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
"STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
"\n",
"Increase the number of iterations (max_iter) or scale the data as shown in:\n",
" https://scikit-learn.org/stable/modules/preprocessing.html\n",
"Please also refer to the documentation for alternative solver options:\n",
" https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
" n_iter_i = _check_optimize_result(\n"
]
},
{
"data": {
"text/plain": [
"0.9755555555555555"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 8
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.842496Z",
"start_time": "2024-09-16T01:49:01.839572Z"
}
},
"source": [
"y_predict = log_reg.predict(X_test)"
],
"outputs": [],
"execution_count": 9
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.848941Z",
"start_time": "2024-09-16T01:49:01.843599Z"
}
},
"source": [
"from sklearn.metrics import confusion_matrix\n",
"\n",
"confusion_matrix(y_test, y_predict)"
],
"outputs": [
{
"data": {
"text/plain": [
"array([[403, 2],\n",
" [ 9, 36]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 10
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.857132Z",
"start_time": "2024-09-16T01:49:01.850617Z"
}
},
"source": [
"from sklearn.metrics import precision_score\n",
"\n",
"precision_score(y_test, y_predict)"
],
"outputs": [
{
"data": {
"text/plain": [
"np.float64(0.9473684210526315)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 11
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.864113Z",
"start_time": "2024-09-16T01:49:01.858452Z"
}
},
"source": [
"from sklearn.metrics import recall_score\n",
"\n",
"recall_score(y_test, y_predict)"
],
"outputs": [
{
"data": {
"text/plain": [
"np.float64(0.8)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 12
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T01:49:01.870655Z",
"start_time": "2024-09-16T01:49:01.865342Z"
}
},
"source": [
"from sklearn.metrics import f1_score\n",
"\n",
"f1_score(y_test, y_predict)"
],
"outputs": [
{
"data": {
"text/plain": [
"np.float64(0.8674698795180723)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 13
}
],
"metadata": {
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 447b1e2

Please sign in to comment.