Demo entry 6365998

002

   

Submitted by anonymous on May 22, 2017 at 11:12
Language: Python 3. Code size: 14.6 kB.

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> a=np.array([1,2,3,4])
>>> a
array([1, 2, 3, 4])
>>> b=np.array([5,6,7,8])
>>> b
array([5, 6, 7, 8])
>>> c=np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
>>> c
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])
>>> np.ze
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    np.ze
AttributeError: 'module' object has no attribute 'ze'
>>> np.zeros((3,4))
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
>>> np.ones((2,3,4),np.int16)
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=int16)
>>> np.empty((2,3))
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
>>> np.full(4,6,np.int16)
array([6, 6, 6, 6], dtype=int16)
>>> np.eye(3)
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> np.arange(0, 0.5, 0.1)
array([ 0. ,  0.1,  0.2,  0.3,  0.4])
>>> np.linspace(0, 0.5, 5)
array([ 0.   ,  0.125,  0.25 ,  0.375,  0.5  ])
>>> np.linspace(0, 0.5, 5, endpoint = False)
array([ 0. ,  0.1,  0.2,  0.3,  0.4])
>>> np.logspace(0, 1, 3)
array([  1.        ,   3.16227766,  10.        ])
>>> np.logspace(0, 1, 3, endpoint = False)
array([ 1.        ,  2.15443469,  4.64158883])
>>> np.logspace(0, 1, 3, base = 2)
array([ 1.        ,  1.41421356,  2.        ])
>>> a = np.array( [1, 2, 3, 4] )
>>> a
array([1, 2, 3, 4])
>>> a.dtype
dtype('int32')
>>> b = np.array([1.2, 3.5, 5.1])
>>> b.dtype
dtype('float64')
>>> ai32 = np.array( [1, 2, 3, 4], dtype = np.int32 )
>>> ai32.dtype
dtype('int32')
>>> af=np.array([1,2,3,4],dtype=np.float)
>>> af.dtype
dtype('float64')
>>> ac = np.array([1, 2, 3, 4], dtype = complex )
>>> ac.dtype
dtype('complex128')
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4])
>>> a
array([1, 2, 3, 4])
>>> b = np.array([5, 6, 7, 8])
>>> b
array([5, 6, 7, 8])
>>> c = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])
>>> c
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])
>>> a.shape
(4,)
>>> b.shape
(4,)
>>> c.shape
(3, 4)
>>> import numpy as np
>>> c = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])
>>> c
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])
>>> c.shape
(3, 4)
>>> c
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])
>>> c.shape = 4, 3
>>> c
array([[ 1,  2,  3],
       [ 4,  4,  5],
       [ 6,  7,  7],
       [ 8,  9, 10]])
>>> import numpy as np
>>> c = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])
>>> c
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])
>>> c.shape = 2, -1
>>> c
array([[ 1,  2,  3,  4,  4,  5],
       [ 6,  7,  7,  8,  9, 10]])
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4])
>>> a
array([1, 2, 3, 4])
>>> d = a.reshape(2, 2)
>>> d
array([[1, 2],
       [3, 4]])
>>> a[1] = 100
>>> a
array([  1, 100,   3,   4])
>>> d
array([[  1, 100],
       [  3,   4]])
>>> a = np.ones((2,2))
>>> a
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> b = np.eye(2)
>>> b
array([[ 1.,  0.],
       [ 0.,  1.]])
>>> vstack((a,b))
Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    vstack((a,b))
NameError: name 'vstack' is not defined
>>> np.vstack((a,b))
array([[ 1.,  1.],
       [ 1.,  1.],
       [ 1.,  0.],
       [ 0.,  1.]])
>>> np.ones((2,2))
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> a
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> b = np.eye(2)
>>> b
array([[ 1.,  0.],
       [ 0.,  1.]])
>>> np.hstack((a,b))
array([[ 1.,  1.,  1.,  0.],
       [ 1.,  1.,  0.,  1.]])
