API and Market Basket Analysis

July 1, 2015

I was considering waiting until I’m done before posting about this project, but instead I thought I’d post my progress and plans while I think about the next steps. I posted earlier about using the UsesThis API to retrieve data about what other software people that use X software also use. I thought I was going to have to code a workaround for people that didn’t have any software listed in their interviews, but when I tweeted about it, Daniel from @usesthis replied that it was actually a bug and fixed it immediately! It makes it even more fun to develop since he is excited about me using his API! @BecomingDataSci: YES! It’s *awesome*. — The Setup (@usesthis) June 19, 2015 After seeing those results, I thought it would be interesting (and educational) to learn how to do a Market Basket Analysis on the software data. Market Basket Analysis is a data mining technique where you can find out what items are usually found in combination, such as groceries people typically buy together. For instance, do people often buy cereal and milk together? If you buy taco shells and ground beef, are you likely to also buy shredded cheese? This type of analysis allows brick and mortar retailers to decide how to position items in a store. Maybe they will put items regularly purchased together closer together to make the trip more convenient. Maybe they will place coupons or advertisements for shredded cheese next to the taco shells. Or maybe they will place the items further apart so you have to pass more goods on the way from one item to the other and are more likely to pick up something you otherwise wouldn’t have. Online retailers can use this type of analysis to recommend products to increase the size of your purchase. “Other customers that added item X to their shopping cart also purchased items Y and Z.” Because I had this interesting set of software usage from The Setup’s interviews, I wanted to analyze what products frequently go together. I searched Google for ‘Market Basket Analysis python,’ and it led me to this tweet by @clayheaton: I just wrote a simple Market Basket analysis module for Python. #analytics https://t.co/aVf58zcHJa — Clay Heaton (@clayheaton) April 4, 2014 I followed that link and checked out the code on github and it seemed to make sense, so I put the results of my usesthis API request into a format it could use. I did a test with the data from 5 interviews, and it ran. Then I tried 50 interviews, and the results showed that people that use Photoshop were likely to also use Illustrator, and vice-versa. It appeared to be working! However, I then hit a snag. I tried to run it with all of the software data, and it ran for a long time then crashed when my computer ran out of memory. Since it’s building an association graph with an edge for every rule (combination of software used), with up to two pieces of software per “side” of the rule (such as “people that have Photoshop and Illustrator also have a Mac”), you can imagine the graph gets pretty big when you have over 10,000 user-software combinations. I tweeted about this and Clay suggested modifying his code to store the items in a sparse matrix instead of a graph, and I agree that that sounds like a good approach, so that’s my next step on this project. I’ll post again when I’m...

Read More

The Setup (usesthis.com) API

June 10, 2015

There’s a really interesting site usesthis.com AKA “The Setup” which interviews people and lists all of the gear that they use, including software. I found out that they have an API, (documented here) and I wanted to use my new API skills in Python to test it out! This one returns JSON unlike the NPR API that returned XML. Basically what I did is use the list API to return all of the interviews of people that use Python, then used the interviews API to return each of those people’s lists of gear. That way, i could tally up the most-frequently-used software (other than Python) used by the interviewed Python users! Here’s my code in HTML IPython notebook form. I haven’t had a chance to practice visualizations yet, so please point me to any resources that will help me make the horizontal bar chart prettier! UsesThis API – Software that Python users use Preview of the ugly chart: Update 6/18/15: What about other software? I added an input so the user can type in any software title. For the output saved below, I typed in “Android” at the prompt. Here it is on nbviewer. You can use the download button in the upper right corner to download it and run it on your local IPython installation to try it out yourself!...

Read More

May 2015 #SoDS Storify

June 9, 2015

I wanted to capture the participation in the #SoDS (Summer of Data Science) hashtag somehow, so I decided to create a monthly Storify to keep up with all of your great tweets! [View the story “#SoDS May 2015” on...

Read More

Monday Silliness

June 8, 2015

