Skip to content

Instantly share code, notes, and snippets.

@diogojc
diogojc / ridge.py
Created December 25, 2011 21:11
Ridge Regression
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
class RidgeRegressor(object):
"""
Linear Least Squares Regression with Tikhonov regularization.
@lukegre
lukegre / LG_xarray_tools.py
Last active December 10, 2020 20:06
function that takes the trend for an xarray.DataArray over the 'time' variable. Returns the slopes and p-values for each location.
def dataset_encoding(xds):
cols = ['source', 'original_shape', 'dtype', 'zlib', 'complevel', 'chunksizes']
info = pd.DataFrame(columns=cols, index=xds.data_vars)
for row in info.index:
var_encoding = xds[row].encoding
for col in info.keys():
info.ix[row, col] = var_encoding.pop(col, '')
return info
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@macoj
macoj / hide_code_pdf.md
Last active December 17, 2020 15:20
Hiding code when exporting jupyter notebook to pdf
@johnhw
johnhw / umap_sparse.py
Last active November 15, 2024 23:31
1 million prime UMAP layout
### JHW 2018
import numpy as np
import umap
# This code from the excellent module at:
# https://stackoverflow.com/questions/4643647/fast-prime-factorization-module
import random
@jthielen
jthielen / wrf_python_extraction.py
Created May 3, 2019 18:44
Use wrf-python, xarray, and pyproj to post-process WRF output
from wrf import getvar
from netCDF4 import Dataset
import xarray as xr
import pyproj
# Extract the variables of interest at time index 17
ds = Dataset('../wrfout_d02_2015-07-12_1200.nc')
variables = [getvar(ds, var, 17) for var in ('z', 'dbz', 'pressure', 'ter', 'ua',
'va', 'wa', 'temp', 'rh')]
data = xr.merge(variables)
@rabernat
rabernat / detrend_xarray.py
Last active November 19, 2023 05:47
Detrend xarray DataArrays using polyfit
import xarray as xr
def detrend_dim(da, dim, deg=1):
# detrend along a single dimension
p = da.polyfit(dim=dim, deg=deg)
fit = xr.polyval(dim, p.polyfit_coefficients)
return da - fit
def detrend(da, dims, deg=1):
# detrend along multiple dimensions
@brews
brews / xarray_wrap_around_pixel_demo.py
Last active October 7, 2024 14:40
Demo using cartopy.util.add_cyclic_point with an xarray Dataset to add a cyclic or wrap-around pixel to the `lon` dimension. This can be useful for plotting with `cartopy` or regridding with `xesmf`.
"""
Demo using cartopy.util.add_cyclic_point with an xarray Dataset to
add a cyclic or wrap-around pixel to the `lon` dimension. This can be useful
for plotting with `cartopy` or regridding with `xesmf`.
"""
import xarray as xr
from cartopy.util import add_cyclic_point