>>> a = np.ones((2,2))
>>> b=a
>>> b
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> b is a
True
>>> c=a.copy()
>>> c
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> c is a
False
>>> a=np.arrange(5)
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    a=np.arrange(5)
AttributeError: 'module' object has no attribute 'arrange'
>>> a = np.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> a[4]
4
>>> a[1:3]
array([1, 2])
>>> a[:3]
array([0, 1, 2])
>>> a[:-1]
array([0, 1, 2, 3])
>>> a
array([0, 1, 2, 3, 4])
>>> a[1:-1:2]
array([1, 3])
>>> a[::-1]
array([4, 3, 2, 1, 0])
>>> a
array([0, 1, 2, 3, 4])
>>> a[5:1:-2]
array([4, 2])
>>> a = np.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> a[1:3]
array([1, 2])
>>> a[1:3]=100,101
>>> a
array([  0, 100, 101,   3,   4])
>>> a = np.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> b = a[1:3]
>>> b
array([1, 2])
>>> b[1] = -10
>>> b
array([  1, -10])
>>> a
array([  0,   1, -10,   3,   4])
>>> x = np.arange(10, 1, -1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> a = x[[3, 3, 1, 8]]
>>> a
array([7, 7, 9, 2])
>>> b = x[[3, 3, -3, 8]]
>>> b
array([7, 7, 4, 2])
>>> x = np.arange(10, 1, -1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> b = x[[3, 3, -3, 8]]
>>> b
array([7, 7, 4, 2])
>>> b[2] = 100
>>> b
array([  7,   7, 100,   2])
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x = np.arange(10, 1, -1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[[3, 5, 1]] = -1, -2, -3
>>> x
array([10, -3,  8, -1,  6, -2,  4,  3,  2])
>>> x = np.arange(10,1,-1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[np.array([3,3,1,8])]
array([7, 7, 9, 2])
>>> x = np.arange(10,1,-1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[np.array([[3,3,1,8],[3,3,-3,8]])]
array([[7, 7, 9, 2],
       [7, 7, 4, 2]])
>>> x = np.arange(5,0,-1)
>>> x
array([5, 4, 3, 2, 1])
>>> x[np.array([True, False, True, False, False])]
array([5, 3])
>>> x = np.arange(5,0,-1)
>>> x
array([5, 4, 3, 2, 1])
>>> x[[True, False, True, False, False]]

Warning (from warnings module):
  File "__main__", line 1
FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
array([4, 5, 4, 5, 5])
>>> x = np.arange(5,0,-1)
>>> x
array([5, 4, 3, 2, 1])
>>> x[np.array([True, False, True, True])]

Warning (from warnings module):
  File "__main__", line 1
VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 5 but corresponding boolean dimension is 4
array([5, 3, 2])
>>> x = np.arange(5,0,-1)
>>> x
array([5, 4, 3, 2, 1])
>>> x[np.array([True,False,True,True])]=-1,-2,-3
>>> x
array([-1,  4, -2, -3,  1])
>>> x = np.random.randint(0, 10, 3)
>>> x
array([0, 7, 2])
>>> x>5
array([False,  True, False], dtype=bool)
>>> x[x>5]
array([7])
>>> from numpy import random as nr
>>> np.set_printoptions(precision=2)
>>> r1 = nr.rand(4, 3)
>>> r1
array([[ 0.15,  0.87,  0.99],
       [ 0.85,  0.49,  0.6 ],
       [ 0.04,  0.69,  0.81],
       [ 0.47,  0.89,  0.84]])
>>> from numpy import random as nr
>>> np.set_printoptions(precision=2)
>>> r2 = nr.randn(4, 3)
>>> r2
array([[ 0.58, -1.51,  0.63],
       [-0.77, -0.49, -0.53],
       [ 1.84, -1.68,  0.67],
       [-1.08, -1.36,  0.57]])
>>> from numpy import random as nr
>>> np.set_printoptions(precision=2)
>>> r3 = nr.randint(0, 10, (4, 3))
>>> r3
array([[2, 0, 7],
       [8, 6, 7],
       [5, 9, 9],
       [1, 2, 0]])
>>> r3 = nr.randint(0, 10, (4, 3))
>>> r3
array([[0, 7, 7],
       [9, 7, 4],
       [3, 9, 3],
       [1, 1, 6]])
>>> np.sum(r3)
57
>>> np.sum(r3, axis = 1)
array([14, 20, 15,  8])
>>> np.sum(r3, axis = 0)
array([13, 24, 20])
>>> r3 = nr.randint(0, 10, (4, 3))
>>> r3
array([[3, 6, 3],
       [1, 1, 6],
       [7, 4, 3],
       [2, 8, 1]])
>>> np.mean(r3)
3.75
>>> np.mean(r3, axis=1)
array([ 4.  ,  2.67,  4.67,  3.67])
>>> np.mean(r3, axis=1)
array([ 4.  ,  2.67,  4.67,  3.67])
>>> np.mean(r3, axis=0)
array([ 3.25,  4.75,  3.25])
>>> a = np.random.randint(0,10,size=(4,5))
>>> a
array([[7, 5, 8, 7, 5],
       [7, 9, 5, 2, 5],
       [6, 0, 0, 8, 8],
       [7, 3, 3, 4, 7]])
>>> np.sort(a)
array([[5, 5, 7, 7, 8],
       [2, 5, 5, 7, 9],
       [0, 0, 6, 8, 8],
       [3, 3, 4, 7, 7]])
>>> a = np.random.randint(0,10,size=(4,5))
>>> a
array([[7, 8, 2, 0, 8],
       [6, 0, 0, 9, 8],
       [0, 0, 9, 9, 9],
       [3, 1, 6, 5, 4]])
>>> np.sort(a, axis=0)
array([[0, 0, 0, 0, 4],
       [3, 0, 2, 5, 8],
       [6, 1, 6, 9, 8],
       [7, 8, 9, 9, 9]])
>>> a = np.random.randint(0,10,size=(4,5))
>>> a
array([[5, 3, 8, 6, 6],
       [8, 8, 8, 7, 6],
       [8, 5, 3, 5, 7],
       [9, 6, 8, 0, 0]])
>>> np.median(a, axis=1)
array([ 6.,  8.,  5.,  6.])
>>> np.median(a, axis=0)
array([ 8. ,  5.5,  8. ,  5.5,  6. ])
>>> a = array( [20,30,40,50] )
Traceback (most recent call last):
  File "<pyshell#175>", line 1, in <module>
    a = array( [20,30,40,50] )
NameError: name 'array' is not defined
>>> a=np.array([20,30,40,50])
>>> b = arange( 4 )
Traceback (most recent call last):
  File "<pyshell#177>", line 1, in <module>
    b = arange( 4 )
NameError: name 'arange' is not defined
>>> b = np.arange( 4 )
>>> c=a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> a = np.ones((2,3), dtype=int)
>>> a *= 3
>>> a
array([[3, 3, 3],
       [3, 3, 3]])
>>> b = random.random((2,3))
Traceback (most recent call last):
  File "<pyshell#185>", line 1, in <module>
    b = random.random((2,3))
NameError: name 'random' is not defined
>>> b = np.random.random((2,3))
>>> b+=a
>>> b
array([[ 3.94,  3.16,  3.8 ],
       [ 3.81,  3.01,  3.49]])
>>> a = np.ones(3, dtype=int32)
Traceback (most recent call last):
  File "<pyshell#189>", line 1, in <module>
    a = np.ones(3, dtype=int32)
NameError: name 'int32' is not defined
>>> a = np.ones(3, dtype=int)
>>> a = np.ones(3, dtype=int16)
Traceback (most recent call last):
  File "<pyshell#191>", line 1, in <module>
    a = np.ones(3, dtype=int16)
NameError: name 'int16' is not defined
>>> b.dtype.name
'float64'
>>> c = a+b
>>> c
array([[ 4.94,  4.16,  4.8 ],
       [ 4.81,  4.01,  4.49]])
>>> c.dtype.name
'float64'
>>> from numpy import *
>>> a = mat("1 -2 1; 0 2 -8; -4 5 9")
>>> a
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  9]])
>>> b=array([[1,5],[3,2]])
>>> b
array([[1, 5],
       [3, 2]])
>>> x=matrix(b)
>>> x
matrix([[1, 5],
        [3, 2]])
>>> list=[1,2,3]
>>> mat(list)
matrix([[1, 2, 3]])
>>> mat( [ [1, 5, 10], [1.0, 3, 4j] ])
matrix([[  1.+0.j,   5.+0.j,  10.+0.j],
        [  1.+0.j,   3.+0.j,   0.+4.j]])
>>> a =
mat("1 -2 1;0 2 -8;-4 5 9")
SyntaxError: invalid syntax
>>> a =mat("1 -2 1;0 2 -8;-4 5 9")
>>> a
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  9]])
>>> a.T
matrix([[ 1,  0, -4],
        [-2,  2,  5],
        [ 1, -8,  9]])
