Skip to content

Instantly share code, notes, and snippets.

View chelseatroy's full-sized avatar

Chelsea Troy chelseatroy

View GitHub Profile
@chelseatroy
chelseatroy / base.py
Created September 26, 2018 03:38
spmatrix nonzero implementation
def nonzero(self):
"""nonzero indices
Returns a tuple of arrays (row,col) containing the indices
of the non-zero elements of the matrix.
Examples
--------
>>> from scipy.sparse import csr_matrix
>>> A = csr_matrix([[1,2,0],[0,0,3],[4,0,5]])
@chelseatroy
chelseatroy / coo.py
Last active September 26, 2018 04:08
nonzero alternative implementation
class coo_1d(_data_matrix, ...):
...
def row(self):
return np.zeros(self.col.shape[0])
def nonzero(self):
...
nz_mask = A.data != 0
return (None, A.col[nz_mask])
@chelseatroy
chelseatroy / coo.py
Created September 26, 2018 03:56
alternative coo implementation
class coo_matrix(_data_matrix, _minmax_mixin):
...
format = 'coo'
def __init__(self, arg1, shape=None, dtype=None, copy=False):
...
else:
#dense argument
@chelseatroy
chelseatroy / tfidf_vectorization_with_pandas.py
Last active March 29, 2025 20:49
Tf-Idf Vectorization with Pandas
import pandas as pd
import numpy as np
import itertool
from nltk import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
df = pd.read_csv('my_data_with_text.csv')
df.columns #id, text, category
@chelseatroy
chelseatroy / models.py
Created October 18, 2018 14:58
Example Bird Class for Transactional AI App
class Bird(models.Model):
species_choices = [
("Spix's Macaw", "Spix's Macaw"),
("Cockatiel", "Cockatiel"),
...
("Caique", "Caique"),
("Guam Kingfisher", "Guam Kingfisher")
]
species = models.CharField(max_length=50, choices=species_choices)
@chelseatroy
chelseatroy / birds.csv
Created October 18, 2018 16:52
Example Bird Data
id species image
1 Cockatiel images/cockatiel-1.jpg
2 Spix's Macaw images/spixmacaw-1.jpg
3 Cockatiel images/cockatiel-2.jpg
4 Caique images/caique-1.jpg
@chelseatroy
chelseatroy / arraytypes.c.src
Created October 25, 2018 05:38
Dot Products in Numpy
/**begin repeat
*
* #name = BYTE, UBYTE, SHORT, USHORT, INT, UINT,
* LONG, ULONG, LONGLONG, ULONGLONG,
* FLOAT, DOUBLE, LONGDOUBLE,
* DATETIME, TIMEDELTA#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong,
* npy_float, npy_double, npy_longdouble,
* npy_datetime, npy_timedelta#
@chelseatroy
chelseatroy / multiplication.pyx
Created November 6, 2018 23:44
Naive Python to C with Cython
import numpy as np
def cython_multiplication(a,b):
product = np.ones(a.shape)
for row_index in np.arange(a.shape[0]):
for col_index in np.arange(a.shape[1]):
product[row_index][col_index] = a[row_index][col_index] * b[row_index][col_index]
return product
@chelseatroy
chelseatroy / main.py
Created November 6, 2018 23:45
Calls with Cython
import ctypes
import time
from _ctypes import Structure, POINTER, byref
from ctypes import c_int, c_float
import pyximport; pyximport.install()
import multiplication
import numpy as np
a = np.random.rand(2000,2000)
@chelseatroy
chelseatroy / multiplication.pyx
Last active November 8, 2018 04:42
Naive Cython Implementation
import numpy as np
def cython_multiplication(double[:,:] a, double[:,:] b, double[:,:] out, int x_dims, int y_dims):
cdef int i, j
for i in range(x_dims):
for j in range(y_dims):
out[i, j] = a[i,j] * b[i,j]
return np.asarray(out)