OK so I’m tired enough to be a little silly right now, and I think I’m finally feeling bold enough to share my parody songs inspired by these twitter exchanges: @matt_slotnick @dpatil haha i've said b4 that O'Jays "for the love of money" but w/"data data data…DATA" runs thru my head b4 projects :) — Data Science Renee (@BecomingDataSci) May 14, 2015 @BecomingDataSci @lukedones @matt_slotnick possible theme song, alter @bryanadams "Summer of 69" to "Summer of Data Science" — Ryan Swanstrom (@ryanswanstrom) May 27, 2015 @ryanswanstrom @becomingdatasci @matt_slotnick '69' -> "datasci" — lukemeister (@lukedones) May 27, 2015 So here goes…. The Summer of Data Sci’ To the tune of… Lyrics: I took my first course in machine learning Found it all for free online Coded ’til my fingers bled Was the Summer of Data Sci Me and some twitter friends Got together and we tried real hard To understand how data covaried (linear dependence between two vars) Oh when I look back now That summer was the best endeavor And if I had the choice I wouldn’t change it whatsoever Those were the best days of my life Ain’t no use in complainin’ When you got learning to do Spent my evenings staring at my laptop And that’s when I downloaded you You were free and open source I thought that you would last forever And then I faced a choice I had to port you now or never Python 2.7 to 3.5 Oh yeah Back in the Summer of Data Sci Ohhh We were consumin’ APIs Which were RESTful and RESTless The response was undefined… I guess nothin’ can work forever, 410, gone And now the times are changin’ Look at everything that’s come and gone Sometimes I import that old library Documented it in IPython You were free and open source Analyzed my datasets on weather Your model helped me understand Bayesian time series forecasts better Those were the best days of my life Oh yeah Back in the Summer of Data Sci Uh-huh It was the Summer of Data Sci, oh yeah Me and my Summer of Data Sci, oh and… For the Love of Data To the tune of… Lyrics: Data, data, data, data. DATA. [6x] Some people got to have it Some people really need it Listen to me y’all, make things make things, make things rad things with it You wanna do things, do things Do things, good things with it Talk about raw data, data Talk about raw data Data files, y’all, c’mon now For the love of data People will knowledge-discover For the love of data People will track their own numbers For the love of data People can’t even press delete Because they never know which algorithm just might need it For that clean, learned-by-machine, forecasting almighty model (cross-validated) For the love of data People will train decision trees For the love of data People develop their own techniques For the love of data A woman will sell her precious time For a small CSV, it contains a lot of weights Call it clean, learned-by-machine, forecasting almighty model (talk about talk about stats) I know that data’s generated by all people Internet of Things, some evil Give me a sample, brother can you data mine? Data can drive some people out of their minds Data data data no good no good don’t sell your soul for data Data data data AI AI deep learning will I know that data’s generated by all people Internet of Things, some evil Give me a sample, brother can you data mine? Data can drive some people out of their minds Data data data Got to have it, I really need it Data data data Give it up, give it up, give it up, yeah. Data data data Gotta have it Some people really need it Give me give me give me raw data Data data data I need I need Give me give me give me How many strings are in this array Don’t let don’t let don’t let data rule ya How many neighbors optimize this k Don’t let don’t let don’t let don’t let data fool ya yeah yeah yeah Got to train it, then really test it Save your code. Save your code. Let the feeds stream stream stream stream stream. People, don’t let data, don’t let data change you almighty model It will keep on changing, changing up your mind I’m tellin’...

Read More

IPython, Requests, lxml, and the NPR API

June 7, 2015

Last week, I decided to learn how to use python to get data from an API. I started with the Codecademy “Introduction to APIs in Python” course, which got me oriented to how requests work, and in the subsequent NPR API lesson, specifically how the NPR stories API works. Certain parts of the course assumed you knew more python than you had learned in the course, so heads-up that there are places you will probably have to google for help since the hints aren’t always related to what you’re stuck on. The course isn’t really a requirement for learning this stuff (and I thought it could use a lot of improvement), but it does give you a guided walk-through, which is nice when you are totally new to a topic. Then, I tweeted about my experience, and got 2 responses encouraging me to use the requests library instead of urllib that codecademy used. @BecomingDataSci the urllib api is terrible. You should take a look at http://t.co/CzIPob2tBV — Daniel Moisset (@dmoisset) June 1, 2015 @dmoisset @BecomingDataSci 2nding using of requests over urllib; esp. with HTTPS, requests tends to do saner things (e.g., cert validation) — Cheng H. Lee (@chenghlee) June 1, 2015 I decided to redo what I had learned from scratch, but using requests. I also wanted to learn how to use IPython, so I used an IPython notebook to play around with the code. Below is the HTML export of my IPython notebook, with comments explaining what I was doing. I’m sure there are better ways to do what I did (feel free to comment with suggestions!), but this was my first time doing any of this without any guidance, so I don’t mind posting it even if it’s a little ugly :) I definitely spent a lot of time understanding the hierarchy of the NPR XML and how to loop through it and display it. If you have done something similar in a more elegant way, please point me to your code! Here are the main resources I used to learn how to do what is in the code: python requests library documentation NPR API documentation python lxml library documentation iPython videos I also wanted to mention that there are a lot of frustrations you can run up against when you’re a python beginner. I was having a lot of problems with seemingly basic stuff (like installing packages with pip) and it took a couple hours of googling and asking someone for help to figure out there was a problem with my path environment variables in windows. I’ll post about that another time, but I just wanted to 1) encourage people not to give up if you get stuck on something that seems to be so basic that most “intro” articles don’t even cover it, and 2) encourage people writing intro articles to make some suggestions about what could go wrong and how to problem-solve. Here’s one example: When I tried to export my IPython notebook to HTML, it gave me a 500 server error saying I needed python packages I didn’t already have. After I installed the first, it told me I needed pandoc, so I installed that as well, but it kept giving me the same error. It turns out that you have to run IPython Notebook as an Administrator in Windows in order to get the HTML export to work properly, but the error message didn’t indicate that at all. This is the kind of frustration that may make beginners think they’re not “getting it” and give up, when it fact it’s something outside the scope of what you’re learning. Python seems to require a lot of this sort of problem-solving. (Note: on my other laptop, I installed python and the scipy stack using Anaconda, and have had a lot fewer issues like this.) Without further ado, here’s my iPython notebook! (I’m having issues making it look readable while embedded in wordpress, so click the link to view in a new tab for now, and I’ll fix for viewing later!) Renee’s 1st IPython Notebook (NPR API using requests and lxml) Here’s the actual ipynb file if you have IPython installed and want to run it yourself: First Python API Usage** **NOTE: WordPress wouldn’t let me upload it with the IPython notebook extension for security reasons, so after you download it, change the “.txt” extension to...

Read More