From 5f1fd3e10aa779d828a1d98795e96bed08813c53 Mon Sep 17 00:00:00 2001 From: Maila Faiza Shafira <113090048+maifzs@users.noreply.github.com> Date: Fri, 9 Jun 2023 17:54:58 +0700 Subject: [PATCH] Add files via upload --- model.ipynb | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 model.ipynb diff --git a/model.ipynb b/model.ipynb new file mode 100644 index 0000000..bc8d209 --- /dev/null +++ b/model.ipynb @@ -0,0 +1,126 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "R9TP3WqxNLCI" + }, + "outputs": [], + "source": [ + "import tensorflow as tf # for model arc\n", + "from tensorflow.keras.models import Sequential\n", + "from tensorflow.keras.layers import Dense, LSTM\n", + "import pandas as pd # for split dataset\n", + "from sklearn.model_selection import train_test_split\n", + "import numpy as np # for predict or test the model" + ] + }, + { + "cell_type": "code", + "source": [ + "# Load dataset\n", + "url = 'https://raw.githubusercontent.com/carbonzeroapp/carbonzero_ml_model/main/data/datasetKgCO2.csv'\n", + "dataset = pd.read_csv(url, delimiter=',', header=0)\n", + "\n", + "x = dataset.iloc[:,:-1].values\n", + "y = dataset.iloc[:,1].values\n", + "\n", + "# Split dataset into train and validation\n", + "x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=0)" + ], + "metadata": { + "id": "Jn8tVe8qNqKY" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Model architecture\n", + "model = Sequential([\n", + " Dense(64, activation='relu', input_shape=(None, 1)),\n", + " Dense(32, activation='relu'),\n", + " LSTM(4, activation='relu'),\n", + " Dense(32, activation='relu'),\n", + " Dense(4, activation='linear')])\n", + "\n", + "# Optimizer and loss for the model\n", + "model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n", + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", + " metrics=['acc'])" + ], + "metadata": { + "id": "CT6JG2tuN7yC" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Training the model\n", + "model.fit(tf.expand_dims(x_train, axis=-1), y_train, epochs=100, validation_data=(x_val, y_val), steps_per_epoch=10)" + ], + "metadata": { + "id": "YNyFqgvnONiV" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Dtype input can be int or float\n", + "input_data = np.array([[30]]) # Single numerical value as input\n", + "\n", + "# Make the prediction\n", + "prediction = model.predict(input_data)\n", + "\n", + "# The prediction will be an array of probabilities for each class\n", + "# If you have multiple classes, you can retrieve the predicted class with the highest probability\n", + "predicted_class = prediction.argmax()\n", + "\n", + "print(\"Predicted class:\", predicted_class)" + ], + "metadata": { + "id": "fNWPNNXPOoE3" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Note\n", + "\n", + "# The input is total emission (Kg CO2)\n", + "# Emission mean Indonesia for each individu is 42 per week\n", + "# The label for class are:\n", + " # '1' stands for 'low' from mean (< 32)\n", + " # '2' stands for 'average' around mean (32 - 52)\n", + " # '3' stands for 'high' from mean (> 52)" + ], + "metadata": { + "id": "_kzN5zJdu48e" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file