Skip to content

Instantly share code, notes, and snippets.

View ivopbernardo's full-sized avatar

ivopbernardo

View GitHub Profile
@ivopbernardo
ivopbernardo / h2o_example.r
Created November 1, 2022 19:14
h2o R Example
# Load h2o
library(h2o)
library(ggplot2)
# Load Dataset - London Bike
london_bike <- read.csv('./london_merged.csv')
# Transforming Weather code and Season to factor
london_bike$weather_code <- as.factor(london_bike$weather_code)
london_bike$season <- as.factor(london_bike$season)
@ivopbernardo
ivopbernardo / caret_examples.r
Last active February 7, 2024 16:11
Caret Library Example - R Language
# caret library example used in blogpost:
# https://towardsdatascience.com/a-guide-to-using-caret-in-r-71dec0bda208
library(caTools)
library(caret)
# Train Test Split on both Iris and Mtcars
train_test_split <- function(df) {
set.seed(42)
@ivopbernardo
ivopbernardo / function_best_practices.r
Last active November 2, 2022 09:26
Blog Post - Function Best Practices
# R Function Best Practices used in blog post:
# https://towardsdatascience.com/writing-better-r-functions-best-practices-and-tips-d48ef0691c24
library(ggplot2)
#----------------------------------#
# Function Indentation
# Proper Indentation - Bad Example
@ivopbernardo
ivopbernardo / mlr_hyperparam.r
Last active November 2, 2022 09:28
Training Hyperparameters with mlr Blog Post
# mlr library example clode - used in blog post:
# https://towardsdatascience.com/decision-tree-hyperparameter-tuning-in-r-using-mlr-3248bfd2d88c
titanic <- read.csv('train.csv')
library(dplyr)
library(rpart)
library(rpart.plot)
library(Metrics)
library(mlr)
@ivopbernardo
ivopbernardo / dplyr.r
Last active November 2, 2022 09:29
Some functions from the dplyr Library
# dplyr library example used in blog post:
# https://towardsdatascience.com/8-cool-dplyr-function-to-learn-in-r-8736d7fa899c
library(dplyr)
starwars_df <- starwars
# Filter using Dplyr
filter_droids <- starwars %>%
filter(species == 'Droid')
import rasterio
from rasterio.plot import show
url = "zip+file:data/mdt.zip!mdt.tif"
lisbon_elevation = rasterio.open(url)
# Plot the raster data to get a sense of it
show(lisbon_elevation, cmap="terrain")
# Get the elevation from the raster data
# Read the metro data
metro = pd.read_csv(Path("data", "metro_stations.csv"))
# Convert to a GeoDataFrame
metro = gpd.GeoDataFrame(
metro,
geometry=gpd.points_from_xy(metro.longitude, metro.latitude),
crs="epsg:4326"
).to_crs(epsg=3857)
# Public Hospitals in Lisbon
hospitals_url = "https://opendata.arcgis.com/datasets/172678f193144512860a397fde991361_4.geojson" # GeoJSON
hospitals_gdf = gpd.read_file(hospitals_url).to_crs(epsg=3857)
hospitals_gdf.head()
# Buffer the house locations by 1km
house_data_gdf_buffer = (
house_data_gdf
.copy()
.assign(geometry_buffer = lambda d: d.buffer(1000))
# Read data directly from the portuguese gov website.
parishes_url = "zip+https://dados.gov.pt/s/resources/freguesias-de-portugal/20181112-195834/cont-aad-caop2017.zip"
parishes = gpd.read_file(parishes_url)
# Left Join the house data to the parishes data, if house is `within` parish.
house_data_gdf = gpd.sjoin(house_data_gdf, parishes, how="left", op="within")
@ivopbernardo
ivopbernardo / convert_dataframe_to_geodataframe.py
Created May 5, 2022 21:34
DareData Blog Post about GeoData
import geopandas as gpd
house_data_gdf = gpd.GeoDataFrame(
house_data,
geometry=gpd.points_from_xy(
house_data.longitude,
house_data.latitude
),
crs="epsg:4326",
)