您的位置:首页 > 编程语言 > Python开发

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

2018-02-23 15:21 2679 查看
首先给出一个没有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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: