如何解决在配方模型的配方中仅选择有序因子
我需要使用tidymodels的食谱包创建食谱。在其中一个步骤中,我需要将有序因子转换为它们的序数得分。但是我似乎没有任何功能 使用以选择所有有序因子。
我知道有一个名为all_nominal()
的函数,但该函数与作为因子的每一列匹配,可以有序或无序。我也尝试过has_type("ordered")
,但这也不起作用。
下面是我想做的一个例子:
library(mlbench)
data("BreastCancer")
rec <- recipe(Class ~ .,BreastCancer) %>%
# Here,I want to select all ordered nominals instead of
# listing them by name. Should there be a function
# all_ordinal in recipes? Or is there another way
# to accomplish this?
step_ordinalscore(Cl.thickness,Cell.size,Cell.shape,Marg.adhesion,Epith.c.size)
欢迎您的帮助,谢谢。
解决方法
没有用于顺序因子的特殊选择器功能,但是您可以自己找到它们,然后将名称向量用于import heartpy as hp
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from collections import deque
from matplotlib.ticker import FuncFormatter
#from . import config
sample_rate = 40
def init(): # initialization function: plot the background of each frame
line.set_ydata([np.nan]*len(x))
line1.set_ydata([np.nan]*len(x1))
# scatter2.set_xdata([np.nan]*len(x2))
# scatter3.set_ydata([np.nan]*len(x3))
# scatter4.set_ydata([np.nan]*len(x4))
# scatter5.set_ydata([np.nan]*len(y5))
scat.set_offsets([]*len(x))
return line,line1,scat,#scatter5,#scatter4,scatter5
def animate(i):
read_fname = 'temp.1D'
datax = np.loadtxt('temp.1D')
wd,m = hp.process(datax,40 ) #run analysis
hr = wd['hr']
rm = wd['rolling_mean']
yb = wd['ybeat']
pl = wd['peaklist']
rp = wd['removed_beats']
rp_y = wd['removed_beats_y']
#pl=ax.collections[pl].get_array()
yhr = np.array(hr[:])
yrm = np.array(rm[:])
yyb = np.array(yb[:])
ypl = np.array(pl[:])
yrp = np.array(rp[:])
yrp_y = np.array(rp_y[:])
yhr = np.concatenate([yhr])
yrm = np.concatenate([yrm])
yyb = np.concatenate([yyb])
ypl = np.concatenate([ypl])
yrp = np.concatenate([rp])
yrp_y = np.concatenate([rp_y])
data.extend(yhr)
data1.extend(yrm)
data2.extend(ypl)
# data3.extend(yrp)
# data4.extend(yrp_y)
data5.extend(yyb)
#data25.extend(ypl,yyb)
line.set_ydata(data)
line1.set_ydata(data1)
# scatter2.set_xdata(data2)
# scatter3.set_ydata(data3)
# scatter4.set_ydata(data4)
# scatter5.set_ydata(data5)
data25 =(np.hstack((ypl[:i,np.newaxis],yyb[:i,np.newaxis])))
scat.set_offsets(data25)
print(yhr)
print(yrm)
print(ypl)
return line,scatter5
max_x = 500
max_y = 1700
#max_x2 = 1000
#max_y5 = 1000
data = deque(np.zeros(max_x),maxlen=max_x)
data1 = deque(np.zeros(max_x),maxlen=max_x)
data2= deque(np.zeros(max_x),maxlen=max_x)
#data3 = deque(np.zeros(max_x),maxlen=max_x)
#data4 = deque(np.zeros(max_x),maxlen=max_x) #hold the last 10 values
data5 = deque(np.zeros(max_y),maxlen=max_y)
data25 = deque(np.zeros(max_x),maxlen=max_x)
x = np.arange(0,max_x)
x1 = np.arange(0,max_x)
x2 = np.arange(0,max_x)
#x3 = np.arange(0,max_x)
#x4 = np.arange(0,max_x)
y5 = np.arange(0,max_y)
fig,ax = plt.subplots()
#fig,ax1 = plt.subplots()
ax.set_ylim(-500,max_y)
ax.set_xlim(0,max_x-1)
line,= ax.plot(x,np.linspace(0,1000,max_x,endpoint = False))
line1,= ax.plot(x1,endpoint = False))
scat = ax.scatter([],[],)
#ax = plt.gca()
#scatter2,= ax.plot(x2,marker="o",ls="",color = "red")
#scatter3,= ax.scatter(x3,50,1000))
#scatter4 = ax.scatter(x4,1000))
#scatter5,= ax.plot(y5,color = "green")
#ax.grid()
#ax.relim()
#ax.autoscale_view(True,True,True)
ax.xaxis.set_major_formatter(FuncFormatter(lambda x,pos: '{:.0}s'.format(max_x -x -990)))
plt.xlabel('Seconds ago')
ani = animation.FuncAnimation(fig,animate,init_func=init,interval=25,blit=True,save_count = 10)
plt.show()
。
step_ordinalscore()
由reprex package(v0.3.0.9001)于2020-10-19创建
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。