Skip to content

Instantly share code, notes, and snippets.

@karishmadudani
karishmadudani / gist:cd31bdc23e1daaa30b6fa202fbb3691f
Created June 3, 2018 02:40
Converting dates to continuous values
import pandas as pd
import time
from datetime import datetime, timedelta
import datetime as dt
from datetime import date
data = {'col': ['id','Number of Days']}
ColumnList=pd.DataFrame(data=data)
today = date(2018, 6, 2)
@karishmadudani
karishmadudani / gist:5d9f2534e36049a8df4f2b48bd528047
Last active June 3, 2018 01:24
Normalize Variable in Python - 1
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
#Import File
your_dataset= pd.read_excel("Path\\FileName.xlsx")
@karishmadudani
karishmadudani / gist:d6d24fe1acbc37531d8ab57aac17d31b
Last active June 3, 2018 01:43
Normalize Variables in Python
#Normalize variables between 0 & 1
VariableList = ['Number of Guests','Number of Bedrooms','Latitude','Longitude']
normVariableName = ['No_of_Guests_Norm','No_of_beds_norm','latitude_norm','longitude_norm']
scaler = MinMaxScaler()
for i in range(0,len(your_dataset)):
for j in range(0,len(VariableList)):
temp = your_dataset[VariableList[j]]
lat = X_test[:,0]
lat = lat.reshape((len(lat),1))
lon = X_test[:,1]
lon = lon.reshape((len(lon),1))
time = X_test[:,2]
time = time.reshape((len(time),1))
print y_test.shape
#split the data
X = RawData[[' Latitude (deg)', ' Longitude (deg)', 'time_secs']]
X = X.values.reshape((len(X),3))
y = RawData[' Vehicle speed (MPH)']
y = y.values.reshape((len(y),1))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.001, random_state=42)
#Create the model and predict
nn = 40
#Create a function to convert time variable to seconds
def hms_to_seconds(t):
h, m, s = [float(j) for j in t.split(':')]
return 3600*h + 60*m + s
#Read the raw files and extract the relevant variables
RawData = pd.DataFrame()
for file in [f for f in os.listdir('C:\Users\lenovo\Desktop\Karishma\Project 2017\OBD\RawData') if f.endswith('.csv')]:
filename = 'C:\Users\lenovo\Desktop\Karishma\Project 2017\OBD\RawData\\' + file
data = pd.read_csv(filename, skiprows=2)
@karishmadudani
karishmadudani / ImportLib.py
Created November 5, 2017 02:55
Predicting Vehicle Speed
import csv
import pandas as pd
import datetime as dt
from os import listdir
import os
import numpy as np
from numpy import mean, sqrt, square, arange
import matplotlib.pyplot as plt
from sklearn import neighbors
from sklearn.model_selection import train_test_split
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Join all the text from the 1000 tweets
Hashtag_Combined = " ".join(Htag_df['Hashtag'].values.astype(str))
no_millennials = " ".join([word for word in Hashtag_Combined.split()
if word != 'millennials'
and word != 'Millennials'
and word != 'Boomers'
and word != 'GenX'
])
Htag_df = pd.DataFrame()
j = 0
for tweet in range(0,len(results)):
hashtag = results[tweet].entities.get('hashtags')
for i in range(0,len(hashtag)):
Htag = hashtag[i]['text']
Htag_df.set_value(j, 'Hashtag',Htag)
j = j+1