Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 3.2.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
In [1]: d={
...: 'Name':'Angela Merkel',
...: 'Country':'Germany',
...: 'Profession':'Chancelor',
...: 'Age':60
...: }
In [2]: type(d)
Out[2]: dict
In [3]: print d['Name'],d['Age']
Angela Merkel 60
In [4]: d.keys()
Out[4]: ['Country', 'Age', 'Profession', 'Name']
In [5]: d.values()
Out[5]: ['Germany', 60, 'Chancelor', 'Angela Merkel']
In [6]: d.items()
Out[6]:
[('Country', 'Germany'),
('Age', 60),
('Profession', 'Chancelor'),
('Name', 'Angela Merkel')]
In [7]: birthday=True
In [8]: if birthday is
File "<ipython-input-8-cdd6aa25a1a1>", line 1
if birthday is
^
SyntaxError: invalid syntax
In [9]: if birthday is True:
...: d['Age']+=1;
...:
In [10]: print d['Age']
61
In [11]: for item in d.iteritems():
...: print item
...:
('Country', 'Germany')
('Age', 61)
('Profession', 'Chancelor')
('Name', 'Angela Merkel')
In [12]: for value in d.itervalues():
...: print type(value)
...:
<type 'str'>
<type 'int'>
<type 'str'>
<type 'str'>
In [13]: s=set(['u','d','ud','du','d','du'])
...: s
...:
Out[13]: {'d', 'du', 'u', 'ud'}
In [14]: t=set(['d','dd','uu','u'])
In [15]: s.union(t)
Out[15]: {'d', 'dd', 'du', 'u', 'ud', 'uu'}
In [16]: s.intersection(t)
Out[16]: {'d', 'u'}
In [17]: s.difference(t)
Out[17]: {'du', 'ud'}
In [18]: t.difference(s)
Out[18]: {'dd', 'uu'}
In [19]: s.symmetric_difference(t)
Out[19]: {'dd', 'du', 'ud', 'uu'}
In [20]: from random import randint
...: l=[randint(0,10) for i in range(1000)]
...: len(l)
...:
Out[20]: 1000
In [21]: l[:20]
Out[21]: [2, 7, 9, 9, 7, 1, 1, 1, 9, 9, 7, 5, 9, 7, 4, 9, 9, 8, 8, 6]
In [22]: s=set(l)
...: s
...:
Out[22]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
In [23]: v=[0.5,0.75,1.0,1.5,2.0]
In [24]: m=[v,v,v]
...: m
...:
Out[24]:
[[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0]]
In [25]: m[1]
Out[25]: [0.5, 0.75, 1.0, 1.5, 2.0]
In [26]: m[1][0]
Out[26]: 0.5
In [27]: v1=[0.5,1.5]
...: v2=[1,2]
...: m=[v1,v2]
...: c=[m,m]
...: c
...:
Out[27]: [[[0.5, 1.5], [1, 2]], [[0.5, 1.5], [1, 2]]]
In [28]: c[1][1][0]
Out[28]: 1
In [29]: v=[0.5,0.75,1.0,1.5,2.0]
...: m=[v,v,v]
...: m
...:
Out[29]:
[[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0]]
In [30]: v[0]='Python'
...: m
...:
Out[30]:
[['Python', 0.75, 1.0, 1.5, 2.0],
['Python', 0.75, 1.0, 1.5, 2.0],
['Python', 0.75, 1.0, 1.5, 2.0]]
In [31]: v=[0.5,0.75,1.0,1.5,2.0]
...: m=3*[deepcopy(v),]
...: m
...:
Traceback (most recent call last):
File "<ipython-input-31-166915cf6c14>", line 2, in <module>
m=3*[deepcopy(v),]
NameError: name 'deepcopy' is not defined
In [32]: from copy import deepcopy
In [33]: v=[0.5,0.75,1.0,1.5,2.0]
...: m=3*[deepcopy(v),]
...: m
...:
Out[33]:
[[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0]]
In [34]: v[0]='Python'
In [35]: m
Out[35]:
[[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0],
[0.5, 0.75, 1.0, 1.5, 2.0]]
In [36]: import numpy as np
In [37]: a=np.array([0,0.5,1.0,1.5,2.0])
...: type(a)
...:
Out[37]: numpy.ndarray
In [38]: a[:2]
Out[38]: array([ 0. , 0.5])
In [39]: a.sum()
Out[39]: 5.0
In [40]: a.std()
Out[40]: 0.70710678118654757
In [41]: a.cumsum()
Out[41]: array([ 0. , 0.5, 1.5, 3. , 5. ])
In [42]: a*2
Out[42]: array([ 0., 1., 2., 3., 4.])
In [43]: a**2
Out[43]: array([ 0. , 0.25, 1. , 2.25, 4. ])
In [44]: np.sqrt(a)
Out[44]: array([ 0. , 0.70710678, 1. , 1.22474487, 1.41421356])
In [45]: b=np.array([a,a*2])
...: b
...:
Out[45]:
array([[ 0. , 0.5, 1. , 1.5, 2. ],
[ 0. , 1. , 2. , 3. , 4. ]])
In [46]: b[0]
Out[46]: array([ 0. , 0.5, 1. , 1.5, 2. ])
In [47]: b[0,2]
Out[47]: 1.0
In [48]: b.sum()
Out[48]: 15.0
In [49]: b.sum(axis=0)
Out[49]: array([ 0. , 1.5, 3. , 4.5, 6. ])
In [50]: b.sum(axis=1)
Out[50]: array([ 5., 10.])
In [51]: c=np.zeros((2,3,4),dtype='i',order='C')
In [52]: c
Out[52]:
array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]])
In [53]: d=np.ones_like(c,dtype='f16',order='C')
In [53]:
In [54]: d
Out[54]:
array([[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]],
[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]], dtype=float32)
In [55]: import random
...: I=5000
...:
In [56]: %time mat=[[random.gauss(0,1) for j in range(I)] for i in range(I)]
Wall time: 25.8 s
In [57]: %time reduce(lambda x,y:x+y,[reduce(lambda x,y:x+y,row) for row in mat])
Wall time: 2.97 s
Out[57]: 445.49932903342665
In [58]: %time mat=np.random.standard_normal((I,I))
Wall time: 1.13 s
In [59]: %time mat.sum()
Wall time: 28 ms
Out[59]: 2638.7219038800572
In [60]: dt=np.dtype([('Name','S10'),('Age','i4'),('Height','f'),('Children/Pets','i4',2)])
In [61]: s=np.array([('Smith',45,1.83,(0,1)),('Jones',53,1.72,(2,2))],dtype=dt)
In [62]: s
Out[62]:
array([('Smith', 45, 1.8300000429153442, [0, 1]),
('Jones', 53, 1.7200000286102295, [2, 2])],
dtype=[('Name', 'S10'), ('Age', '<i4'), ('Height', '<f4'), ('Children/Pets', '<i4', (2,))])
In [63]: s['Name']
Out[63]:
array(['Smith', 'Jones'],
dtype='|S10')
In [64]: s['Height'].mean()
Out[64]: 1.7750001
In [65]: s[1]['Age']
Out[65]: 53
In [66]: r=np.random.standard_normal((4,3))
...: s=np.random.standard_normal((4,3))
...:
In [67]: r+s
Out[67]:
array([[-1.08236119, -0.40845649, -1.39591288],
[ 0.67282979, -1.67472306, 1.02340313],
[ 0.25657081, 2.4666021 , -1.08896318],
[-0.65978059, -0.89335707, 2.04573807]])
In [68]: 2*r+3
Out[68]:
array([[ 0.4383362 , 2.06321283, 1.73050739],
[ 6.24047385, 1.95914397, 5.98931023],
[ 3.30047169, 5.48339121, 3.1089062 ],
[ 3.36953153, 1.69323078, 4.93485884]])
In [69]: s=np.random.standard_normal(3)
In [70]: r+s
Out[70]:
array([[-0.16614389, -0.12067005, -0.28967217],
[ 2.73492494, -0.17270448, 1.83972925],
[ 1.26492386, 1.58941914, 0.39952724],
[ 1.29945378, -0.30566107, 1.31250355]])
In [71]: s=np.random.standard_normal(4)
In [72]: r+s
Traceback (most recent call last):
File "<ipython-input-72-500bf6181e22>", line 1, in <module>
r+s
ValueError: operands could not be broadcast together with shapes (4,3) (4,)
In [73]: r.transpose()+s
Out[73]:
array([[-1.28805207, 2.92514733, 1.00283501, -0.78751221],
[-0.47561376, 0.78448238, 2.09429476, -1.62566259],
[-0.64196647, 2.79956551, 0.90705226, -0.00484856]])
In [74]: np.shape(r,T)
Traceback (most recent call last):
File "<ipython-input-74-65fbd7bcb393>", line 1, in <module>
np.shape(r,T)
NameError: name 'T' is not defined
In [75]: np.shape(4,s)
Traceback (most recent call last):
File "<ipython-input-75-550edbb0f81b>", line 1, in <module>
np.shape(4,s)
TypeError: shape() takes exactly 1 argument (2 given)
In [76]: np.shape(r,s)
Traceback (most recent call last):
File "<ipython-input-76-2f6caeb9b36c>", line 1, in <module>
np.shape(r,s)
TypeError: shape() takes exactly 1 argument (2 given)
In [77]: np.shape(r)
Out[77]: (4L, 3L)
In [78]: def f(x):
...: return 3*x+5
...:
In [79]: f(0.5)
Out[79]: 6.5
In [80]: f(r)
Out[80]:
array([[ 1.1575043 , 3.59481924, 3.09576109],
[ 9.86071078, 3.43871595, 9.48396534],
[ 5.45070754, 8.72508681, 5.1633593 ],
[ 5.5542973 , 3.03984618, 7.90228826]])
In [81]: import math
In [82]: math.sin(r)
Traceback (most recent call last):
File "<ipython-input-82-36a33c1d845d>", line 1, in <module>
math.sin(r)
TypeError: only length-1 arrays can be converted to Python scalars
In [83]: np.sin(r)
Out[83]:
array([[-0.95825405, -0.45145347, -0.59297325],
[ 0.99877806, -0.49725153, 0.99710266],
[ 0.14967133, 0.94633337, 0.05442619],
[ 0.18371629, -0.60787737, 0.82342984]])
In [84]: np.sin(np.pi)
Out[84]: 1.2246467991473532e-16
In [85]: x=np.random.standard_normal((5,10000000))\
...: y=2*x+3
File "<ipython-input-85-77676bfcb2ae>", line 1
x=np.random.standard_normal((5,10000000))y=2*x+3
^
SyntaxError: invalid syntax
In [86]: x=np.random.standard_normal((5,10000000))
...: y=2*x+3
...: C=np.array((x,y),order='C')
...: F=np.array((x,y),order='F')
...: x=0.0,y=0.0
File "<ipython-input-86-f82a1956aebd>", line 5
x=0.0,y=0.0
SyntaxError: can't assign to literal
In [87]: x=np.random.standard_normal((5,10000000))
...: y=2*x+3
...: C=np.array((x,y),order='C')
...: F=np.array((x,y),order='F')
...: x=0.0;y=0.0
...:
In [88]: C[:2].round(2)
Out[88]:
array([[[ 1.5 , 0.72, 1. , ..., 1.81, -1.73, -0.89],
[ 0.06, 2.27, 0.51, ..., -0.31, 0.13, -0.55],
[ 1.77, 1.52, -0.48, ..., -0.22, 1.05, 0.67],
[-0.96, 0.53, -1.19, ..., -0.3 , -1.59, -1.75],
[-1.12, -1.62, 1.14, ..., 0.34, 1.15, -0.11]],
[[ 6. , 4.43, 5.01, ..., 6.63, -0.46, 1.21],
[ 3.12, 7.54, 4.02, ..., 2.38, 3.27, 1.89],
[ 6.54, 6.04, 2.03, ..., 2.56, 5.1 , 4.33],
[ 1.08, 4.06, 0.63, ..., 2.39, -0.18, -0.49],
[ 0.76, -0.25, 5.29, ..., 3.68, 5.29, 2.79]]])
In [89]: %timeit C.sum()
10 loops, best of 3: 103 ms per loop
In [90]: %timeit F.sum()
10 loops, best of 3: 103 ms per loop
In [91]: %timeit C[0].sum(axis=0)
10 loops, best of 3: 92.4 ms per loop
In [92]: %timeit C[0].sum(axis=1)
10 loops, best of 3: 50.8 ms per loop
In [93]: %timeit F.sum(axis=0)
1 loops, best of 3: 770 ms per loop
In [94]: %timeit F.sum(axis=1)
1 loops, best of 3: 2.1 s per loop
In [95]: