Files
ai-med-edu/seminars/Тема 1.1/Dataset.ipynb
T

176 lines
4.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "553zXx91g7fz"
},
"source": [
"Пример загрузки данных с сайта Breast Cancer Wisconsin (Original)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JYb7HU1k9lFC",
"outputId": "7a608a7c-9f85-4cc8-9b72-65a67381e960"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.metrics import accuracy_score\n",
"\n",
"# 1) Ссылка на датасет\n",
"dataset_url = \"https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data\"\n",
"\n",
"# 2) Имена столбцов (по документации UCI)\n",
"col_names = [\n",
" \"Sample_code_number\",\n",
" \"Clump_Thickness\",\n",
" \"Uniformity_of_Cell_Size\",\n",
" \"Uniformity_of_Cell_Shape\",\n",
" \"Marginal_Adhesion\",\n",
" \"Single_Epithelial_Cell_Size\",\n",
" \"Bare_Nuclei\",\n",
" \"Bland_Chromatin\",\n",
" \"Normal_Nucleoli\",\n",
" \"Mitoses\",\n",
" \"Class\"\n",
"]\n",
"\n",
"# 3) Считываем csv\n",
"df = pd.read_csv(\n",
" dataset_url,\n",
" names=col_names,\n",
" na_values=[\"?\"],\n",
" header=None\n",
")\n",
"\n",
"# 4) Быстрый осмотр\n",
"print(df.head())\n",
"print(df.info())\n",
"\n",
"# 5) Удалим строки с пропусками\n",
"df.dropna(inplace=True)\n",
"\n",
"# 6) Разделим на признаки (X) и целевую переменную (y)\n",
"y = df[\"Class\"]\n",
"X = df.drop([\"Sample_code_number\", \"Class\"], axis=1)\n",
"\n",
"# 7) Разделим на train и test\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)\n",
"\n",
"# 8) Обучим модель (DecisionTree)\n",
"clf = DecisionTreeClassifier(random_state=42)\n",
"clf.fit(X_train, y_train)\n",
"\n",
"# 9) Предсказание и оценка\n",
"predictions = clf.predict(X_test)\n",
"acc = accuracy_score(y_test, predictions)\n",
"print(f\"Accuracy: {acc:.3f}\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2t5p4Mt7hKSw"
},
"source": [
"Пример кода с requests + BeautifulSoup. Подставить нужную ссылку"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1L4ZnJL5iDMm"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 321
},
"id": "SqUDAUG0esKO",
"outputId": "150062f1-72e8-4f8e-b006-12ef211609ab"
},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup\n",
"import pandas as pd\n",
"\n",
"url = \"https://opendata.some-region.ru/datasets/interesting-table\"\n",
"response = requests.get(url)\n",
"soup = BeautifulSoup(response.text, \"html.parser\")\n",
"\n",
"# Предположим, на странице есть <table> с классом \"data-table\"\n",
"table_html = soup.find(\"table\", {\"class\": \"data-table\"})\n",
"\n",
"# Преобразуем в DataFrame, если таблица более-менее стандартная\n",
"df_list = pd.read_html(str(table_html)) # возвращает список df\n",
"df = df_list[0] # берем первую таблицу\n",
"\n",
"df.head()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "dl9b0hqOiFII"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"excel_url = \"https://data.gov.ru/files/dataset.xls\"\n",
"df_excel = pd.read_excel(excel_url, sheet_name=\"Sheet1\")\n",
"print(df_excel.columns)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "A_13q1K9iMr2"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"json_url = \"https://data.gov.ru/files/dataset.json\"\n",
"df_json = pd.read_json(json_url, orient=\"records\")\n",
"print(df_json.info())\n"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}