Demo entry 6366003

12633

   

Submitted by anonymous on May 22, 2017 at 11:14
Language: Python 3. Code size: 21.5 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.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.dt
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    a.dt
AttributeError: 'numpy.ndarray' object has no attribute 'dt'
>>> 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 = 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 = 4,3
>>> c
array([[ 1,  2,  3],
       [ 4,  4,  5],
       [ 6,  7,  7],
       [ 8,  9, 10]])
>>> 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]])
>>> a = np.array([1,2,3,4])
>>> a
array([1, 2, 3, 4])
>>> d = a.reshape(2,2)
>>> a
array([1, 2, 3, 4])
>>> 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)) #创建元素全为“1”的数组
>>> a
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> b = np.eye(2) #创建对角元素为“1”的数组
>>> b
array([[ 1.,  0.],
       [ 0.,  1.]])
>>> vstack((a,b))
Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    vstack((a,b))
NameError: name 'vstack' is not defined
>>> vstack((a,b))
Traceback (most recent call last):
  File "<pyshell#62>", line 1, in <module>
    vstack((a,b))
NameError: name 'vstack' is not defined
>>> import numpy
>>> 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.]])
>>> a = np.ones((2,2))
>>> 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.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> a[4] #用整数作为下标获取数组中某个元素
4
>>> a[1:3] #用切片作为下标获取数组一部分
array([1, 2])
>>> a[:3] #切片省略开始下标,表示从a[0]开始
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] #省略切片的开始下标和结束下标,步长为-1,整个数组头尾颠倒
array([4, 3, 2, 1, 0])
>>> a[5:1:-2] #步长为负数,开始下标必须大于结束下标
array([4, 2])
>>> a = np.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> 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
>>> b
array([1, 2])
>>> b[1] = -10 #修改b 的第2 个元素
KeyboardInterrupt
>>> b[1] = -10 #修改b的第2个元素
>>> 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]] #获取x 中下标为3,3,1,8 的4 个元素,组成一个新的数组
>>> a
array([7, 7, 9, 2])
>>> b = x[[3, 3, -3, 8]] #下标是负数,表示取倒数第3 个元素
>>> 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([5, 4, 1])
>>> x>5
array([False, False, False], dtype=bool)
>>> x[x>5]
array([], dtype=int32)
>>> from numpy import random as nr
>>> np.set_printoptions(precision=2)#显示小数后两位
>>> r1 = nr.rand(4, 3)
>>> r1
array([[ 0.51,  0.94,  0.02],
       [ 0.39,  0.1 ,  0.67],
       [ 0.8 ,  0.26,  0.62],
       [ 0.91,  0.99,  0.32]])
>>> from numpy import random as nr
>>> np.set_printoptions(precision=2)
>>> r2 = nr.randn(4, 3)
>>> r2
array([[ 1.78, -1.51,  1.15],
       [-1.35, -0.98,  0.15],
       [ 1.23, -0.37,  2.13],
       [-0.2 ,  0.64, -0.29]])
>>> from numpy import random as nr
>>> np.set_printoptions(precision=2)
>>> r3 = nr.randint(0, 10, (4, 3))
>>> r3
array([[9, 9, 1],
       [8, 3, 6],
       [5, 8, 5],
       [8, 2, 2]])
>>> r3 = nr.randint(0, 10, (4, 3))
>>> r3
array([[5, 8, 1],
       [1, 3, 1],
       [2, 7, 9],
       [7, 1, 4]])
>>> np.sum(r3)
49
>>> np.sum(r3, axis = 1)
array([14,  5, 18, 12])
>>> np.sum(r3, axis = 0)
array([15, 19, 15])
>>> r3 = nr.randint(0, 10, (4, 3))
>>> r3
array([[7, 6, 7],
       [0, 1, 7],
       [1, 2, 4],
       [0, 8, 4]])
>>> np.mean(r3)
3.9166666666666665
>>> 
KeyboardInterrupt
>>> 
KeyboardInterrupt
>>> np.mean(r3,axis=1)
array([ 6.67,  2.67,  2.33,  4.  ])
>>> np.mean(r3,axis=0)
array([ 2.  ,  4.25,  5.5 ])
>>> a = np.random.randint(0,10,size=(4,5))
>>> a
array([[1, 1, 9, 4, 4],
       [9, 1, 7, 5, 0],
       [5, 1, 5, 6, 0],
       [7, 8, 4, 9, 9]])
>>> np.sort(a) #对a 中每行的数值进行排序
array([[1, 1, 4, 4, 9],
       [0, 1, 5, 7, 9],
       [0, 1, 5, 5, 6],
       [4, 7, 8, 9, 9]])
>>> a = np.random.randint(0,10,size=(4,5))
>>> a
array([[8, 9, 5, 2, 0],
       [5, 6, 8, 5, 9],
       [5, 2, 4, 1, 9],
       [4, 0, 5, 9, 9]])
