introduction to python for statistics,analysis笔记2

一、行列式连接concatenate函数,axis=0是垂直拼接,axis=1是水平拼接

1 x=np.array([[1.0,2],[3,4]]);
2 y=np.array([[5.0,6],[7,8]]);
3 z=np.concatenate((x,y),axis=0);
4 print x,'

'
5 print y,'

'
6 
7 print z
[[ 1.  2.]
 [ 3.  4.]] 


[[ 5.  6.]
 [ 7.  8.]] 


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

另一种方法有:

z = vstack((x,y)) # Same as z = concatenate((x,y),axis = 0)

z = hstack((x,y)) # Same as z = concatenate((x,y),axis = 1)

二、行列式选取可能降维

>>> x = array([[1.0,2],[3,4]])
>>> x[:1,:] # Row 1, all columns, 2-dimensional
array([[ 1., 2.]])
>>> x[0,:] # Row 1, all columns, dimension reduced
array([ 1., 2.])


2、查看行列式的维数使用,np.ndim(A)函数

3、重复扩展,相当于在方括号内元素扩展

>>> x = array([[0.0]*3]*3) # *3 repeats the list 3 times
>>> x
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])

4、修改行列式元素值时,需要注意类型。可能会被修改,numpy中,会自动将插入的数据类型修改为行列式类型

>>> x = [0, 1, 2, 3, 4] # Integers
>>> y = array(x)
>>> y.dtype
dtype(’int32’)
>>> y[0] = 3.141592
>>> y
array([3, 1, 2, 3, 4])

5、行列式的转置和重改维度大小

B=A.T#行列式转置

B=np.reshape(A,(m,n)),#m,n是要更后改的行数和列数

6、flat切片,他将行列式转换成一维行列式,进行切片

a=np.arange(10);
b=np.reshape(a,(2,5));
b.flat[2:7]
array([2, 3, 4, 5, 6])

7、行列式复制三种方法(1)copy()

import copy as cp
a=np.arange(10);
b=np.reshape(a,(2,5));
x1=cp.deepcopy(b[0]);#函数法
x2=b[:,0].copy();#方法
x3=np.array(b[0]);#array行列式

print b
print b[0];
print x1
print x2
print x3,' '

x1[0]=12;
print b
print b[0];
print x1
print x2
print x3,' '

x2[0]=20;
print b
print b[0];
print x1
print x2
print x3,' '

x3[0]=50
x2[0]=20;
print b
print b[0];
print x1
print x2
print x3,' '

[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4]
[0 1 2 3 4]
[0 5]
[0 1 2 3 4] 



[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4]
[12  1  2  3  4]
[0 5]
[0 1 2 3 4] 



[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4]
[12  1  2  3  4]
[20  5]
[0 1 2 3 4] 



[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4]
[12  1  2  3  4]
[20  5]
[50  1  2  3  4] 

8、导入模块

import pylab as pl
import scipy as sp
import numpy as np

 9、数组运算

(1)加减运算都符合线性代数运算

(2)乘运算要注意数组的广播机制,还不太理解???????????

一般的:数组运算是元素与元素相乘,矩阵相乘是符合线性代数运算机制的

想要改变数组的运算机制符合线性代数运算机制可以通过函数np.dot(A,B)

x=np.ones((2,4));
y=np.ones((4,2));
print np.dot(x,y)

[[ 4.  4.]
 [ 4.  4.]]

(3)除运算,数组和矩阵都是元素与元素之间的运算。

x=np.ones((2,4))*10;
y=np.ones((2,4))*2;
x/y

array([[ 5.,  5.,  5.,  5.],
       [ 5.,  5.,  5.,  5.]])
x=np.mat(np.ones((2,4))*10)
y=np.mat(np.ones((2,4))*2)
x/y

matrix([[ 5.,  5.,  5.,  5.],
        [ 5.,  5.,  5.,  5.]])

(4)指数运算

对于数组是元素的指数运算

x=np.array([1,2,3]);
x**2

array([1, 4, 9])

对于矩阵是Z=A**m,是矩阵A碟乘m次,对于m在python中必须是整数,可以是负数,如果是负数,则Z=inv(A**(abs(m)));

x=np.mat(np.reshape(np.arange(9),(3,3)))
print x
x**2

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

matrix([[ 15,  18,  21],
        [ 42,  54,  66],
        [ 69,  90, 111]])

(5)矩阵转置运算A.transpose(),transpose(A),A.T都表示矩阵A的转置

x=np.mat(np.random.randn(2,2))
print x;
x.T*x

[[-1.02490428  1.76366191]
 [ 0.48344916 -0.36091476]]

matrix([[ 1.28415188, -1.98206858],
        [-1.98206858,  3.2407628 ]])

 10、数组切片的不同

当使用切片或标量进行赋值时,不会复制内容。而是产生一个视图。当使用索引或逻辑值进行赋值时,会对原始数组进行复制。

a=np.arange(10);
​
b=a[1:5];
c = a[[1,2,3,4]]
print a
print b
b[0]=100;
print a
print b
​
print c
​
[0 1 2 3 4 5 6 7 8 9]
[1 2 3 4]
[  0 100   2   3   4   5   6   7   8   9]
[100   2   3   4]
[1 2 3 4]
原文地址:https://www.cnblogs.com/zhaopengcheng/p/5399250.html