In [1]:
print 'hello world'


hello world

In [2]:
jark1=365
jark2=366

In [3]:
print jark1
print jark2


365
366

In [4]:
print type(jark1)


<type 'int'>

In [5]:
jark = int('test')
print jark


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-4f289782d0cf> in <module>()
----> 1 jark = int('test')
      2 print jark

ValueError: invalid literal for int() with base 10: 'test'

In [6]:
jark = [1,2,3,4,5]
for i in jark:
    print jark


[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

In [19]:
for i in jark:
    print i


test1
test2

In [20]:
for i in jark:
    print i
print '123'


test1
test2
123

In [10]:
jark =['test1','test2']
for i in range(0,len(jark)):
    print jark[i]


test1
test2

In [17]:
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # 第二个实例
   print '当前水果 :', fruit


当前水果 : banana
当前水果 : apple
当前水果 : mango

In [18]:
for fruit in fruits:        # 第二个实例
   print '当前水果 :', fruit


当前水果 : banana
当前水果 : apple
当前水果 : mango

In [54]:
import numpy
world_alcohol = numpy.genfromtxt('world_alcohol.txt',delimiter=',',skip_header=1)
print type(world_alcohol)
#print world_alcohol
is_value_empty = numpy.isnan(world_alcohol[:,4])
#print is_value_empty
world_alcohol[is_value_empty,4] = '0'
#a = world_alcohol[:,4].astype(float)
#a = a.astype(float)
total_alcohol = world_alcohol[:,4].astype(float).sum()
avg_alcohol = world_alcohol[:,4].astype(float).mean()

print total_alcohol
print avg_alcohol


<type 'numpy.ndarray'>
1137.78
1.14120361083

In [22]:
print world_alcohol


[[             nan              nan              nan              nan
               nan]
 [  1.98600000e+03              nan              nan              nan
    0.00000000e+00]
 [  1.98600000e+03              nan              nan              nan
    5.00000000e-01]
 ..., 
 [  1.98700000e+03              nan              nan              nan
    7.50000000e-01]
 [  1.98900000e+03              nan              nan              nan
    1.50000000e+00]
 [  1.98500000e+03              nan              nan              nan
    3.10000000e-01]]

In [45]:
vector = numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print vector
print matrix

print matrix[:,1]
print matrix[:,0:3]

print 

print matrix[1:2,2:3]


#matrix == 30

#b = (vector == 10)
#print b 
#print vector[b]

#c = (matrix[:,1]==25)
#print c 
#print matrix[:,c]

#print vector.mean()
#print matrix.mean()
#print help(numpy.sum)
print matrix.sum(axis=1)


[ 5 10 15 20]
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]
[10 25 40]
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]

[[30]]
[ 30  75 120]

In [24]:
print vector.shape
print matrix.shape


(4,)
(3, 3)

In [25]:
vector.dtype


Out[25]:
dtype('int64')

In [26]:
matrix.dtype


Out[26]:
dtype('int64')

In [64]:
import numpy as np
print np.arange(15)
a = np.arange(15).reshape(3,5)
a


[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
Out[64]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

In [65]:
a.shape


Out[65]:
(3, 5)

In [66]:
a.ndim


Out[66]:
2

In [67]:
a.dtype.name


Out[67]:
'int64'

In [68]:
a.size


Out[68]:
15

In [77]:
np.zeros((3,4),dtype=np.int32)


Out[77]:
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int32)

In [76]:
np.ones((2,3,4),dtype=np.int32)


Out[76]:
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=int32)

In [80]:
np.arange(10,40,1)


Out[80]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39])

In [81]:
np.arange(14).reshape(2,7)


Out[81]:
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13]])

In [87]:
np.random.random((2,3))*2-1


Out[87]:
array([[-0.2997619 , -0.58260105, -0.36503016],
       [-0.40324714,  0.41624014,  0.3857419 ]])

