In [1]:
y = randn(1000)
plot(y)


Out[1]:
[<matplotlib.lines.Line2D at 0x106806950>]

In [116]:
cd data/citydata/


/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/citydata

In [117]:
ls


worldcitiespop.txt          worldcitiespop_LICENSE.txt

In [11]:
import pandas as pd

In [118]:
filename = 'worldcitiespop.txt'

In [119]:
data = pd.read_csv(filename)

In [15]:
plot(data.Longitude, data.Latitude, ',')


Out[15]:
[<matplotlib.lines.Line2D at 0x108f191d0>]

In [120]:
data.describe()


Out[120]:
Population Latitude Longitude
count 47980.000000 3173958.000000 3173958.000000
mean 47719.570634 27.188166 37.088860
std 302888.715626 21.952617 63.223020
min 7.000000 -54.933333 -179.983333
25% 3732.000000 11.633333 7.303176
50% 10779.000000 32.497222 35.280000
75% 27990.500000 43.716667 95.703542
max 31480498.000000 82.483333 180.000000

In [19]:
data.head()


Out[19]:
Country City AccentCity Region Population Latitude Longitude
0 ad aixas Aix�s 6 NaN 42.483333 1.466667
1 ad aixirivali Aixirivali 6 NaN 42.466667 1.500000
2 ad aixirivall Aixirivall 6 NaN 42.466667 1.500000
3 ad aixirvall Aixirvall 6 NaN 42.466667 1.500000
4 ad aixovall Aixovall 6 NaN 42.466667 1.483333

In [27]:
data[data.AccentCity=='Dikili']


Out[27]:
Country City AccentCity Region Population Latitude Longitude
2786267 tr dikili Dikili 25 NaN 39.381066 41.566456
2786268 tr dikili Dikili 35 NaN 39.071004 26.890170
2786269 tr dikili Dikili 44 NaN 39.050000 38.050000
2786270 tr dikili Dikili 58 NaN 39.385705 36.323177
2786271 tr dikili Dikili 60 NaN 40.350126 37.073293
2786272 tr dikili Dikili 63 NaN 36.984167 40.087222
2786273 tr dikili Dikili 81 NaN 37.362557 35.714004


In [28]:
cd fbdata


(bookmark:fbdata) -> /Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook
/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook

In [29]:
pwd


Out[29]:
u'/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook'

In [30]:
import networkx as nx

In [31]:
g = nx.read_edgelist('0.edges')

In [32]:
hist(g.degree().values(), bins=20)


Out[32]:
(array([81, 65, 44, 42, 20, 18, 16,  5,  4,  8,  5,  6,  2,  1,  4,  2,  4,
        2,  2,  2]),
 array([  1. ,   4.8,   8.6,  12.4,  16.2,  20. ,  23.8,  27.6,  31.4,
        35.2,  39. ,  42.8,  46.6,  50.4,  54.2,  58. ,  61.8,  65.6,
        69.4,  73.2,  77. ]),
 <a list of 20 Patch objects>)


In [33]:
x = linspace(-10, 10)
y = sin(x)

In [37]:
plot(x, sin(x), '-r', label='sinus')
plot(x, cos(x), '--g', label='cosinus')
xticks([-10, 0, 10])
yticks([-1, 0, 1])
ylim(-2, 2)
legend()
grid()




In [40]:
plot(randn(1000, 2))


Out[40]:
[<matplotlib.lines.Line2D at 0x1094dc1d0>,
 <matplotlib.lines.Line2D at 0x1094dcc90>]

In [41]:
line = _[0]

In [44]:
line.set_color('r') # doesnt work in notebook


In [45]:
x = linspace(0, 2*pi, 1000)
y = 1 + 2*cos(5*x)
subplot(1,2,1)
plot(x,y)
subplot(1,2,2, polar=True)
polar(x,y)


Out[45]:
[<matplotlib.lines.Line2D at 0x1097a5d50>]

images:


In [47]:
import urllib2

In [101]:
png = urllib2.urlopen('http://ipython.rossant.net/squirrel.png')

In [102]:
im = imread(png)

In [103]:
im.shape


Out[103]:
(300, 300, 3)


In [51]:
from PIL import Image

In [52]:
img = Image.fromarray((im*255).astype('uint8'))


In [104]:
imshow(im)


Out[104]:
<matplotlib.image.AxesImage at 0x10a9fa610>

In [54]:
imshow(array(img.rotate(45.)))


Out[54]:
<matplotlib.image.AxesImage at 0x10b2ee450>

Color quantization:


In [55]:
from scipy.cluster.vq import *

In [105]:
M = im[:,:,0].ravel()

In [106]:
centroids, _ = kmeans(M, 4)

In [58]:
qnt, _ = vq(M, centroids)

In [59]:
clustered = centroids[reshape(qnt, (300, 300))]

In [61]:
mymap = matplotlib.colors.ListedColormap([(0, .2, .3), (.85, .1, .13), (.44, .6, .6), (1., .9, .65)])

In [62]:
imshow(clustered, cmap=mymap)


Out[62]:
<matplotlib.image.AxesImage at 0x109f1ee50>

In [68]:
imsave('squirrelama.png', clustered, cmap=mymap)

Multichannel quantization:


In [111]:
Mred = transpose(im[:,:,0].ravel())
Mgreen = im[:,:,1].ravel().T
Mblue = im[:,:,2].ravel().T

In [110]:
Mred.shape


Out[110]:
(90000,)

In [88]:
BigM = concatenate((Mred, Mgreen, Mblue), axis=1)

In [76]:
Mcentoids, _ = kmeans(M, 5)

In [77]:
Mqnt, _ = vq(BigM, Mcentoids)

In [79]:
Mclustered = Mcentoids[reshape(Mqnt, (300, 300, 3))]

In [81]:
Mqnt.shape


Out[81]:
(270000,)

In [82]:
Mcentoids.shape


Out[82]:
(5,)

In [89]:
BigM.shape


Out[89]:
(270000,)

laters



In [121]:
locations = data[['Longitude','Latitude']].as_matrix()

In [122]:
population = data.Population

In [124]:
from mpl_toolkits.basemap import Basemap


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-124-5e6824321d57> in <module>()
----> 1 from mpl_toolkits.basemap import Basemap

ImportError: No module named basemap

BASEMAP HERE



In [125]:
from mpl_toolkits.mplot3d import Axes3D

In [127]:
X = linspace(-5, 5)
Y = X
X, Y = meshgrid(X, Y)

R = sqrt(X**2 + Y**2)
Z = sin(R)

In [130]:
ax = gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=mpl.cm.coolwarm, linewidth =0)




In [1]:
# gui qt (this section works in console mode, but crashes the kernel in the notebook)

In [2]:
from PySide import QtGui

In [3]:
class SideApp(QtGui.QWidget):
    def __init__(self):
        super(SideApp, self).__init__()
        
        self.button = QtGui.QPushButton('Click me', self)
        self.button.clicked.connect(self.clicked)
        
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.button)
        self.setLayout(vbox)
        self.show()
        
    def clicked(self):
        msg = QtGui.QMessageBox(self)
        msg.setText('Hello world!')
        msg.show()

In [1]:
# window = SideApp()

In [ ]: