This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| R to python useful data wrangling snippets | |
| The dplyr package in R makes data wrangling significantly easier. | |
| The beauty of dplyr is that, by design, the options available are limited. | |
| Specifically, a set of key verbs form the core of the package. | |
| Using these verbs you can solve a wide range of data problems effectively in a shorter timeframe. | |
| Whilse transitioning to Python I have greatly missed the ease with which I can think through and solve problems using dplyr in R. | |
| The purpose of this document is to demonstrate how to execute the key dplyr verbs when manipulating data using Python (with the pandas package). | |
| dplyr is organised around six key verbs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A method for modifying only select off-diagonal items in a matrix | |
| # From "Thierry" and "Ben Bolker" | |
| # At http://stackoverflow.com/a/11759744/479554 | |
| # A sample matrix | |
| size <- 6 | |
| mat <- matrix(seq_len(size ^ 2), ncol = size) | |
| print(mat) | |
| # A companion matrix that indicates how "off" a diagonal is: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Using airquality dataset | |
| data <- airquality | |
| data[4:10,3] <- rep(NA,7) | |
| data[1:5,4] <- NA | |
| # Removing categorical variables | |
| data <- airquality[-c(5,6)] | |
| summary(data) | |
| #------------------------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import numpy | |
| from pandas import DataFrame | |
| from sklearn.feature_extraction.text import CountVectorizer | |
| from sklearn.naive_bayes import MultinomialNB | |
| from sklearn.pipeline import Pipeline | |
| from sklearn.cross_validation import KFold | |
| from sklearn.metrics import confusion_matrix, f1_score | |
| NEWLINE = '\n' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # encoding: utf-8 | |
| import tweepy #https://github.com/tweepy/tweepy | |
| import csv | |
| #Twitter API credentials | |
| consumer_key = "" | |
| consumer_secret = "" | |
| access_key = "" |