>>> a = mat("1 -2 1;0 2 -8;-4 5 9")
>>> a
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  9]])
>>> a.H
matrix([[ 1,  0, -4],
        [-2,  2,  5],
        [ 1, -8,  9]])
>>> a = mat("1 -2 1;0 2 -8;-4 5 9")
>>> a
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  9]])
>>> a.I
matrix([[ 29. ,  11.5,   7. ],
        [ 16. ,   6.5,   4. ],
        [  4. ,   1.5,   1. ]])
>>> a = mat("1 -2 1;0 2 -8;-4 5 9")
>>> a
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  9]])
>>> a.A
array([[ 1, -2,  1],
       [ 0,  2, -8],
       [-4,  5,  9]])
>>> a = mat("1 -2 1 3;0 2 -8 6;-4 5 9 7")
>>> a
matrix([[ 1, -2,  1,  3],
        [ 0,  2, -8,  6],
        [-4,  5,  9,  7]])
>>> a.shape
(3, 4)
>>> a.shape[0]
3
>>> a.shape[1]
4
>>> a.sort()
>>> a
matrix([[-2,  1,  1,  3],
        [-8,  0,  2,  6],
        [-4,  5,  7,  9]])
>>> a.all()
False
>>> a.all(axis=0)
matrix([[ True, False,  True,  True]], dtype=bool)
>>> a.all(axis=1)
matrix([[ True],
        [False],
        [ True]], dtype=bool)
