Zadanie Biblioteka_DL

This commit is contained in:
s464903 2024-04-23 22:35:16 +02:00
parent 51236af46f
commit 0f7264d4d1
4 changed files with 1770 additions and 0 deletions

View File

@ -0,0 +1,250 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0f5b3979-7704-4ea7-ac84-424f73e9a62d",
"metadata": {},
"source": [
"# Predykcja na modelu zapisanym w pliku"
]
},
{
"cell_type": "markdown",
"id": "ef017730-4bbd-4fad-926a-2800fa636ccd",
"metadata": {},
"source": [
"## Import bibliotek"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a330d65d-8876-49b8-9507-27ee9f50d1e6",
"metadata": {},
"outputs": [],
"source": [
"import keras\n",
"import pandas as pd\n",
"from sklearn.preprocessing import MinMaxScaler, OneHotEncoder\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "markdown",
"id": "4bd52369-8c81-45a1-b091-5edae9bc2673",
"metadata": {},
"source": [
"## Pobranie modelu z pliku"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "aa7d4e1f-2924-4c84-a06b-f19b451e923b",
"metadata": {},
"outputs": [],
"source": [
"model = keras.models.load_model('./model.keras')"
]
},
{
"cell_type": "markdown",
"id": "1c94dca7-7670-4ed9-b6ca-5c59c734cc44",
"metadata": {},
"source": [
"## Przygotowanie danych do testowania modelu"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3e72f7ea-91a6-45ac-9f38-872175ad2a8d",
"metadata": {},
"outputs": [],
"source": [
"dataset = pd.read_csv('./lettuce-growth-days/lettuce_dataset_updated.csv', encoding='ISO-8859-1')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9f52a6e7-ae62-4ea6-bde5-e2b221fa2d7a",
"metadata": {},
"outputs": [],
"source": [
"ph_level = dataset['pH Level'].values.tolist()\n",
"temp_F = dataset['Temperature (F)'].values.tolist()\n",
"humid = dataset['Humidity'].values.tolist()\n",
"days = dataset['Growth Days'].values.tolist()\n",
"plant_id = dataset['Plant_ID'].values.tolist()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a720aa83-bb5a-4c6f-89a5-f6e1fb7cf0e2",
"metadata": {},
"outputs": [],
"source": [
"X = []\n",
"Y = []\n",
"\n",
"id = plant_id[0]\n",
"temp_sum = 0\n",
"humid_sum = 0\n",
"ph_level_sum = 0\n",
"day = 1\n",
"\n",
"for i in range(0, len(plant_id)):\n",
" if plant_id[i] == id:\n",
" temp_sum += temp_F[i]\n",
" humid_sum += humid[i]\n",
" ph_level_sum += ph_level[i]\n",
" day = days[i]\n",
" else:\n",
" temp = []\n",
" temp.append(temp_sum/day)\n",
" temp.append(humid_sum/day)\n",
" temp.append(ph_level_sum/day)\n",
" X.append(temp)\n",
" Y.append(day)\n",
" temp_sum = 0\n",
" humid_sum = 0\n",
" ph_level_sum = 0\n",
" day = 1\n",
" id = plant_id[i]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d8ef6898-38bb-4191-b7b9-bc47dd1ae5d8",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\obses\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:808: FutureWarning: `sparse` was renamed to `sparse_output` in version 1.2 and will be removed in 1.4. `sparse_output` is ignored unless you leave `sparse` to its default value.\n",
" warnings.warn(\n"
]
}
],
"source": [
"scaler = MinMaxScaler()\n",
"X = scaler.fit_transform(X)\n",
"X = np.array(X)\n",
"Y = np.array(Y)\n",
"\n",
"encoder = OneHotEncoder(sparse=False)\n",
"y_onehot = encoder.fit_transform(Y.reshape(-1,1))\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y_onehot, test_size=0.4, random_state=42)"
]
},
{
"cell_type": "markdown",
"id": "72650088-d370-4814-b381-6e5a5e01fe58",
"metadata": {},
"source": [
"## Testowanie modelu"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ed8d7cb0-eefd-4e64-80e1-4ea4ff6bb369",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 216ms/step - accuracy: 0.8571 - loss: 0.7300\n",
"Dokładność testowa: 85.71%\n"
]
}
],
"source": [
"test_loss, test_accuracy = model.evaluate(X_test, y_test)\n",
"print(f\"Dokładność testowa: {test_accuracy:.2%}\")"
]
},
{
"cell_type": "markdown",
"id": "5f0bceec-f95e-4192-aced-c9997eafee28",
"metadata": {},
"source": [
"## Predykcja na zbiorze testowym"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6ce776f4-859b-42d9-a756-bd29b11d1b56",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 32ms/step\n"
]
}
],
"source": [
"predictions = model.predict(X_test)"
]
},
{
"cell_type": "markdown",
"id": "9908472c-08ae-4e46-afe3-27eefb43f671",
"metadata": {},
"source": [
"## Zapisanie wyników predykcji do pliku tekstowego"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "738043a9-a312-4f96-af22-593362bc9624",
"metadata": {},
"outputs": [],
"source": [
"with open(\"predictions.txt\", \"w\") as txt_file:\n",
" for line in predictions:\n",
" txt_file.write(\" \".join(str(line)) + \"\\n\") "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da604cf2-d13d-4dd3-a596-7b24ba5d92ef",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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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