In [90]:
from numpy import pi
np.linspace(2,2*pi,100)


Out[90]:
array([ 2.        ,  2.0432645 ,  2.086529  ,  2.12979349,  2.17305799,
        2.21632249,  2.25958699,  2.30285149,  2.34611598,  2.38938048,
        2.43264498,  2.47590948,  2.51917398,  2.56243847,  2.60570297,
        2.64896747,  2.69223197,  2.73549647,  2.77876096,  2.82202546,
        2.86528996,  2.90855446,  2.95181896,  2.99508346,  3.03834795,
        3.08161245,  3.12487695,  3.16814145,  3.21140595,  3.25467044,
        3.29793494,  3.34119944,  3.38446394,  3.42772844,  3.47099293,
        3.51425743,  3.55752193,  3.60078643,  3.64405093,  3.68731542,
        3.73057992,  3.77384442,  3.81710892,  3.86037342,  3.90363791,
        3.94690241,  3.99016691,  4.03343141,  4.07669591,  4.1199604 ,
        4.1632249 ,  4.2064894 ,  4.2497539 ,  4.2930184 ,  4.33628289,
        4.37954739,  4.42281189,  4.46607639,  4.50934089,  4.55260539,
        4.59586988,  4.63913438,  4.68239888,  4.72566338,  4.76892788,
        4.81219237,  4.85545687,  4.89872137,  4.94198587,  4.98525037,
        5.02851486,  5.07177936,  5.11504386,  5.15830836,  5.20157286,
        5.24483735,  5.28810185,  5.33136635,  5.37463085,  5.41789535,
        5.46115984,  5.50442434,  5.54768884,  5.59095334,  5.63421784,
        5.67748233,  5.72074683,  5.76401133,  5.80727583,  5.85054033,
        5.89380482,  5.93706932,  5.98033382,  6.02359832,  6.06686282,
        6.11012731,  6.15339181,  6.19665631,  6.23992081,  6.28318531])

In [91]:
np.sin(np.linspace(2,2*pi,100))


Out[91]:
array([  9.09297427e-01,   8.90447773e-01,   8.69931624e-01,
         8.47787376e-01,   8.24056473e-01,   7.98783328e-01,
         7.72015240e-01,   7.43802306e-01,   7.14197328e-01,
         6.83255712e-01,   6.51035366e-01,   6.17596591e-01,
         5.83001968e-01,   5.47316243e-01,   5.10606202e-01,
         4.72940549e-01,   4.34389776e-01,   3.95026031e-01,
         3.54922986e-01,   3.14155693e-01,   2.72800450e-01,
         2.30934655e-01,   1.88636659e-01,   1.45985625e-01,
         1.03061376e-01,   5.99442444e-02,   1.67149258e-02,
        -2.65456752e-02,  -6.97565952e-02,  -1.12836964e-01,
        -1.55706156e-01,  -1.98283940e-01,  -2.40490630e-01,
        -2.82247236e-01,  -3.23475610e-01,  -3.64098591e-01,
        -4.04040152e-01,  -4.43225542e-01,  -4.81581425e-01,
        -5.19036016e-01,  -5.55519218e-01,  -5.90962752e-01,
        -6.25300285e-01,  -6.58467552e-01,  -6.90402482e-01,
        -7.21045306e-01,  -7.50338675e-01,  -7.78227768e-01,
        -8.04660387e-01,  -8.29587065e-01,  -8.52961150e-01,
        -8.74738897e-01,  -8.94879548e-01,  -9.13345411e-01,
        -9.30101924e-01,  -9.45117729e-01,  -9.58364722e-01,
        -9.69818112e-01,  -9.79456463e-01,  -9.87261737e-01,
        -9.93219327e-01,  -9.97318081e-01,  -9.99550331e-01,
        -9.99911896e-01,  -9.98402102e-01,  -9.95023774e-01,
        -9.89783234e-01,  -9.82690290e-01,  -9.73758217e-01,
        -9.63003731e-01,  -9.50446959e-01,  -9.36111403e-01,
        -9.20023890e-01,  -9.02214531e-01,  -8.82716654e-01,
        -8.61566751e-01,  -8.38804405e-01,  -8.14472215e-01,
        -7.88615721e-01,  -7.61283312e-01,  -7.32526143e-01,
        -7.02398033e-01,  -6.70955367e-01,  -6.38256992e-01,
        -6.04364104e-01,  -5.69340132e-01,  -5.33250627e-01,
        -4.96163129e-01,  -4.58147051e-01,  -4.19273538e-01,
        -3.79615345e-01,  -3.39246692e-01,  -2.98243131e-01,
        -2.56681400e-01,  -2.14639283e-01,  -1.72195464e-01,
        -1.29429377e-01,  -8.64210589e-02,  -4.32510021e-02,
        -2.44929360e-16])

