Python从excel读取数据,并使用scipy进行散点的平滑曲线化方法

首先给出一个没有smooth过的曲线

import xlrd
import numpy as np
import matplotlib.pyplot as plt 
from scipy.interpolate import spline  
workbook = xlrd.open_workbook("/Users/lm/Documents/实验一/算法对比.xlsx")
sheet_01 = workbook.sheets()[0]

dataset = []
for i in range(0, sheet_01.nrows):
    dataset.append(sheet_01.row_values(i))  
dataset=np.array(dataset)  #将list转换为numpy.ndarray
X=dataset[:1,:].ravel()
y=dataset[1:5,]
plt.figure(1, figsize=(8, 4))
i=0
plt.xlim((0,140))
plt.ylim((0,0.50))
new_xticks = np.linspace(0, 150, 16)
plt.xticks(new_xticks)
new_yticks = np.linspace(0, 0.5, 11)
plt.yticks(new_yticks)
#xnew = np.linspace(X.min(),X.max(),3000) #3000 represents number of points to make between T.min and T.max 
labels=['Fisher','NB','Decision Tree','Logistic Regression']
while i<len(y):
    #power_smooth = spline(X,y[i],xnew)  
    plt.plot(X,y[i], linewidth=0.8,label=labels[i])
    i=i+1


#从第二个sheet中绘制点
sheet_02 = workbook.sheets()[1]
dataset_02 = []
i=0
for i in range(0, sheet_02.nrows):
    dataset_02.append(sheet_02.row_values(i))  
dataset_02 = np.array(dataset_02)  #将list转换为numpy.ndarray
scatter_x = np.linspace(10,140,14)
scatter_y = dataset_02[1:,]
i=0
while i<len(scatter_y):
    plt.scatter(scatter_x,scatter_y[i],s=15,marker='.')
    i=i+1
plt.xlabel('number of feature')
plt.ylabel('error in classification')
plt.legend(loc='best')
plt.savefig('/Users/lm/Documents/分类误差_无曲线拟合.png', dpi=200)
plt.show()

输出的曲线如下图


使用scipy库可以进行曲线的smooth

代码如下

import xlrd
import numpy as np
import matplotlib.pyplot as plt 
from scipy.interpolate import spline  
workbook = xlrd.open_workbook("/Users/lm/Documents/实验一/算法对比.xlsx")
sheet_01 = workbook.sheets()[0]

dataset = []
for i in range(0, sheet_01.nrows):
    dataset.append(sheet_01.row_values(i))  
dataset=np.array(dataset)  #将list转换为numpy.ndarray
X=dataset[:1,:].ravel()
y=dataset[1:5,]
plt.figure(1, figsize=(8, 4))
i=0
plt.xlim((0,140))
plt.ylim((0,0.50))
new_xticks = np.linspace(0, 150, 16)
plt.xticks(new_xticks)
new_yticks = np.linspace(0, 0.5, 11)
plt.yticks(new_yticks)
xnew = np.linspace(X.min(),X.max(),3000) #3000 represents number of points to make between T.min and T.max 
labels=['Fisher','NB','Decision Tree','Logistic Regression']
while i<len(y):
    power_smooth = spline(X,y[i],xnew)  
    li,=plt.plot(xnew,power_smooth, linewidth=0.8,label=labels[i])
    i=i+1


#从第二个sheet中绘制点
sheet_02 = workbook.sheets()[1]
dataset_02 = []
i=0
for i in range(0, sheet_02.nrows):
    dataset_02.append(sheet_02.row_values(i))  
dataset_02 = np.array(dataset_02)  #将list转换为numpy.ndarray
scatter_x = np.linspace(10,140,14)
scatter_y = dataset_02[1:,]
i=0
while i<len(scatter_y):
    plt.scatter(scatter_x,scatter_y[i],s=15,marker='.')
    i=i+1
plt.legend(loc='best')
plt.xlabel('number of feature')
plt.ylabel('error in classification')
plt.savefig('/Users/lm/Documents/分类误差_曲线拟合.png', dpi=200)
plt.show()

输出的图片为


所用数据:点击该链接    密码:ruy8


  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值