>>> np.sort(a, axis=0) #对a 中每列值进行排序
array([[4, 0, 4, 1, 0],
       [5, 2, 5, 2, 9],
       [5, 6, 5, 5, 9],
       [8, 9, 8, 9, 9]])
>>> a = np.random.randint(0,10,size=(4,5))
>>> a
array([[7, 4, 2, 4, 7],
       [2, 5, 1, 1, 7],
       [3, 8, 0, 0, 9],
       [6, 2, 9, 5, 8]])
>>> np.median(a, axis=1) #按照行取中值
array([ 4.,  2.,  3.,  6.])
>>> np.median(a, axis=0) #按照列取中值
array([ 4.5,  4.5,  1.5,  2.5,  7.5])
>>> a = array( [20,30,40,50] )
Traceback (most recent call last):
  File "<pyshell#171>", line 1, in <module>
    a = array( [20,30,40,50] )
NameError: name 'array' is not defined
>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> b
array([0, 1, 2, 3])
>>> c = a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> a = ones((2,3), dtype=int)
Traceback (most recent call last):
  File "<pyshell#178>", line 1, in <module>
    a = ones((2,3), dtype=int)
NameError: name 'ones' is not defined
>>> 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#182>", 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.14,  3.62,  3.06],
       [ 3.66,  3.34,  3.98]])
>>> a = np.ones(3, dtype=int32)
Traceback (most recent call last):
  File "<pyshell#186>", line 1, in <module>
    a = np.ones(3, dtype=int32)
NameError: name 'int32' is not defined
>>> a = np.ones(3, np.dtype=int32)
SyntaxError: keyword can't be an expression
>>> a = np.ones(3, dtype=int32)
Traceback (most recent call last):
  File "<pyshell#188>", line 1, in <module>
    a = np.ones(3, dtype=int32)
NameError: name 'int32' is not defined
>>> a = np.ones(3, dtype=int 32)
SyntaxError: invalid syntax
>>> a = np.ones(3, dtype=32)
Traceback (most recent call last):
  File "<pyshell#190>", line 1, in <module>
    a = np.ones(3, dtype=32)
  File "C:\Python34\lib\site-packages\numpy\core\numeric.py", line 183, in ones
    a = empty(shape, dtype, order)
TypeError: data type not understood
>>> a = np.ones(3, dtype=int (32))
Traceback (most recent call last):
  File "<pyshell#191>", line 1, in <module>
    a = np.ones(3, dtype=int (32))
  File "C:\Python34\lib\site-packages\numpy\core\numeric.py", line 183, in ones
    a = empty(shape, dtype, order)
TypeError: data type not understood
>>> a = np.ones(3, dtype=int)
>>> b = linspace(0,pi,3)
Traceback (most recent call last):
  File "<pyshell#193>", line 1, in <module>
    b = linspace(0,pi,3)
NameError: name 'linspace' is not defined
>>> b = np.linspace(0,pi,3)
Traceback (most recent call last):
  File "<pyshell#194>", line 1, in <module>
    b = np.linspace(0,pi,3)
NameError: name 'pi' is not defined
>>> 
>>> import math
>>> b = linspace(0,pi,3)
Traceback (most recent call last):
  File "<pyshell#197>", line 1, in <module>
    b = linspace(0,pi,3)
NameError: name 'linspace' is not defined
>>> b = np.linspace(0,pi,3)
Traceback (most recent call last):
  File "<pyshell#198>", line 1, in <module>
    b = np.linspace(0,pi,3)
NameError: name 'pi' is not defined
>>> import math as np
>>> b = np.linspace(0,pi,3)
Traceback (most recent call last):
  File "<pyshell#200>", line 1, in <module>
    b = np.linspace(0,pi,3)
AttributeError: 'module' object has no attribute 'linspace'
>>> b.dtype.name
'float64'
>>> c = a+b
>>> c
array([[ 4.14,  4.62,  4.06],
       [ 4.66,  4.34,  4.98]])
>>> c.dtype.name
'float64'
>>> from numpy import *
>>> a = np.mat("1 -2 1; 0 2 -8; -4 5 9")
Traceback (most recent call last):
  File "<pyshell#206>", line 1, in <module>
    a = np.mat("1 -2 1; 0 2 -8; -4 5 9")
AttributeError: 'module' object has no attribute 'mat'
>>> a = np.matrix("1 -2 1; 0 2 -8; -4 5 9")
Traceback (most recent call last):
  File "<pyshell#207>", line 1, in <module>
    a = np.matrix("1 -2 1; 0 2 -8; -4 5 9")
AttributeError: 'module' object has no attribute 'matrix'
>>> 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=np.matrix(b) #mat 是matrix 的别名
Traceback (most recent call last):
  File "<pyshell#212>", line 1, in <module>
    x=np.matrix(b) #mat 是matrix 的别名