>>> a = mat('0 2 7 1; 3 4 8 3; 5 0 9 5')
>>> a
matrix([[0, 2, 7, 1],
        [3, 4, 8, 3],
        [5, 0, 9, 5]])
>>> a.astype(float)
matrix([[ 0.,  2.,  7.,  1.],
        [ 3.,  4.,  8.,  3.],
        [ 5.,  0.,  9.,  5.]])
>>> a = np.mat('0 2 7 1; 3 4 8 3; 5 0 9 5')
>>> a
matrix([[0, 2, 7, 1],
        [3, 4, 8, 3],
        [5, 0, 9, 5]])
>>> a.argsort()
matrix([[0, 3, 1, 2],
        [0, 3, 1, 2],
        [1, 0, 3, 2]], dtype=int32)
>>> a = np.mat('0 2 7 1; 3 4 8 3; 5 0 9 5')
>>> a
matrix([[0, 2, 7, 1],
        [3, 4, 8, 3],
        [5, 0, 9, 5]])
>>> a.clip(2,5)
matrix([[2, 2, 5, 2],
        [3, 4, 5, 3],
        [5, 2, 5, 5]])
>>> a = np.mat("1 -2 1;0 2 -8;-4 5 9")
>>> a
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  9]])
>>> a[0]
matrix([[ 1, -2,  1]])
>>> a[0,1]
-2
>>> a[1, 1:2]
matrix([[2]])
>>> a[2, 0:2]
matrix([[-4,  5]])
>>> import numpy as np
>>> a = [[1, 2, 1], [2, -1, 3], [3, 1, 2]]
>>> a = np.array(a)
>>> a
array([[ 1,  2,  1],
       [ 2, -1,  3],
       [ 3,  1,  2]])
>>> b = [7,7,18]
>>> b = np.array(b)
>>> b
array([ 7,  7, 18])
>>> x = np.linalg.solve(a,b)
>>> x
array([ 7.,  1., -2.])
>>> np.dot(a,x)
array([  7.,   7.,  18.])
>>> from numpy import *
>>> a = [[1, 2, 1], [2, -1, 3], [3, 1, 2]]
>>> a = np.mat(a)
>>> a
matrix([[ 1,  2,  1],
        [ 2, -1,  3],
        [ 3,  1,  2]])
>>> b = [7,7,18]
>>> b = np.mat(b)
>>> b
matrix([[ 7,  7, 18]])
>>> x = np.linalg.solve(a,b)
Traceback (most recent call last):
  File "<pyshell#261>", line 1, in <module>
    x = np.linalg.solve(a,b)
  File "C:\Python34\lib\site-packages\numpy\linalg\linalg.py", line 384, in solve
    r = gufunc(a, b, signature=signature, extobj=extobj)
ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 1 is different from 3)
>>> np.dot(a,x)
matrix([[  7.,   7.,  18.]])
>>>  x = linalg.solve(a,b)
 
SyntaxError: unexpected indent
>>> x = linalg.solve(a,b)
Traceback (most recent call last):
  File "<pyshell#264>", line 1, in <module>
    x = linalg.solve(a,b)
  File "C:\Python34\lib\site-packages\numpy\linalg\linalg.py", line 384, in solve
    r = gufunc(a, b, signature=signature, extobj=extobj)
ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 1 is different from 3)
>>> b=b.T
>>> b
matrix([[ 7],
        [ 7],
        [18]])
>>> x = linalg.solve(a,b)
>>> x
matrix([[ 7.],
        [ 1.],
        [-2.]])
>>> 

This snippet took 0.05 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).