Welcome to Canopy's interactive data-analysis environment!

Type '?' for more information.

Python 2.7.13 |Enthought, Inc. (x86_64)| (default, Mar 2 2017, 16:05:12) [MSC v.1500 64 bit (AMD64)]

Type "copyright", "credits" or "license" for more information.


IPython 5.3.0 -- An enhanced Interactive Python.

? -> 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.


# Strings:


In [17]: s = "Yossarian said, "


In [18]: twice = 'I see everything twice!'


In [19]: s+twice

Out[19]: 'Yossarian said, I see everything twice!'


In [20]: twice*2

Out[20]: 'I see everything twice!I see everything twice!'


In [21]: twice*5

Out[21]: 'I see everything twice!I see everything twice!I see everything twice!I see everything twice!I see everything twice!'


In [22]: len(s)

Out[22]: 16


In [23]: len(twice*2)

Out[23]: 46


In [24]: len(twice)

Out[24]: 23


In [25]: twice.find('thing')

Out[25]: 11


In [26]: twice[11]

Out[26]: 't'


In [27]: twice.find('thing-bla')

Out[27]: -1


In [28]: twice.replace('e', 'A')

Out[28]: 'I sAA AvArything twicA!'


In [29]: twice.replace('ev', 'AB')

Out[29]: 'I see ABerything twice!'


In [30]: twice.upper()

Out[30]: 'I SEE EVERYTHING TWICE!'


In [31]: _.lower()

Out[31]: 'i see everything twice!'


In [32]: twice.captalize()


AttributeErrorTraceback (most recent call last)
<ipython-input-32-8cbf19e78e45> in <module>()
----> 1 twice.captalize()

AttributeError: 'str' object has no attribute 'captalize'


In [33]: twice.capitalize()

Out[33]: 'I see everything twice!'


In [34]: print twice

I see everything twice!


# Formating output:

In [35]: print 'Floating point: %f' % 123.063

Floating point: 123.063000


In [36]: x = 123.063


In [37]: print 'Floating point: %f' % x

Floating point: 123.063000


In [38]: print 'Floating point: %.3f' % x

Floating point: 123.063


In [39]: print 'Floating point: %10.3f' % x

Floating point: 123.063


In [40]: print 'Floating point: %10.3f. Integer: %d' % (x, 145)

Floating point: 123.063. Integer: 145


In [41]: y = 145


In [42]: print 'Floating point: %10.3f. Integer: %d. Hex: %x' % (x, y, y)

Floating point: 123.063. Integer: 145. Hex: 91


In [43]: y = 31


In [44]: print 'Floating point: %10.3f. Integer: %d. Hex: %x' % (x, y, y)

Floating point: 123.063. Integer: 31. Hex: 1f


In [45]: print 'Floating point: %10.3f. Integer: %d. Hex: %x. String: %s' % (x, y, y, 'hello')

Floating point: 123.063. Integer: 31. Hex: 1f. String: hello


In [47]: ar = arange(6).reshape(2,3)


In [48]: ar

Out[48]:

array([[0, 1, 2],

[3, 4, 5]])


In [49]: s2 = array_str(ar)


In [50]: s2

Out[50]: '[[0 1 2]\n [3 4 5]]'


In [51]: print 'My matrix:\n %s' % s2

My matrix:

[[0 1 2]

[3 4 5]]


# Reading/Writing files:

In [52]: ar = np.loadtxt('myfile01.csv')


ValueErrorTraceback (most recent call last)
<ipython-input-52-a1020855793f> in <module>()
----> 1 ar = np.loadtxt('myfile01.csv')

C:\Users\Oleksandr\AppData\Local\Enthought\Canopy\edm\envs\standard27\lib\site-packages\numpy\lib\npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
    928
    929             # Convert each value according to its column and store
--> 930             items = [conv(val) for (conv, val) in zip(converters, vals)]
    931             # Then pack it according to the dtype's nesting
    932             items = pack_items(items, packing)
C:\Users\Oleksandr\AppData\Local\Enthought\Canopy\edm\envs\standard27\lib\site-packages\numpy\lib\npyio.pyc in floatconv(x)
    657         if b'0x' in x:
    658             return float.fromhex(asstr(x))
--> 659         return float(x)
    660
    661     typ = dtype.type
ValueError: invalid literal for float(): 95.01,76.21,61.54,40.57,5.79,20.28,1.53


In [53]: ar = np.loadtxt('myfile01.csv', delimiter=',')


