R-like vectors and functions in Python.
Project description
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:
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))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyrat-sdrabing-0.2.1.tar.gz
.
File metadata
- Download URL: pyrat-sdrabing-0.2.1.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d147111850a42ddf777bfb3b1c2ddacafce039d1b43c904762a93f1aa42e1ad6 |
|
MD5 | dcd204a455d6eb876651df0fe10ff9cc |
|
BLAKE2b-256 | 4886a24775e4941bf9ec4fbbf3506e3159ac611dbda2fc06805b7fb357994e3f |
File details
Details for the file pyrat_sdrabing-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: pyrat_sdrabing-0.2.1-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6976014eeca2be29b5fcd3a65e662de167475cadc910136a1b7d90d1396d0ff |
|
MD5 | 180057439c153e6c16eeeb80d0bf74ce |
|
BLAKE2b-256 | 06af2bdf48cea1b2c6643b1c8d62f0fcc04cbf66e8b5501779da671b30376665 |