In [2]:
import numpy as np
B = np.arange(3)
print B
print np.exp(B)
print np.sqrt(B)


[0 1 2]
[ 1.          2.71828183  7.3890561 ]
[ 0.          1.          1.41421356]

In [9]:
a = np.floor(10*np.random.random((3,4)))
print a 
print '--------'
print a.ravel()
print '--------'
#a.shape = (6,2)
a.reshape(6,-1)
print a 
print '--------'
print a.T


[[ 8.  5.  4.  2.]
 [ 8.  6.  8.  4.]
 [ 5.  0.  2.  3.]]
--------
[ 8.  5.  4.  2.  8.  6.  8.  4.  5.  0.  2.  3.]
--------
[[ 8.  5.  4.  2.]
 [ 8.  6.  8.  4.]
 [ 5.  0.  2.  3.]]
--------
[[ 8.  8.  5.]
 [ 5.  6.  0.]
 [ 4.  8.  2.]
 [ 2.  4.  3.]]

In [12]:
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print a 
print '-----'
print b 
print '-----'
print np.hstack((a,b))
print np.vstack((a,b))


[[ 5.  7.]
 [ 2.  7.]]
-----
[[ 5.  9.]
 [ 6.  2.]]
-----
[[ 5.  7.  5.  9.]
 [ 2.  7.  6.  2.]]
[[ 5.  7.]
 [ 2.  7.]
 [ 5.  9.]
 [ 6.  2.]]

In [19]:
a = np.floor(10*np.random.random((2,12)))
print a 
print '-----'
print np.hsplit(a,3)
print '-----'
print np.hsplit(a,(3,7))
print '-----'

a = np.floor(10*np.random.random((12,2)))
print a
print '-----'
#np.vsplit(a,3)
print np.vsplit(a,3)


[[ 4.  8.  7.  0.  8.  4.  1.  4.  5.  4.  6.  7.]
 [ 7.  9.  8.  4.  2.  3.  3.  1.  0.  4.  1.  5.]]
-----
[array([[ 4.,  8.,  7.,  0.],
       [ 7.,  9.,  8.,  4.]]), array([[ 8.,  4.,  1.,  4.],
       [ 2.,  3.,  3.,  1.]]), array([[ 5.,  4.,  6.,  7.],
       [ 0.,  4.,  1.,  5.]])]
-----
[array([[ 4.,  8.,  7.],
       [ 7.,  9.,  8.]]), array([[ 0.,  8.,  4.,  1.],
       [ 4.,  2.,  3.,  3.]]), array([[ 4.,  5.,  4.,  6.,  7.],
       [ 1.,  0.,  4.,  1.,  5.]])]
-----
[[ 3.  8.]
 [ 7.  6.]
 [ 2.  7.]
 [ 2.  8.]
 [ 0.  9.]
 [ 6.  0.]
 [ 8.  2.]
 [ 2.  6.]
 [ 2.  4.]
 [ 4.  4.]
 [ 4.  4.]
 [ 7.  2.]]
-----
[array([[ 3.,  8.],
       [ 7.,  6.],
       [ 2.,  7.],
       [ 2.,  8.]]), array([[ 0.,  9.],
       [ 6.,  0.],
       [ 8.,  2.],
       [ 2.,  6.]]), array([[ 2.,  4.],
       [ 4.,  4.],
       [ 4.,  4.],
       [ 7.,  2.]])]

In [21]:
a = np.arange(12)
b = a 
print a,b
print b is a 
b.shape = 3,4
print a.shape
print id(a)
print id(b)


[ 0  1  2  3  4  5  6  7  8  9 10 11] [ 0  1  2  3  4  5  6  7  8  9 10 11]
True
(3, 4)
4649038032
4649038032

In [45]:
c = a.view()
print c is a
#c.reshape(6,2)
c.shape = (2,6)
print c.shape
print a 
print c 
c[0,0]=9999
print a 
print c 
print id(a)
print id(c)


False
(2, 6)
[[9999    1    2    3]
 [9999    5    6    7]
 [   8    9   10   11]]
[[9999    1    2    3 9999    5]
 [   6    7    8    9   10   11]]
[[9999    1    2    3]
 [9999    5    6    7]
 [   8    9   10   11]]
[[9999    1    2    3 9999    5]
 [   6    7    8    9   10   11]]
4649038032
4649767920

In [ ]:
d = a.copy()
print d is a 
d[0,0] =9999
print d
print a 
print id(a)
print id(d)

In [47]:
data = np.sin(np.arange(20)).reshape(5,4)
print data
print data.reshape(4,5)
print data.shape
#reshape cann't change this arange


[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
[[ 0.          0.84147098  0.90929743  0.14112001 -0.7568025 ]
 [-0.95892427 -0.2794155   0.6569866   0.98935825  0.41211849]
 [-0.54402111 -0.99999021 -0.53657292  0.42016704  0.99060736]
 [ 0.65028784 -0.28790332 -0.96139749 -0.75098725  0.14987721]]
(5, 4)

In [72]:
print data
ind = data.argmax(axis=1)
print ind
#data_max = data[ind,range(data.shape[0])]
#print data_max
print data.shape[0]
#print range(data.shape[1])
#print data[ind,range(data.shape[1])]
print data[range(data.shape[0]),ind]


[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
[2 3 0 2 3]
5
[ 0.90929743  0.6569866   0.98935825  0.99060736  0.14987721]

In [75]:
print data
index = data.argmax(axis=0)
print index
print range(data.shape[1])
print data[index,range(data.shape[1])]


[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
[2 0 3 1]
[0, 1, 2, 3]
[ 0.98935825  0.84147098  0.99060736  0.6569866 ]

In [77]:
print data 
ind = data.argmin(axis=1)
print ind
print range(data.shape[0])
print data[range(data.shape[0]),ind]


[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
[0 1 3 0 1]
[0, 1, 2, 3, 4]
[ 0.         -0.95892427 -0.99999021 -0.53657292 -0.96139749]

In [79]:
a = np.arange(0,40,10)
print a 
b = np.tile(a,(3,4))
print b


[ 0 10 20 30]
[[ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]]

In [96]:
a = np.array([[4,3,5],[8,7,6],[1,2,8]])
print a 
print '-----'
print np.sort(a,axis=0)
print '-----'
print np.sort(a,axis=1)
print '-----'
a.sort(axis=0)
print a


[[4 3 5]
 [8 7 6]
 [1 2 8]]
-----
[[1 2 5]
 [4 3 6]
 [8 7 8]]
-----
[[3 4 5]
 [6 7 8]
 [1 2 8]]
-----
[[1 2 5]
 [4 3 6]
 [8 7 8]]

In [97]:
a = np.array([4,1,3,2])
j = np.argsort(a)
print j 
print '-----'
print a[j]


[1 3 2 0]
-----
[1 2 3 4]

In [ ]: