Professional Documents
Culture Documents
Python Programming Basics
Python Programming Basics
In [66]: pd.DataFrame(p)
Out[66]:
col_1 col_2
0 {u'n1': 5} {u'n1': 9}
1 {u'n2': 6} {u'n2': 10}
In [67]: pd.DataFrame(p)
Out[67]:
col_1 col_2
0 {u'n1': 5} {u'n1': 9}
1 {u'n2': 6} {u'n2': 10}
In [68]: np.array((5,5))
Out[68]: array([5, 5])
In [69]: np.random.randint((4,4))
mtrand.pyx in mtrand.RandomState.randint()
In [70]: np.random.randint(1,100,(4,4))
Out[70]:
array([[13, 1, 76, 14],
[21, 55, 98, 93],
[88, 74, 88, 66],
[91, 41, 49, 45]])
In [71]: np.random((3,4))
In [72]: np.random.random((3,3))
Out[72]:
array([[0.62880183, 0.24702432, 0.13784995],
[0.51472655, 0.77375286, 0.82740054],
[0.90100416, 0.16905432, 0.48120537]])
In [73]: A= np.random.randint(1,100,(4,4))
In [74]: A
Out[74]:
array([[55, 23, 92, 55],
[20, 30, 26, 44],
[37, 42, 52, 82],
[54, 82, 75, 19]])
In [75]: A.copy()
Out[75]:
array([[55, 23, 92, 55],
[20, 30, 26, 44],
[37, 42, 52, 82],
[54, 82, 75, 19]])
In [76]: Z = A
In [77]: Z
Out[77]:
array([[55, 23, 92, 55],
[20, 30, 26, 44],
[37, 42, 52, 82],
[54, 82, 75, 19]])
In [78]: np.sort(Z)
Out[78]:
array([[23, 55, 55, 92],
[20, 26, 30, 44],
[37, 42, 52, 82],
[19, 54, 75, 82]])
In [79]: help(np.sort)
Help on function sort in module numpy.core.fromnumeric:
Parameters
----------
a : array_like
Array to be sorted.
axis : int or None, optional
Axis along which to sort. If None, the array is flattened before
sorting. The default is -1, which sorts along the last axis.
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
Sorting algorithm. Default is 'quicksort'.
order : str or list of str, optional
When `a` is an array with fields defined, this argument specifies
which fields to compare first, second, etc. A single field can
be specified as a string, and not all fields need be specified,
but unspecified fields will still be used, in the order in which
they come up in the dtype, to break ties.
Returns
-------
sorted_array : ndarray
Array of the same type and shape as `a`.
See Also
--------
ndarray.sort : Method to sort an array in-place.
argsort : Indirect sort.
lexsort : Indirect stable sort on multiple keys.
searchsorted : Find elements in a sorted array.
partition : Partial sort.
Notes
-----
The various sorting algorithms are characterized by their average speed,
worst case performance, work space size, and whether they are stable. A
stable sort keeps items with the same key in the same relative
order. The three available algorithms have the following
properties:
All the sort algorithms make temporary copies of the data when
sorting along any but the last axis. Consequently, sorting along
the last axis is faster and uses less space than sorting along
any other axis.
The sort order for complex numbers is lexicographic. If both the real
and imaginary parts are non-nan then the order is determined by the
real parts except when they are equal, in which case the order is
determined by the imaginary parts.
Previous to numpy 1.4.0 sorting real and complex arrays containing nan
values led to undefined behaviour. In numpy versions >= 1.4.0 nan
values are sorted to the end. The extended sort order is:
where R is a non-nan real value. Complex values with the same nan
placements are sorted according to the non-nan part if it exists.
Non-nan values are sorted as before.
.. versionadded:: 1.12.0
Examples
--------
>>> a = np.array([[1,4],[3,1]])
>>> np.sort(a) # sort along the last axis
array([[1, 4],
[1, 3]])
>>> np.sort(a, axis=None) # sort the flattened array
array([1, 1, 3, 4])
>>> np.sort(a, axis=0) # sort along the first axis
array([[1, 1],
[3, 4]])
In [80]: Z
Out[80]:
array([[55, 23, 92, 55],
[20, 30, 26, 44],
[37, 42, 52, 82],
[54, 82, 75, 19]])
In [81]: np.sort(z,axis = 1)
Out[81]:
array([[1, 1, 4],
[1, 3, 3],
[2, 3, 4]])
In [82]: np.sort(Z,axis = 1)
Out[82]:
array([[23, 55, 55, 92],
[20, 26, 30, 44],
[37, 42, 52, 82],
[19, 54, 75, 82]])
In [86]: x = np.random.random([3,3])
In [87]: x
Out[87]:
array([[0.8016841 , 0.12227096, 0.65451371],
[0.2947682 , 0.88168101, 0.11115688],
[0.5604033 , 0.54275188, 0.65132816]])
In [88]: x = np.random.random([2,3])
In [89]: x
Out[89]:
array([[0.56574296, 0.96400087, 0.59604626],
[0.61267651, 0.99812334, 0.95208975]])
In [90]: p = np.matrix(x)
In [91]: p
Out[91]:
matrix([[0.56574296, 0.96400087, 0.59604626],
[0.61267651, 0.99812334, 0.95208975]])
In [92]: x
Out[92]:
array([[0.56574296, 0.96400087, 0.59604626],
[0.61267651, 0.99812334, 0.95208975]])
In [93]: x.shape()
In [94]: x.shape
Out[94]: (2L, 3L)
In [95]: len(x)
Out[95]: 2
In [96]: x.dim
AttributeErrorTraceback (most recent call last)
<ipython-input-96-d19b20e12c3e> in <module>()
----> 1 x.dim
In [97]: x.ndim
Out[97]: 2
In [98]: x.resize((3,2))
In [99]: x
Out[99]:
array([[0.56574296, 0.96400087],
[0.59604626, 0.61267651],
[0.99812334, 0.95208975]])
In [100]: x.reshape()
In [101]: x.reshape((3,2))
Out[101]:
array([[0.56574296, 0.96400087],
[0.59604626, 0.61267651],
[0.99812334, 0.95208975]])
In [102]: x.flatten()
Out[102]:
array([0.56574296, 0.96400087, 0.59604626, 0.61267651, 0.99812334,
0.95208975])
In [103]: x.flatten()
Out[103]:
array([0.56574296, 0.96400087, 0.59604626, 0.61267651, 0.99812334,
0.95208975])
In [104]: x.flatten().resize(2,3\)
File "<ipython-input-104-95fa8b2ca2b1>", line 1
x.flatten().resize(2,3\)
^
SyntaxError: unexpected character after line continuation character
In [105]: x.flatten().resize(2,3)
In [106]: x
Out[106]:
array([[0.56574296, 0.96400087],
[0.59604626, 0.61267651],
[0.99812334, 0.95208975]])
In [107]: x
Out[107]:
array([[0.56574296, 0.96400087],
[0.59604626, 0.61267651],
[0.99812334, 0.95208975]])
In [108]: x.reshape(3,2)
Out[108]:
array([[0.56574296, 0.96400087],
[0.59604626, 0.61267651],
[0.99812334, 0.95208975]])
In [109]: x.reshape((3,2))
Out[109]:
array([[0.56574296, 0.96400087],
[0.59604626, 0.61267651],
[0.99812334, 0.95208975]])
In [110]: z = np.random.randint((5,5))
mtrand.pyx in mtrand.RandomState.randint()
In [111]: z = np.random.randint(1,100,(5,5))
In [112]: z
Out[112]:
array([[39, 7, 73, 62, 24],
[86, 63, 33, 67, 45],
[68, 59, 23, 49, 14],
[20, 44, 60, 5, 85],
[ 6, 29, 12, 70, 32]])
In [113]: z[0,0]
Out[113]: 39
In [114]: z[2,4]
Out[114]: 14
In [115]: z[0,3]
Out[115]: 62
In [116]: z[1,3]
Out[116]: 67
In [117]: z[0:1,1:2]
Out[117]: array([[7]])
In [118]: z[0:2,1:3]
Out[118]:
array([[ 7, 73],
[63, 33]])
In [119]: z = linspace(1:2:12)
File "<ipython-input-119-061d0f1205f4>", line 1
z = linspace(1:2:12)
^
SyntaxError: invalid syntax
In [120]: z = linspace(1,12,3)
In [121]: z = np.linspace(1,12,3)
In [122]: z
Out[122]: array([ 1. , 6.5, 12. ])
In [123]: z
Out[123]: array([ 1. , 6.5, 12. ])
In [124]: y = np.random.randomint(1,100,(4,5))
In [125]: y = np.random.randint(1,100,(4,5))
In [126]: y
Out[126]:
array([[58, 11, 82, 38, 62],
[98, 21, 45, 50, 57],
[72, 5, 73, 39, 79],
[ 3, 44, 13, 61, 29]])
In [134]: y
Out[134]:
array([[258, 211, 282, 238, 262],
[298, 221, 245, 250, 257],
[ 72, 5, 73, 39, 79],
[ 3, 44, 13, 61, 29]])