Here is how we can run a single query across multiple regions.
-- Create a table with a single id column
CREATE EXTERNAL TABLE ids(id bigint)
PARTITIONED BY (region string)
ROW FORMAT DELIMITED
-- Add two partitions, use two buckets from two regions
ALTER TABLE ids ADD PARTITION (region='us')
ALTER TABLE ids ADD PARTITION (region='eu')
-- Count the distinct ids, in all regions
SELECT COUNT(DISTINCT id)
GluonNLP provides Pre-trained models for common NLP tasks. It has carefully designed APIs that greatly reduce the implementation complexity.
import mxnet as mx
import gluonnlp as nlp
glove = nlp.embedding.create('glove', source='glove.6B.50d')
def cos_similarity(embedding, word1, word2):
vec1, vec2 = embedding[word1], embedding[word2]
return mx.nd.dot(vec1, vec2) / (vec1.norm() * vec2.norm())
cos_similarity(glove, 'baby', 'infant').asnumpy()
This will load wikipedia article words as a python list.
train = nlp.data.WikiText2(segment='train')
Labels: machine_learning, nlp, python
Usually I download a file and extract using 2 linux commands like this...
! wget https://github.com/d6t/d6tstack/raw/master/test-data.zip
! unzip -o test-data.zip
But it can also be done using python code as shown below!
cfg_fname_sample = "test-data.zip"
"https://github.com/d6t/d6tstack/raw/master/" + cfg_fname_sample, cfg_fname_sample
zip_ref = zipfile.ZipFile(cfg_fname_sample, "r")
Labels: pandas, python, usability
YOLO helps detect objects in an image using a pre-trained model.
1) Install darknet
git clone https://github.com/pjreddie/darknet
2) download the pre-trained weight file
3) Run the detector
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
You will see the output something like this...
An image called predictions.jpg is saved in current directory.
Labels: machine_learning, tensorflow
Athena and redshift both are great database. But there are times when we need a bridge to connect them. For e.g. when we need to join a redshift table with Athena. Redshift spectrum can be used in such cases.
1) Create an IAM role called "RedshiftCopyUnload" using the cloudformation template shown in this example:
2) Create database using the Role created in the first step:
create external schema spectrum_schema from data catalog
create external database if not exists;
3) Create external table:
create external table spectrum_schema.testme (number bigint)
row format delimited fields terminated by '|'
stored as textfile
4) Create internal table native to redshift using select query on external table:
create table mypincode as select * from spectrum_schema.testme limit 10;
Labels: athena, redshift
The following code will initiate a Linux instance of type m3.medium using spot pricing and associate it to IP address 22.214.171.124 Make sure to use your own elastic IP address and key. Do not forget to change the access_key and secret_key parameters.
This will return the instance id and the ssh command that you can use to connect to your instance. The output will look something like...
job instance id: i-029a926e68118d089
ssh -i dec15a.pem email@example.com
You can list all instances along with their details like launch time, image_id and save the results as pandas dataframe using showEc2 method like this...
Now "df" is a pandas dataframe object. You can sort or groupby the instances just like an excel sheet.
You can delete the instance by providing the instance ID that was generated in the first step using deleteEc2 method.
You can also use cloudformation template for this purpose. Visit the following link and look for "Linux EC2 Instance on SPOT" section.
Click on "Launch Stack" button. It is nothing but GUI for the python code mentioned above. You will simply have to submit a form for the methods like key and IP address.
Labels: aws, aws_cloudformation, boto, usability
Save the following shell script and run it with your github username to get the list of all starred repositories.
sh myscript.sh shantanuo > starred.txt
You will need to install jq (yum install jq)
STARS=$(curl -sI https://api.github.com/users/$USER/starred?per_page=1|egrep '^link'|egrep -o 'page=[0-9]+'|tail -1|cut -c6-)
for PAGE in `seq $PAGES`; do
curl -sH "Accept: application/vnd.github.v3.star+json" "https://api.github.com/users/$USER/starred?per_page=100&page=$PAGE"|jq -r '.|[.starred_at,.repo.full_name]|@tsv'
Labels: git, shell script
Some basic numpy methods everyone should be aware of.
import numpy as np
mylist = [[100, 2, 3], [4, 5786, 6]]
a = np.array(mylist)
np.append(a, [10, 11, 12])
b = np.array([, ])
np.append(a, b, axis=1)
np.append(a, [[50, 60, 70]], axis=0)
np.insert(a, 2, [1, 2, 34])
np.where(a == 3)
np.delete(a, 1, axis=0)
np.delete(a, 1, axis=1)
Labels: pandas, python
There are times when I find a great article or web page but don't have time to read. I use EmailThis service to save text & images from a website to my email inbox. The concept is very simple. Drag and drop a bookmarklet to the bookmark toolbar and click on it to send the current web-page to your inbox!
But I did not like the premium ads and partial content that the site sends. So I built my own serverless API to get exactly the same functionality using mailgun and Amazon Web Services.
Once you register with mailgun, you will get a URL and API-Key that you need to copy-paste to notepad. You will need to provide this information when you launch the cloudformation template by clicking on this link.
Once the resources are created, you can see a URL in output section something like this...
Right click on any bookmark and then copy-paste the above link. Make sure that you have changed the URL and email address to your own. Now click on this bookmarklet while you are on an important web page that you need to send to your inbox. Enjoy!
Labels: api_gateway, aws, aws_lambda, usability