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

42 篇文章 0 订阅

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()

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()

• 2
点赞
• 24
收藏
觉得还不错? 一键收藏
• 1
评论
01-14
05-13
11-26 1万+

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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