AttributeError: 'module' object has no attribute 'matrix'
>>> x=matrix(b) #mat 是matrix 的别名
>>> 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 = np.mat("1 -2 1;0 2 -8;-4 5 9")
Traceback (most recent call last):
  File "<pyshell#218>", line 1, in <module>
    a = np.mat("1 -2 1;0 2 -8;-4 5 9")
AttributeError: 'module' object has no attribute 'mat'
>>> 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 = np.mat("1 -2 1;0 2 -8;-4 5 9")
Traceback (most recent call last):
  File "<pyshell#222>", line 1, in <module>
    a = np.mat("1 -2 1;0 2 -8;-4 5 9")
AttributeError: 'module' object has no attribute 'mat'
>>> a
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  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 = np.mat("1 -2 1;0 2 -8;-4 5 9")
Traceback (most recent call last):
  File "<pyshell#230>", line 1, in <module>
    a = np.mat("1 -2 1;0 2 -8;-4 5 9")
AttributeError: 'module' object has no attribute 'mat'
a
>>> 
>>> 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 = np.mat("1 -2 1 3;0 2 -8 6;-4 5 9 7")
Traceback (most recent call last):
  File "<pyshell#235>", line 1, in <module>
    a = np.mat("1 -2 1 3;0 2 -8 6;-4 5 9 7")
AttributeError: 'module' object has no attribute 'mat'
>>> a
matrix([[ 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')
Traceback (most recent call last):
  File "<pyshell#251>", line 1, in <module>
    a = np.mat('0 2 7 1; 3 4 8 3; 5 0 9 5')
AttributeError: 'module' object has no attribute 'mat'
>>> 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.argsort()
matrix([[0, 3, 1, 2],
        [0, 3, 1, 2],
        [1, 0, 3, 2]], dtype=int32)
>>> 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.clip(2,5)
matrix([[2, 2, 5, 2],
        [3, 4, 5, 3],
        [5, 2, 5, 5]])
>>> a = 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#281>", 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)
>>> x = linalg.solve(a,b)
Traceback (most recent call last):
  File "<pyshell#282>", 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)
>>> import numpy as np
>>> x = np.linalg.solve(a,b)
Traceback (most recent call last):
  File "<pyshell#284>", 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)
>>> import linalg from numpy
SyntaxError: invalid syntax
>>> np.dot(a,x)
matrix([[  7.,   7.,  18.]])
>>> x = np.linalg.solve(a, b)
Traceback (most recent call last):
  File "<pyshell#287>", 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)
>>> import numpy as np
>>> ================================ RESTART ================================
>>> from numpy import *
>>> a = [[1, 2, 1], [2, -1, 3], [3, 1, 2]]
>>> a = mat(a)
>>> a
matrix([[ 1,  2,  1],
        [ 2, -1,  3],
        [ 3,  1,  2]])
>>> b = [7,7,18]
>>> b= mat(b)
>>> b
matrix([[ 7,  7, 18]])
>>> x=np.linalg.solve(a,b)
Traceback (most recent call last):
  File "<pyshell#296>", line 1, in <module>
    x=np.linalg.solve(a,b)
NameError: name 'np' is not defined
>>> x=linalg.solve(a,b)
Traceback (most recent call last):
  File "<pyshell#297>", 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)
>>> import numpy as np
>>> a
matrix([[ 1,  2,  1],
        [ 2, -1,  3],
        [ 3,  1,  2]])
>>> b
matrix([[ 7,  7, 18]])
>>> x=np.linalg.solve(a,b)
Traceback (most recent call last):
  File "<pyshell#301>", 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)
>>> b = [[7,7,18],[0,0,0],[0,0,0]]
>>> b
[[7, 7, 18], [0, 0, 0], [0, 0, 0]]
>>> b= mat(b)
>>> b
matrix([[ 7,  7, 18],
        [ 0,  0,  0],
        [ 0,  0,  0]])
>>>  x=np.linalg.solve(a,b)
 
SyntaxError: unexpected indent
>>> x=np.linalg.solve(a,b)
>>> x
matrix([[-3.5, -3.5, -9. ],
        [ 3.5,  3.5,  9. ],
        [ 3.5,  3.5,  9. ]])
>>> b = b.T
>>> b
matrix([[ 7,  0,  0],
        [ 7,  0,  0],
        [18,  0,  0]])
>>> b = [7,7,18]
>>> b.T
Traceback (most recent call last):
  File "<pyshell#312>", line 1, in <module>
    b.T
AttributeError: 'list' object has no attribute 'T'
>>> b=mat(b)
>>> b= b.T
>>> b
matrix([[ 7],
        [ 7],
        [18]])
>>> x=np.linalg.solve(a,b)
>>> x
matrix([[ 7.],
        [ 1.],
        [-2.]])
>>> 

This snippet took 0.07 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).