/logo-square.png

Sparse Matrix Construction And Use In R

In this post, we’ll cover the basics of constructing and using sparse matrices with R’s Matrix package. For background on what sparse matrices are and how they’re stored in compressed formats, check out my previous article Sparse Matrix Storage Formats. Sparse Matrix Construction Sparse Matrix From Base R Matrix library(Matrix) # Build a base R matrix from scratch, comprised of # 0s with probability 0.80 # 1s with probability 0.

Sparse Matrix Storage Formats

Sparse matrices arise natrually in many problems. For example, if we want to predict the price of an item on craigslist using the post’s text, we could build a matrix where each row represents a craigslist post, each column represents a keyword {bad, boat, car, good, new, shoes, used}, and element $ (i,j) $ represents the number of times keyword $ j $ appears in post $ i $. PostId Post post 123 selling my boat post 225 used car for sale.

Introduction To Inheritance In C++

Setup In this tutorial, we’ll learn how inheritance works in C++ by tinkering with an example. Let’s start by building simple Dog and Cat classes. Dog Class Create a Dog class that Has member variables: string name_ double weight_ Has member functions: void bark() that prints “woof woof” to the console double top_speed() that returns the dog’s top speed based on its weight Has a Dog(string name, double weight) constructor that prints “Dog Constructor” to the console #include <iostream>#include <string> class Dog { public: // Constructor Dog(std::string name, double weight): name_{name}, weight_{weight} { std::cout << "Dog Constructor" << std::endl; }; // Getters std::string get_name() { return name_; }; double get_weight() { return weight_; }; // Methods void bark() { std::cout << "woof woof" << std::endl; }; double top_speed() { return (weight_ < 40) ?

Using Rcpp in Xcode

Introduction In this tutorial, we’ll look at how you can configure Xcode to work with and debug Rcpp objects and methods. Why? I’m interested in building fast machine learning models with C++ and exposing those models to R, much like ranger and xgboost. Rcpp makes it easy to pass objects between R and C++. However, the only way to debug Rcpp code within RStudio that I’m aware of is to litter the code with print statements.

Sorting A Vector In C++

Here we’ll look at one of the most common data manipulation tasks - sorting. Specifically, we’ll be using the STL’s sort method. Basics To get the ball rolling, let’s see how to sort a vector of integers. #include <iostream> // std::cout#include <algorithm> // std::sort#include <vector> // std::vector int main() { std::vector<int> foo = {5, 3, 10, 1, 7}; // Make values std::sort(foo.begin(), foo.end()); // Sort for(int &x : foo) std::cout << x << " "; // Print } 1 3 5 7 10

Making A Binary Search Tree in C++

This article is about implementing a Binary Search Tree (BST) in C++. I’ll skip the part about defining what a BST is since that’s a horse that’s been beaten many times. I am new to C++, so my implementation may have flaws. I welcome and encourage critique from other programmers :) Draft 1 We start by implementing a TreeNode struct. struct TreeNode { // member vars int data; TreeNode* left; TreeNode* right; // constructor TreeNode(int data): data(data), left(nullptr), right(nullptr) {} }; Notes: