ium_464903/Biblioteka_DL_predykcja.ipynb
2024-04-23 22:35:16 +02:00

251 lines
6.2 KiB
Plaintext

{
"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.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}