Skip to content

Instantly share code, notes, and snippets.

import matplotlib.pyplot as plt
from numpy.random import normal, uniform
import pandas as pd
import os
os.listdir()
data = pd.read_csv("DSI.csv")
data.head()
@nmolivo
nmolivo / 001-LOOCV-cross_val_score
Created November 28, 2017 15:31
Attempt to calculate an R^2 score for LOOCV using cross_val_score
scores = cross_val_score(LinearRegression(), Xr, yr, cv=397, scoring = "r2")
print("Cross-validated scores:", scores)
print("Average: ", scores.mean())
print("Variance: ", np.std(scores))
@nmolivo
nmolivo / 001-LOOCV-r2-MSE
Created November 28, 2017 15:35
My proposed solution to calculating metrics for LOOCV
loo = LeaveOneOut()
ytests = []
ypreds = []
for train_idx, test_idx in loo.split(Xr):
X_train, X_test = X_array[train_idx], X_array[test_idx] #requires arrays
y_train, y_test = y_array[train_idx], y_array[test_idx]
model = LinearRegression()
model.fit(X = X_train, y = y_train)
y_pred = model.predict(X_test)
@nmolivo
nmolivo / s3-upload
Created December 4, 2017 02:49
Storing images in an AWS S3 bucket from their URLs
mapping_dict ={}
for i, img_url in enumerate(image_list[0:10000]):
img_name = "img_%05d" % (i,)
mapping_dict[img_name] = img_url
if (img_url == np.nan) | (str(img_url) == "nan"):
continue
else:
# Uses the creds in ~/.aws/credentials
s3_image_filename = img_name
@nmolivo
nmolivo / rekognition-detect_labels
Last active December 4, 2017 03:17
AWS Rekognition: DetectLabels
# http://docs.aws.amazon.com/rekognition/latest/dg/get-started-exercise.html
fileName='img_00001'
bucket='bucket_name'
client=boto3.client('rekognition')
## ^^ we only need to do this code once for the following examples. I include it
## re-instated in case you want to check out different pics.
response = client.detect_labels(Image={'S3Object':{'Bucket':bucket,'Name':fileName}},MinConfidence=75)
response
@nmolivo
nmolivo / rekognition-detect_text
Last active December 4, 2017 03:23
AWS Rekognition: DetectText
fileName= 'img_00006'
bucket='bucket_name'
text_in_image = client.detect_text(Image={'S3Object':{'Bucket':bucket,'Name':fileName}})
text_in_image["TextDetections"]
@nmolivo
nmolivo / rekognition-recognize_celebrities
Last active December 4, 2017 03:43
AWS Rekognition: Recognize Celebrities
fileName= 'img_00012'
bucket='bucket_name'
celeb_detect = client.recognize_celebrities(Image={'S3Object':{'Bucket':bucket,'Name':fileName}})
celeb_detect["CelebrityFaces"]
@nmolivo
nmolivo / rekognition-for-loop
Last active December 4, 2017 04:48
AWS Rekognition Image Tagger
bucket_name = 'bucket_name'
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
images = [img.key for img in bucket.objects.all()] #fetches image names from your S3 bucket
client = boto3.client('rekognition')
results_wide = []
results_long = []
for img in images:
@nmolivo
nmolivo / shark_incident_activity.py
Created January 2, 2018 21:17
Shark Bite Activity Categorization
model_data['activity'] = model_data['Activity'].map({'Scuba Diving':'Diving', 'Scuba diving':'Diving','2 boats capsized': 'Disaster','Treading water': 'Swimming',
'Air Disaster': 'Disaster','Surfing': 'Surfing', 'Night bathing': 'Swimming', 'Snorkeling': 'Swimming','Measuring sharks': 'Handling',
'Swimming': 'Swimming', 'Surfing': 'Surfing', 'Kayaking / Fishing': 'Fishing','Body surfing': 'Surfing', 'Fishing': 'Fishing',
'Spearfishing': 'Spear Fishing','Swimming, poaching abalone': 'Diving', 'Wading': 'Shallow Water', 'Canoeing': 'Rowing',
'Feeding sharks': 'Handling', 'Paddle boarding':'Rowing','SUP':'Rowing','Body boarding':'Surfing', 'Touching a shark': 'Handling',
'Attempting to lasso a shark':'Handling','Surfing ':'Surfing','Tagging sharks':'Handling', 'Kakaying': 'Rowing','Washing hands':'Fishing',
'Grabbing shark for a selfie': 'Handling', 'Kayak fishin
@nmolivo
nmolivo / selenium-import
Created February 8, 2018 08:12
selenium-import
import os
import pandas as pd
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary