基础练习

  1. 字符串创建数字矩阵
from numpy import *
A = mat('1 2 3; 4 5 6; 7 8 9')
print("Creating from string: \n", A)
BaseTestMat1
  1. numpy数组创建数字矩阵
mat(arange(16).reshape(2,8))
BaseTestMat2
  1. 插值函数
    插值可通过函数在有限个点处的取值状况,估算出函数在其他点处的近似值。
    是的插值模块
class scipy.interpolate.interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
# 这是一个类,用于完成一维数据的插值运算。

BaseTest_Interp_1

# -*-coding:utf-8 -*-
import numpy as np
from scipy import interpolate
import pylab as pl

x=np.linspace(0,10,11)
#x=[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.]
y=np.sin(x)
xnew=np.linspace(0,10,101)

pl.plot(x,y,"ro") 
'''
r为红色,b为蓝色,g为绿色,默认蓝色
o打印出圆点
*打印出星星
默认为直线,或者“-”
'''
# 按顺序打印出各种插值的结果在同一张图上
for kind in ["nearest","zero","slinear","quadratic","cubic"]:# 各种插值类型
    f=interpolate.interp1d(x,y,kind=kind)
    ynew=f(xnew)
    pl.plot(xnew,ynew,label=str(kind))
'''
    nearest, zero 阶梯插值
    slinear 线性插值
    quadratic 2阶样条曲线插值
    cubic 3阶样条曲线插值
    ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of first, second or third order)
'''
pl.legend(loc="lower right")
pl.show()
BaseTest_Interp_1 RedPoint

BaseTest_Interp_1 对上图进行几种不同类型的插值

你可能感兴趣的:(基础练习)