Guest User

Untitled

a guest
Nov 30th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. import psycopg2
  2. connection = psycopg2.connect("host=localhost port=5432 dbname=postgres user=kimishi password=********")
  3. connection.get_backend_pid()
  4.  
  5. import pandas as pd
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. import seaborn as sns
  9. import math
  10. from matplotlib.patches import Ellipse
  11. from scipy import stats
  12.  
  13. df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence>=0.5 and energy>=0.5;", con=connection, index_col='index' )
  14. df = df_pre[df_pre['artist_genres'].str.contains('pop')]
  15.  
  16. def corrfunc(x, y, **kws):
  17. r, _ = stats.spearmanr(x, y)
  18. ax = plt.gca()
  19. ax.axis('off')
  20. ellcolor = plt.cm.RdBu(0.5*(r+1))
  21. txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
  22. ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
  23. facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
  24. ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
  25. horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
  26.  
  27. d2 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
  28. 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
  29. columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
  30.  
  31. sns.set(font_scale=2)
  32. g = sns.PairGrid(d2)
  33. g = g.map_lower(sns.kdeplot, cmap='Blues_d')
  34. g = g.map_diag(sns.distplot, kde=False)
  35. g = g.map_upper(corrfunc)
  36. g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
  37. for ax in g.axes.flatten():
  38. for t in ax.get_xticklabels():
  39. _ = t.set(rotation=40)
  40. g.savefig('/Users/kimishi/Desktop/fig1_1_1.png')
  41.  
  42.  
  43. df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence<0.5 and energy>=0.5;", con=connection, index_col='index' )
  44. df = df_pre[df_pre['artist_genres'].str.contains('pop')]
  45.  
  46. def corrfunc(x, y, **kws):
  47. r, _ = stats.spearmanr(x, y)
  48. ax = plt.gca()
  49. ax.axis('off')
  50. ellcolor = plt.cm.RdBu(0.5*(r+1))
  51. txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
  52. ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
  53. facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
  54. ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
  55. horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
  56.  
  57. d3 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
  58. 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
  59. columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
  60.  
  61. sns.set(font_scale=2)
  62. g = sns.PairGrid(d3)
  63. g = g.map_lower(sns.kdeplot, cmap='Blues_d')
  64. g = g.map_diag(sns.distplot, kde=False)
  65. g = g.map_upper(corrfunc)
  66. g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
  67. for ax in g.axes.flatten():
  68. for t in ax.get_xticklabels():
  69. _ = t.set(rotation=40)
  70. g.savefig('/Users/kimishi/Desktop/fig1_1_2.png')
  71.  
  72.  
  73. df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence<0.5 and energy<0.5;", con=connection, index_col='index' )
  74. df = df_pre[df_pre['artist_genres'].str.contains('pop')]
  75.  
  76. def corrfunc(x, y, **kws):
  77. r, _ = stats.spearmanr(x, y)
  78. ax = plt.gca()
  79. ax.axis('off')
  80. ellcolor = plt.cm.RdBu(0.5*(r+1))
  81. txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
  82. ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
  83. facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
  84. ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
  85. horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
  86.  
  87. d4 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
  88. 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
  89. columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
  90.  
  91. sns.set(font_scale=2)
  92. g = sns.PairGrid(d4)
  93. g = g.map_lower(sns.kdeplot, cmap='Blues_d')
  94. g = g.map_diag(sns.distplot, kde=False)
  95. g = g.map_upper(corrfunc)
  96. g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
  97. for ax in g.axes.flatten():
  98. for t in ax.get_xticklabels():
  99. _ = t.set(rotation=40)
  100. g.savefig('/Users/kimishi/Desktop/fig1_1_3.png')
  101.  
  102.  
  103. df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence>=0.5 and energy<0.5;", con=connection, index_col='index' )
  104. df = df_pre[df_pre['artist_genres'].str.contains('pop')]
  105.  
  106. def corrfunc(x, y, **kws):
  107. r, _ = stats.spearmanr(x, y)
  108. ax = plt.gca()
  109. ax.axis('off')
  110. ellcolor = plt.cm.RdBu(0.5*(r+1))
  111. txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
  112. ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
  113. facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
  114. ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
  115. horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
  116.  
  117. d5 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
  118. 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
  119. columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
  120.  
  121. sns.set(font_scale=2)
  122. g = sns.PairGrid(d5)
  123. g = g.map_lower(sns.kdeplot, cmap='Blues_d')
  124. g = g.map_diag(sns.distplot, kde=False)
  125. g = g.map_upper(corrfunc)
  126. g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
  127. for ax in g.axes.flatten():
  128. for t in ax.get_xticklabels():
  129. _ = t.set(rotation=40)
  130. g.savefig('/Users/kimishi/Desktop/fig1_1_4.png')
Add Comment
Please, Sign In to add comment