Python常用包学习(三)——scipy

scipy主要是用于处理科学计算中的常见问题:插值、积分、优化、图像处理、统计、特殊函数等;
scipy主要是与numpy合作使用,有效的处理numpy中的数据;

下文只是一个简介,帮助大家了解一下这个包,如果想深入了解,可以查看如下页面:

https://www.yiibai.com/scipy/scipy_input_output.html

#!/usr/bin/env python
# _*_ UTF-8 _*_

import scipy 
import numpy as np
'''1、IO操作:'''
from scipy import io as spio

# 数据的读入和读出:
# a = np.ones((3,3))
# spio.savemat('file.mat', {'a':a})
# data = spio.loadmat('file.mat', struct_as_record=True)
# print(data['a'])
# 图片读取:
# data = misc.imread('111.png')
# print(data.shape)
# 导入txt文件:
# np.loadtxt()
# np.savetxt()
# 智能导入txt、csv::
# np.genfromtxt()
# np.recfromcsv()
# 高速存储数据:
# np.save()
# np.load()
'''2、特殊函数:'''
from scipy import special
# 贝塞尔函数:
# special.jn()
# 椭圆函数:
# special.ellipj()
'''3、线性代数运算:'''
from scipy import linalg
# 主要用到scipy.linalg:
# 行列式运算:
# arr = np.array([[1,2],[3,4]])
# print(linalg.det(arr))
# 求逆矩阵:
# iarr = linalg.inv(arr)
# print(iarr)
# 奇异值分解:
# arr = np.arange(9).reshape((3,3))+np.diag([1,0,1])
# print(arr)
# uarr, spec, vharr = linalg.svd(arr)
# print(uarr)
# print(spec)
# print(vharr)
# 通过svd的运算数据获取到原始数据矩阵:
# sarr = np.diag(spec)
# svd_mat = uarr.dot(sarr).dot(vharr)
# print(svd_mat)
# 快速傅里叶变换:
# time_step = 0.02
# period = 5
# time_vec = np.arange(0, 20, time_step)
# sig = np.sin(2*np.pi/period*time_vec)+0.5*np.random.randn(time_vec.size)
# print(sig)
'''4、优化和拟合:'''

# 主要是根据一些函数来确定最优质,或者通过数据来确认函数的一些参数:
# 找到最小值或等式的数值解的问题
from scipy import optimize
import matplotlib.pyplot as plt
# def f(x):
#     return x**2 + 10*np.sin(x)
# x = np.arange(-10, 10, 0.1)
# plt.plot(x, f(x))
# plt.show()
# 找到最优值:0为初始点
# print(optimize.fmin_bfgs(f, 0, disp=0))
# 如果不知道初始值改怎么设置,则可以给定一个范围grid,然后蛮力运算所有范围内的值:
# grid = (-10, 10, 0.1)
# xmin_global = optimize.brute(f, (grid,))
# print(xmin_global)
# 找到局部最小值(0,10)范围内:
# xmin_local = optimize.fminbound(f, 0, 10)
# print(xmin_local)
# 找到f(x)=0的根:2为初始值
# root = optimize.fsolve(f, 2)
# print(root)
# 曲线拟合:
# 最小二乘法来进行拟合:
# xdata = np.linspace(-10, 10, num=20)
# ydata = f(xdata)+np.random.randn(xdata.size)
# def f2(x, a, b):
#     return a*x**2 + b*np.sin(x)
# guess = [2,2]  # 初始值
# params, params_covariance = optimize.curve_fit(f2, xdata, ydata, guess)
# print(params)
# print(params_covariance)
'''5、统计与随机数'''
from scipy import stats
# 直方图与概率密度函数:
# a = np.random.normal(size=1000)
# bins = np.arange(-4, 5)
# # print(bins)
# histogram = np.histogram(a, bins=bins, normed=True)[0]
# bins = 0.5*(bins[1:]+bins[:-1])
# print(bins)
# b = stats.norm.pdf(bins)
# plt.plot(bins, histogram)
# plt.plot(bins, b)
# plt.show()
# 求中位数(也叫做50百分位点):
# a = np.random.normal(size=1000)
# print(np.median(a))
# 计算90百分位点:
# print(stats.scoreatpercentile(a, 90))
# b = np.random.normal(1,1,size=10)
# T检验来决定两个样本是否显著性差异:
# print(stats.ttest_ind(a,b))
# pvalue:是指两个过程相同的概率,越接近0,两个过程越可能有不同的均值;越接近1,这两个过程几乎完全一样;
# statistic:与两个随机过程之间成比例,并且幅度和差异之间显著程度有关。
'''6、插值:'''

# 从各个样本点穿过,即将各个点串联在一起
from scipy import interpolate as iplt
# 原始的值:
# x = np.linspace(0, 4, 12)
# y = np.cos(x**2/3+4)
# print(x)
# print(y)
# plt.plot(x, y, 'o')
# plt.show()
# f1 = iplt.interp1d(x, y, kind='cubic')
# f2 = iplt.interp1d(x, y, kind='cubic')
# 新的值插入到x,y中,然后创建新的函数f1;
# xnew = np.linspace(0, 4, 300)
# 在xy的基础上插入新的值,然后创建新的函数f1
# plt.plot(x, y, 'o', xnew, f1(xnew)) # , '-', xnew, f2(xnew), '--'
# plt.legend(['data','linear','cubic','nearest'], loc='best')
# plt.show()

'''7、积分计算:'''
from scipy.integrate import quad
# res, err = quad(np.sin, 0, np.pi/2)
# res:积分值;
# err:积分值绝对误差估计值;

'''8、图像处理'''
from scipy import ndimage
from scipy import misc
from scipy.misc import imread, imsave, imresize
from scipy.spatial.distance import pdist, squareform, cdist
# 图像的几何变换:
# img = imread("111.png")
# img_type = img.dtype
# print(img_type)
# img_shape = img.shape
# print(img_shape) # [高, 宽, 通道]
# img_tint = img*[1, 0.9, 0.9]
# img_resize = imresize(img, (5, 5))
# print(type(img_resize))
# l1 = np.array([1,2,3,4])
# l2 = np.array([3,47,8,10])
# np.savetxt('1111',(l1, l2))
# print(img_resize)
# imsave("111_color.png", img_resize)
# np.savetxt("1111.txt", img_resize)

'''9、计算两点之间的距离:'''
# x1 = np.array([[1,1]])
# x2 = np.array([[4,5]])
# distance = cdist(x1, x2, "euclidean")
# print(distance)

猜你喜欢

转载自blog.csdn.net/livan1234/article/details/81486530