-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} | ||
] | ||
} |