Code Monkey home page Code Monkey logo

pyrat's Introduction

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))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.