Quantcast
Channel: Machine Learning
Viewing all articles
Browse latest Browse all 62779

Question on Sparse Matrix Factorization in Python

$
0
0

I apologize in advance if this is not the most appropriate place to post this question, but I was struggling with this in Python (I'm more of an R Programmer), and I thought this subreddit, in particular, would know the solution.

I am attempting to take a highly cardinal variable (i.e., a character variable stored as a vector with many levels; e.g., States in the US) and store it as a sparse matrix.

In R, there is a function called sparse.model.matrix, which does all of the real leg work for me. To my knowledge, there is no Python analogue of this command when the inputs are character values.

This led me to try and make the character vector a numeric (the most obvious solution), but I soon discovered this was non-trivial (at least, that's what my initial research has suggested). In R, it's relatively easy to convert a categorical variable into a numeric one by the following:

> x <- c('IL','AL','NY','MA','CA') > is.character(x) [1] TRUE > x <- as.numeric(factor(x)) > x [1] 3 1 5 4 2 

You'll notice that R turns the categories into integers based on their alphabetic order.

So, I have two questions:

1: Is there a function that automatically creates a series of variables (stored in two ways: character or numeric) and automatically maps them to a sparse matrix?

2: Is there a quick function that can take a character vector and map it to a numeric vector in the same way that I have shown above?

I suppose I could code this function myself, but I thought that I would likely be reinventing the wheel and so I thought I would consult all of you before poisoning myself with a Python bite.

Thank you in advance.

submitted by econometrician
[link][3 comments]

Viewing all articles
Browse latest Browse all 62779

Trending Articles