In [54]: ar

Out[54]:

array([[ 95.01, 76.21, 61.54, 40.57, 5.79, 20.28, 1.53],

[ 23.11, 45.65, 79.19, 93.55, 35.29, 19.87, 74.68],

[ 60.68, 1.85, 92.18, 91.69, 81.32, 60.38, 44.51],

[ 48.6 , 82.14, 73.82, 41.03, 0.99, 27.22, 93.18],

[ 89.13, 44.47, 17.63, 89.36, 13.89, 19.88, 46.6 ]])


In [55]: ar = np.loadtxt('myfile02.csv', delimiter=',')


ValueErrorTraceback (most recent call last)
<ipython-input-55-51b996d33666> in <module>()
----> 1 ar = np.loadtxt('myfile02.csv', delimiter=',')

C:\Users\Oleksandr\AppData\Local\Enthought\Canopy\edm\envs\standard27\lib\site-packages\numpy\lib\npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
    928
    929             # Convert each value according to its column and store
--> 930             items = [conv(val) for (conv, val) in zip(converters, vals)]
    931             # Then pack it according to the dtype's nesting
    932             items = pack_items(items, packing)
C:\Users\Oleksandr\AppData\Local\Enthought\Canopy\edm\envs\standard27\lib\site-packages\numpy\lib\npyio.pyc in floatconv(x)
    657         if b'0x' in x:
    658             return float.fromhex(asstr(x))
--> 659         return float(x)
    660
    661     typ = dtype.type
ValueError: could not convert string to float: A


In [56]: np.genfromtxt('myfile02.csv', dtype=float, delimiter=',', names=True)

Out[56]:

array([(95.01, 76.21, 61.54, 40.57, 5.79, 20.28, 1.53),

(23.11, 45.65, 79.19, 93.55, 35.29, 19.87, 74.68),

(60.68, 1.85, 92.18, 91.69, 81.32, 60.38, 44.51),

(48.6, 82.14, 73.82, 41.03, 0.99, 27.22, 93.18),

(89.13, 44.47, 17.63, 89.36, 13.89, 19.88, 46.6)],

dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<f8'), ('D', '<f8'), ('E', '<f8'), ('F', '<f8'), ('G', '<f8')])


In [57]: ar[0,0]

Out[57]: 95.010000000000005


In [58]: ar = np.genfromtxt('myfile02.csv', dtype=float, delimiter=',', names=True)


In [59]: ar[0,0]


IndexErrorTraceback (most recent call last)
<ipython-input-59-fa156fc3dbac> in <module>()
----> 1 ar[0,0]

IndexError: too many indices for array


In [60]: ar[0]

Out[60]: (95.01, 76.21, 61.54, 40.57, 5.79, 20.28, 1.53)


In [61]: ar[1]

Out[61]: (23.11, 45.65, 79.19, 93.55, 35.29, 19.87, 74.68)


In [65]: ar = loadtxt('myfile01.csv', delimiter=',')


In [66]: ar

Out[66]:

array([[ 95.01, 76.21, 61.54, 40.57, 5.79, 20.28, 1.53],

[ 23.11, 45.65, 79.19, 93.55, 35.29, 19.87, 74.68],

[ 60.68, 1.85, 92.18, 91.69, 81.32, 60.38, 44.51],

[ 48.6 , 82.14, 73.82, 41.03, 0.99, 27.22, 93.18],

[ 89.13, 44.47, 17.63, 89.36, 13.89, 19.88, 46.6 ]])


In [67]: ar1=ar.reshape(7,5)


In [68]: ar1

Out[68]:

array([[ 95.01, 76.21, 61.54, 40.57, 5.79],

[ 20.28, 1.53, 23.11, 45.65, 79.19],

[ 93.55, 35.29, 19.87, 74.68, 60.68],

[ 1.85, 92.18, 91.69, 81.32, 60.38],

[ 44.51, 48.6 , 82.14, 73.82, 41.03],

[ 0.99, 27.22, 93.18, 89.13, 44.47],

[ 17.63, 89.36, 13.89, 19.88, 46.6 ]])


In [69]: ar1=ar1*10


In [70]: ar1

Out[70]:

array([[ 950.1, 762.1, 615.4, 405.7, 57.9],

[ 202.8, 15.3, 231.1, 456.5, 791.9],

[ 935.5, 352.9, 198.7, 746.8, 606.8],

[ 18.5, 921.8, 916.9, 813.2, 603.8],

[ 445.1, 486. , 821.4, 738.2, 410.3],

[ 9.9, 272.2, 931.8, 891.3, 444.7],

[ 176.3, 893.6, 138.9, 198.8, 466. ]])


In [75]: np.savetxt('test', ar)


In [76]: savetxt('output.csv', ar, delimiter=',', newline='#')


In [77]: savetxt('output.csv', ar*10, delimiter=',', newline='#')


# More arrays:

In [78]: x = arange(9)


In [80]: x

Out[80]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])


