Advertisement
Guest User

Untitled

a guest
Nov 28th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.23 KB | None | 0 0
  1. import psycopg2
  2. import numpy as np
  3.  
  4. connection = psycopg2.connect(host = 'imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com',
  5.                        database = 'dvdrental',
  6.                        user = 'imperial',
  7.                        password = 'imperial')
  8.  
  9. cursor = connection.cursor()
  10.  
  11. sql = 'SELECT * FROM film'
  12.  
  13. cursor.execute(sql)
  14.  
  15. cursor.fetchone()
  16.  
  17. cursor.fetchall()
  18.  
  19. films = cursor.fetchall()
  20.  
  21. films
  22.  
  23. films[0][1]
  24.  
  25. cursor.close()
  26. connection.close()
  27.  
  28. cursor.execute('CREATE DATABASE dogs')
  29.  
  30. # Dogs
  31.  
  32. connection = psycopg2.connect(host = 'imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com',
  33.                        database = 'dogs',
  34.                        user = 'imperial',
  35.                        password = 'imperial')
  36.  
  37. cursor = connection.cursor()
  38.  
  39. sql = "INSERT INTO dogs VALUES (1, 'rover', 'Alsatian', 9)"
  40. cursor.execute(sql)
  41.  
  42. connection.commit()
  43.  
  44. sql = "INSERT INTO dogs VALUES (2, 'Fido', 'Great Dane', 9)"
  45. cursor.execute(sql)
  46.  
  47. # Movies
  48.  
  49. connection = psycopg2.connect(host = 'imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com',
  50.                        database = 'movies',
  51.                        user = 'imperial',
  52.                        password = 'imperial')
  53. cursor = connection.cursor()
  54.  
  55. sql = "SELECT budget, gross FROM movie "
  56. cursor.execute(sql)
  57. budget_and_gross = cursor.fetchall()
  58.  
  59. connection.rollback()
  60.  
  61. budget_and_gross
  62.  
  63.  
  64.  
  65.  
  66. budgets = [x[0] for x in budget_and_gross]
  67. grosses = [x[1] for x in budget_and_gross]
  68.  
  69. [np.log(x) for x in budgets]
  70.  
  71. import matplotlib.pyplot as plt
  72. %matplotlib inline
  73.  
  74. plt.scatter(budgets, grosses)
  75. plt.xlabel('Budget')
  76. plt.ylabel('Gross')
  77.  
  78. import numpy
  79.  
  80. np.log(4)
  81.  
  82. np.log
  83.  
  84. type(grosses[0])
  85.  
  86. np.log(budgets[0])
  87.  
  88. log_budgets = []
  89. for x in budgets:
  90.   if x is not None:
  91.     log_budgets.append(np.log(x))
  92.   else:      
  93.     log_budgets.append(0)
  94.  
  95. log_grosses = []
  96. for x in grosses:
  97.   if x is not None:
  98.     log_grosses.append(np.log(x))
  99.   else:      
  100.     log_grosses.append(0)
  101.  
  102. %pdb
  103.  
  104. np.log(None)
  105.  
  106. plt.scatter(log_budgets, log_grosses)
  107. plt.xlabel('Budget')
  108. plt.ylabel('Gross')
  109.  
  110. plt.plot(grosses, 'x')
  111.  
  112. sql = "SELECT budget, gross FROM movie ORDER BY title_year "
  113. cursor.execute(sql)
  114. budget_and_gross_by_year = cursor.fetchall()
  115.  
  116. budget_and_gross_by_year
  117.  
  118. %pdb
  119.  
  120. plt.plot([x[1] for x in budget_and_gross_by_year])
  121.  
  122.  
  123.  
  124. NOTEBOOK 2
  125.  
  126. #Template
  127. import numpy as np
  128. import scipy
  129. import matplotlib.pyplot as plt
  130.  
  131. from pdb import set_trace
  132. from IPython.display import Image
  133. from time import sleep
  134. from matplotlib.image import imread
  135.  
  136. %matplotlib inline
  137. %load_ext autoreload
  138. %autoreload 2
  139.  
  140. from os import listdir
  141. from os.path import join
  142.  
  143. import pandas
  144. import psycopg2
  145.  
  146. #conn_str = "postgres://imperial:imperial@imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com/dvdrental"
  147. conn_str= "host=imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com user=imperial password=imperial dbname=dvdrental"
  148. conn = psycopg2.connect(conn_str)
  149.  
  150. conn
  151.  
  152. import pandas as pd
  153.  
  154. df = pd.read_sql('select * from film', con=conn)
  155.  
  156.  
  157.  
  158. df
  159.  
  160. df = pd.read_sql('select * from actor', con=conn)
  161.  
  162.  
  163. df
  164.  
  165. df.loc[199].first_name
  166.  
  167. df.loc[189,'last_name']
  168.  
  169. years =
  170.  
  171. df['replacement_cost']
  172.  
  173. df
  174.  
  175. # Movies analysis
  176.  
  177. conn_str= "host=imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com user=imperial password=imperial dbname=movies"
  178. conn = psycopg2.connect(conn_str)
  179.  
  180. import pandas as pd
  181.  
  182. df = pd.read_sql('SELECT * FROM movie', con=conn)
  183.  
  184.  
  185. df
  186.  
  187. query ="""
  188. SELECT title_year, AVG(gross) AS mean_gross
  189. FROM movie
  190. WHERE gross IS NOT NULL AND title_year IS NOT NULL
  191. GROUP BY title_year
  192. ORDER BY title_year
  193. """
  194.  
  195. df = pd.read_sql(query, con=conn)
  196.  
  197. df
  198.  
  199. print query
  200.  
  201. y
  202.  
  203. x = df['title_year']
  204. y = df['mean_gross']
  205.  
  206. plt.plot(x,y)
  207.  
  208. plt.plot(y)
  209.  
  210. query ="""
  211. SELECT budget, gross
  212. FROM movie
  213. WHERE budget < 10000000
  214. AND gross < 10000000
  215.  
  216. """
  217.  
  218. df = pd.read_sql(query, con=conn)
  219.  
  220. df
  221.  
  222. x = df['budget']
  223. y = df['gross']
  224.  
  225. plt.scatter(x,y)
  226. plt.xlabel('Budget')
  227. plt.ylabel('Gross')
  228.  
  229. query ="""
  230. SELECT imdb_score, cast_total_facebook_likes
  231. FROM movie
  232. WHERE num_user_for_reviews > 1000
  233. AND cast_total_facebook_likes < 100000
  234.  
  235. """
  236.  
  237. df = pd.read_sql(query, con=conn)
  238.  
  239. df
  240.  
  241. x = df['imdb_score']
  242. y = df['cast_total_facebook_likes']
  243.  
  244. plt.scatter(x,y)
  245. plt.xlabel('IMDB score')
  246. plt.ylabel('Cast total Facebook likes')
  247.  
  248. y = [np.log(p) for p in y]
  249.  
  250. x = [np.log(p) for p in x]
  251.  
  252. plt.hist(y)
  253.  
  254. y
  255.  
  256. x
  257.  
  258. plt.hist(x)
  259.  
  260.  
  261. NOTEBOOK 3
  262.  
  263. import sqlalchemy
  264.  
  265. from sqlalchemy import create_engine, inspect
  266.  
  267. db_uri = 'postgres://imperial:imperial@imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com/dvdrental'
  268.  
  269. engine = create_engine(db_uri)
  270.  
  271. engine
  272.  
  273. inspector = inspect(engine)
  274.  
  275. print(inspector.get_table_names())
  276.  
  277. result = engine.execute('SELECT * FROM film')
  278.  
  279. result
  280.  
  281. for r in result:
  282.     print(r.description)
  283.  
  284.  
  285. fetched_result = engine.execute('SELECT * FROM film').fetchall()
  286.  
  287. fetched_result[0].description
  288.  
  289. fetched_result[1].title
  290.  
  291. # Models - dogs
  292.  
  293. engine = create_engine('postgres://imperial:imperial@imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com/dogs')
  294.  
  295. engine
  296.  
  297. from sqlalchemy.orm import sessionmaker
  298. Session = sessionmaker()
  299. Session.configure(bind=engine)
  300. session = Session()
  301.  
  302. session
  303.  
  304. class Dog(Base):
  305.   __tablename__ = 'dogs'
  306.   id = Column(Integer, primary_key=True)
  307.   name = Column(String)
  308.   breed = Column(String)
  309.   owner_id = Column(Integer)
  310.  
  311.   def __repr__(self):
  312.     return self.name
  313.  
  314. dogs = session.query(Dog).all()
  315.  
  316. dogs
  317.  
  318. new_dog = Dog(id=3, name='Bonzo', breed='Labrador', owner_id=1)
  319.  
  320. new_dog
  321.  
  322. session.add(new_dog)
  323.  
  324. session.new
  325.  
  326. session.commit()
  327.  
  328. class Owner(Base):
  329.   __tablename__ = "owners"
  330.   id = Column(Integer, primary_key=True)
  331.   name = Column(String)
  332.  
  333.   def __repr__(self):
  334.     return self.name
  335.  
  336. class Dog(Base):
  337.   __tablename__ = 'dogs'
  338.   id = Column(Integer, primary_key=True)
  339.   name = Column(String)
  340.   breed = Column(String)
  341.   owner_id = Column(Integer, ForeignKey('owners.id'))
  342.   owner = relationship('Owner', back_populates='dogs')
  343.  
  344.   def __repr__(self):
  345.     return self.name
  346.  
  347. class Owner(Base):
  348.   __tablename__ = "owners"
  349.   id = Column(Integer, primary_key=True)
  350.   name = Column(String)
  351.   dogs = relationship('Dog', back_populates='owner')
  352.  
  353.   def __repr__(self):
  354.     return self.name
  355.  
  356. owner = session.query(Owner).first()
  357.  
  358. owner
  359.  
  360. owner.dogs
  361.  
  362. session.rollback()
  363.  
  364. bonzo = session.query(Dog).get(3)
  365.  
  366. bonzo
  367.  
  368. bonzo.owner
  369.  
  370. # Models - dvdrental
  371.  
  372. import sqlalchemy
  373. from sqlalchemy import create_engine
  374.  
  375. engine = create_engine('postgres://imperial:imperial@imperial.ckp3dl3vzxoh.eu-west-2.rds.amazonaws.com/dvdrental')
  376.  
  377. engine
  378.  
  379. from sqlalchemy.ext.declarative import declarative_base
  380.  
  381. Base = declarative_base()
  382.  
  383. from sqlalchemy import Column, Integer, String
  384.  
  385. class Film(Base):
  386.   __tablename__ = 'film'
  387.   film_id = Column(Integer, primary_key=True)
  388.   title = Column(String)
  389.  
  390.   def __repr__(self):
  391.     return self.title
  392.  
  393. from sqlalchemy.orm import sessionmaker
  394.  
  395. Session = sessionmaker()
  396.  
  397. Session.configure(bind=engine)
  398.  
  399. session = Session()
  400.  
  401. films = session.query(Film).all()
  402.  
  403. session.rollback()
  404.  
  405. films
  406.  
  407. new_film = Film(film_id = 9999, title="First Light")
  408.  
  409. session.add(new_film)
  410.  
  411. session.dirty
  412.  
  413. session.new
  414.  
  415. session.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement