python数据分析 numpy库的学习(2)

  这篇介绍对numpy中的数据如何进行具体的数学操作,既包括普通的四则运算,也有通用函数运算,以及一些基本的线性代数操作。
  
#最基本的数组运算,和标量进行四则运算
arr=np.array([[1,2,3],[4,5,6],[7,8,8]])
'''print(arr*2)
print(arr/3)
print(arr+3) #每一项都会和标量进行四则运算'''

#数组与数组之间的运算
#普通四则运算,不同型的数组是不能进行下列运算的,详见线性代数的操作
'''print(arr+arr)
print(arr-arr)
print(arr/arr)
#两个同型的数组之间是可以进行布尔运算的,会生成一个同型的布尔矩阵
print(arr>=arr)
#不同数据类型的数组进行操作之后会转化为更精细的类型,int+float会得出float。
#下面这个操作会直接修改数组本身而不是生成一个新数组
arr +=2
print(arr)

#下面介绍基本的线性代数运算
print(arr*arr) #这还不是线代乘法,只是对应位置数据相乘'''
a=np.array([1,2,3])
'''print(np.diag(a,k=0)) #让一个一维数组拉长成一个n阶阵,原元素分布在对角,其他值为0

print(np.dot(arr,arr)) #矩阵乘法
print(np.trace(arr))  #计算矩阵的迹,也就是对角线元素和

#linalg是numpy中专门用来操作矩阵的子程序包
print(np.linalg.det(arr))  #求矩阵行列式的值
print(np.linalg.inv(arr)) #求矩阵的逆,对于奇异矩阵或者未满秩矩阵都会报错'''

#求解线性方程组,这个函数输出的数组就是该线性方程组的解向量
#print(np.linalg.solve(arr,a)) 

#求解方程的特征向量
'''print(np.linalg.eigvals(arr))  #这个返回的是按列排列的特征值,转置了一下
print(np.linalg.eig(arr)) #这个返回的第一个数组是特征值,后面的三个数组是一一对应的特征向量'''



#python通用化的函数我就列个表表示一下,作为工具参考。下面写几个例子展示一下格式。
'''print(np.square(arr))
print(np.mod(arr,2)) #求余数后也是以同型矩阵的格式输出
#不分一元二元格式都是一样的'''
methoddescription
sin(x, /[, out, where, casting, order, …]) 正弦函数, element-wise.
cos(x, /[, out, where, casting, order, …]) 余弦函数 element-wise.
tan(x, /[, out, where, casting, order, …]) 正切函数, element-wise.
arcsin(x, /[, out, where, casting, order, …]) 反正弦函数, element-wise.
arccos(x, /[, out, where, casting, order, …]) 反余弦函数, element-wise.
arctan(x, /[, out, where, casting, order, …]) 反正切函数, element-wise.
hypot(x1, x2, /[, out, where, casting, …]) 传入直角三角形的“直角边”,返回其斜边。
arctan2(x1, x2, /[, out, where, casting, …]) x1 / x2的 Element-wise 反正切线正确选择象限。
degrees(x, /[, out, where, casting, order, …]) 将角度从弧度转换为度。
radians(x, /[, out, where, casting, order, …]) 将角度从度转换为弧度。
unwrap(p[, discont, axis]) 通过将值之间的增量更改为2 * pi来展开。
deg2rad(x, /[, out, where, casting, order, …]) 将角度从度转换为弧度。
rad2deg(x, /[, out, where, casting, order, …]) 将角度从弧度转换为度。
methoddescription
signbit(x, /[, out, where, casting, order, …]) Returns element-wise True where signbit is set (less than zero).
copysign(x1, x2, /[, out, where, casting, …]) Change the sign of x1 to that of x2, element-wise.
frexp(x[, out1, out2], / [[, out, where, …]) Decompose the elements of x into mantissa and twos exponent.
ldexp(x1, x2, /[, out, where, casting, …]) Returns x1 * 2**x2, element-wise.
nextafter(x1, x2, /[, out, where, casting, …]) Return the next floating-point value after x1 towards x2, element-wise.
spacing(x, /[, out, where, casting, order, …]) Return the distance between x and the nearest adjacent number
methoddescription
add(x1, x2, /[, out, where, casting, order, …]) Add arguments element-wise.
reciprocal(x, /[, out, where, casting, …]) Return the reciprocal of the argument, element-wise.
positive(x, /[, out, where, casting, order, …]) Numerical positive, element-wise.
negative(x, /[, out, where, casting, order, …]) Numerical negative, element-wise.
multiply(x1, x2, /[, out, where, casting, …]) Multiply arguments element-wise.
divide(x1, x2, /[, out, where, casting, …]) Returns a true division of the inputs, element-wise.
power(x1, x2, /[, out, where, casting, …]) First array elements raised to powers from second array, element-wise.
subtract(x1, x2, /[, out, where, casting, …]) Subtract arguments, element-wise.
true_divide(x1, x2, /[, out, where, …]) Returns a true division of the inputs, element-wise.
floor_divide(x1, x2, /[, out, where, …]) Return the largest integer smaller or equal to the division of the inputs.
float_power(x1, x2, /[, out, where, …]) First array elements raised to powers from second array, element-wise.
fmod(x1, x2, /[, out, where, casting, …]) Return the element-wise remainder of division.
mod(x1, x2, /[, out, where, casting, order, …]) Return element-wise remainder of division.
modf(x[, out1, out2], / [[, out, where, …]) Return the fractional and integral parts of an array, element-wise.
remainder(x1, x2, /[, out, where, casting, …]) Return element-wise remainder of division.
divmod(x1, x2[, out1, out2], / [[, out, …]) Return element-wise quotient and remainder simultaneously.

methoddescription
angle(z[, deg]) Return the angle of the complex argument.
real(val) Return the real part of the complex argument.
imag(val) Return the imaginary part of the complex argument.
conj(x, /[, out, where, casting, order, …]) Return the complex conjugate, element-wise.
conjugate(x, /[, out, where, casting, …]) Return the complex conjugate, element-wise

  

#下面再介绍一个操作,是数组中的数据进行排序
acf=np.array([[100,2,34],[34,23,56],[17892,234,1]])
print(np.sort(acf,axis=0))#这个函数返回的还是源数组,0表示按行排,1表示按列排

print(np.argsort(acf,axis=0)) #这个函数返回的是每一列的索引数组
print(np.lexsort(acf)) #这个函数返回的是最后一行的索引数组,从小到大

 

posted on 2020-03-17 22:27  张达铃  阅读(169)  评论(0)    收藏  举报

导航