In [82]: x=x.reshape(3,3)


In [83]: x

Out[83]:

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])


Out[85]:

array([[0, 0, 0],

[0, 0, 0],

[0, 0, 0]])


In [86]: y = linspace(0, 1, 9)


In [87]: y

Out[87]: array([ 0. , 0.125, 0.25 , 0.375, 0.5 , 0.625, 0.75 , 0.875, 1. ])


In [88]: y.reshape(3,3)

Out[88]:

array([[ 0. , 0.125, 0.25 ],

[ 0.375, 0.5 , 0.625],

[ 0.75 , 0.875, 1. ]])


In [89]: y=y.reshape(3,3)


In [90]: x

Out[90]:

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])


In [91]: y

Out[91]:

array([[ 0. , 0.125, 0.25 ],

[ 0.375, 0.5 , 0.625],

[ 0.75 , 0.875, 1. ]])


In [92]: x*y

Out[92]:

array([[ 0. , 0.125, 0.5 ],

[ 1.125, 2. , 3.125],

[ 4.5 , 6.125, 8. ]])


In [93]: dot(x, y)

Out[93]:

array([[ 1.875, 2.25 , 2.625],

[ 5.25 , 6.75 , 8.25 ],

[ 8.625, 11.25 , 13.875]])


In [94]: z = arange(3)


In [95]: z

Out[95]: array([0, 1, 2])


In [97]: transpose(z)

Out[97]: array([0, 1, 2])


In [98]: z=z.reshape(3, 1)


In [99]: z

Out[99]:

array([[0],

[1],

[2]])


In [100]: dot(x, z)

Out[100]:

array([[ 5],

[14],

[23]])


In [101]: matrix(x)

Out[101]:

matrix([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])


In [102]: np.asarray(_)

Out[102]:

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])


In [104]: ar

Out[104]:

array([[ 95.01, 76.21, 61.54, 40.57, 5.79, 20.28, 1.53],

[ 23.11, 45.65, 79.19, 93.55, 35.29, 19.87, 74.68],

[ 60.68, 1.85, 92.18, 91.69, 81.32, 60.38, 44.51],

[ 48.6 , 82.14, 73.82, 41.03, 0.99, 27.22, 93.18],

[ 89.13, 44.47, 17.63, 89.36, 13.89, 19.88, 46.6 ]])


In [105]: linalg.det(x)

Out[105]: 0.0


In [106]: linalg.det(y)

Out[106]: 0.0


In [107]: m=random.rand(3,3)


In [108]: linalg.det(m)

Out[108]: 0.0059424094073611076


In [109]: linalg.inv(m)

Out[109]:

array([[ 3.15387856, 6.28254931, -3.14214528],

[ -7.47564975, 8.6969103 , 5.47727013],

[ 10.18899835, -14.90855094, -4.94807362]])


In [110]: linalg.eig(m)

Out[110]:

(array([ 0.72379768+0.j , 0.02266429+0.08772897j,

0.02266429-0.08772897j]),

array([[ 0.68232730+0.j , -0.09702437-0.47750429j,

-0.09702437+0.47750429j],

[ 0.16384250+0.j , -0.44234883+0.19491663j,

-0.44234883-0.19491663j],

[ 0.71245006+0.j , 0.72726264+0.j , 0.72726264-0.j ]]))


In [111]: %run "c:\users\oleksa~1\appdata\local\temp\tmpfjvodm.py"

Loaded data:

[[ 1. 3. -2. 5.]

[ 3. 5. 6. 7.]

[ 2. 4. 3. 8.]]

Exctracted A:

[[ 1. 3. -2.]

[ 3. 5. 6.]

[ 2. 4. 3.]]

Exctracted b:

[[ 5.]

[ 7.]

[ 8.]]

Computed inverse of A:

[[ 2.25 4.25 -7. ]

[-0.75 -1.75 3. ]

[-0.5 -0.5 1. ]]

Found x:

[[-15.]

[ 8.]

[ 2.]]