Skip to main content

PyRat

R-like vectors and functions in Python.

Warning

This is not Pandas, or scikit-learn, or numpy. This is not R, either. Do not use this for industry purposes. Do not use this unless you want your project to break.

Simply have fun with it, or use this for educational purposes.

Installation

Clone this repository to your local machine with git, then install with Python.

git clone https://github.com/shanedrabing/pyrat.git
cd pyrat
python setup.py install

Getting Started

Import the libraries with Python.

import pyrat.base
import pyrat.stats
import pyrat.utils
import pyrat.closure

The closure library is the only not inspired by R. All other libraries contain familiar functions. If you want to locate a function within R, look into the find function, determine the namespace, and see if PyRat contains the function!

Motivating Examples

So, why use PyRat? If you're a fan of R (like myself), you may have learned that the capabilities of vectors and functions that operate on them are immensely powerful.

Linear Modeling

Let's try modeling a line of best fit between horsepower and miles per gallon from the mtcars dataset:

import matplotlib.pyplot as plt

from pyrat.base import log10, seq
from pyrat.stats import lm, predict
from pyrat.utils import read_csv, struct

df = read_csv("data/mtcars.csv")

x = df["hp"]
y = df["mpg"]

m = lm(log10(x), log10(y))

x_new = seq(min(x), max(x), length_out=101)
y_new = 10 ** predict(m, log10(x_new))

plt.scatter(x, y, color="black")
plt.plot(x_new, y_new, color="red")
plt.xlabel("Horsepower")
plt.ylabel("Miles per Gallon")
plt.tight_layout()
plt.savefig("data/fit.png")
plt.clf()

A plot of the fitted linear model:

data/fit.png

Functional Programming

PyRat has its own version of "piping", whereby the items of a vector (or the vector itself) can be continuously operated on. All intermediates are immutable, as the vector is just a tuple on steroids.

from pyrat.base import c, paste

taxa = c("Canis lupus", "Felis catus", "Ursidae", "Anura (order)")

print(
    taxa
    .apply("https://en.wikipedia.org/w/index.php?search={}".format)
    .thread(requests.get)
    .apply(getattr, "content")
    .apply(bs4.BeautifulSoup, "lxml")
    .apply(bs4.BeautifulSoup.select_one, "h1")
    .apply(getattr, "text")
    .transform(paste, "(" + taxa + ")", collapse="\n")
)

Here's the output:

Wolf (Canis lupus)
Cat (Felis catus)
Bear (Ursidae)
Frog (Anura (order))

Metadata

Release files for pyrat-sdrabing 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyrat-sdrabing 0.2.1
File Size Uploaded
pyrat-sdrabing-0.2.1.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyrat-sdrabing 0.2.1
File Interpreter ABI Platform
pyrat_sdrabing-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.3 kB

Release files / pyrat-sdrabing-0.2.1.tar.gz

Download URL pyrat-sdrabing-0.2.1.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d147111850a42ddf777bfb3b1c2ddacafce039d1b43c904762a93f1aa42e1ad6
BLAKE2b-256 checksum
How to use checksums
4886a24775e4941bf9ec4fbbf3506e3159ac611dbda2fc06805b7fb357994e3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/3.8.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.8.10

Release files / pyrat_sdrabing-0.2.1-py3-none-any.whl

Download URL pyrat_sdrabing-0.2.1-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6976014eeca2be29b5fcd3a65e662de167475cadc910136a1b7d90d1396d0ff
BLAKE2b-256 checksum
How to use checksums
06af2bdf48cea1b2c6643b1c8d62f0fcc04cbf66e8b5501779da671b30376665
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/3.8.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page