In [1]:
#import pandas as pd


/Users/kelty/Desktop/Amazon-Review-Project/venv/lib/python2.7/site-packages/pandas/io/excel.py:626: UserWarning: Installed openpyxl is not supported at this time. Use >=1.6.1 and <2.0.0.
  .format(openpyxl_compat.start_ver, openpyxl_compat.stop_ver))

In [1]:
import numpy as np

In [29]:
import datetime as dt

In [3]:
import matplotlib.pyplot as plt

In [4]:
import MySQLdb

In [5]:
%matplotlib inline

In [6]:
def time_data_clean(time_data):
    rating = [0.0]*len(time_data)
    time = [0]*len(time_data)
    rating = [x[0] for x in time_data]
    time = [x[1] for x in time_data]
    return rating, time

In [7]:
db = MySQLdb.connect(host="localhost", user="root", db = "home_kitchen")

In [8]:
cursor = db.cursor()

In [9]:
tablename = 'electronics'

In [10]:
prod_id = ' B000KO0GY6'

In [11]:
sql = "Select RTime, RScore From " +tablename + " Where PID = " + '"' + prod_id +'";'

In [12]:
print sql


Select RTime, RScore From electronics Where PID = " B000KO0GY6";

In [13]:
cursor.execute(sql)


Out[13]:
427L

In [14]:
time_data = cursor.fetchall()

In [15]:
time_data = sorted(time_data)

In [74]:
#print time_data

In [75]:
#rating, time = time_data_clean(time_data)

In [76]:
#print rating

In [77]:
#print time

In [16]:
rating = zip(*time_data)[1]

In [17]:
time = zip(*time_data)[0]

In [30]:
dates=[dt.datetime.fromtimestamp(ts) for ts in time]

In [18]:
#print time

In [32]:
plt.scatter(dates, rating)


Out[32]:
<matplotlib.collections.PathCollection at 0x1061d8910>

In [20]:
avg = [0]*len(time)

In [21]:
limited_avg = [0]*len(time)

In [22]:
avg[0] = rating[0]

In [23]:
limited_avg[0] = rating[0]

In [24]:
for k in range(1, len(time)):
    avg[k]= np.mean(rating[:k])

In [25]:
for k in range(1, len(time)):
    if k<100:
        limited_avg[k]= np.mean(rating[:k])
    else:
        limited_avg[k]=np.mean(rating[k-100:k])

In [26]:
#print avg

In [33]:
plt.scatter(dates, avg)


Out[33]:
<matplotlib.collections.PathCollection at 0x106265350>

In [34]:
plt.scatter(dates, limited_avg)


Out[34]:
<matplotlib.collections.PathCollection at 0x1062ef390>

In [28]:
timemin = time[0]

In [29]:
mink = 0

In [30]:
for k in range(len(time)):
    if time[k]<timemin:
        timemin = time[k]
        mink = k

In [31]:
print mink


29

In [32]:
print rating[29]


5.0

In [33]:
print timemin


957225600

In [35]:
limited_avg_50 = limited_avg[50:]

In [ ]: