最近很多小伙伴都在问Python-消除numpy数组或Pandas数据帧的每一行中的NaN值和numpy去掉a中一些数这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展"CALaye
最近很多小伙伴都在问Python - 消除 numpy 数组或 Pandas 数据帧的每一行中的 NaN 值和numpy去掉a中一些数这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展"CALayer position contains NaN: [nan nan]" 自定义滑块导致的错误信息、/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN、ffmpeg 管道从 numpy 数组或 bgr24 字节生成 h264 块、Numpy Rolling Window with Min Periods & Max (类似于 Pandas Rolling but Numpy)等相关知识,下面开始了哦!
本文目录一览:- Python - 消除 numpy 数组或 Pandas 数据帧的每一行中的 NaN 值(numpy去掉a中一些数)
- "CALayer position contains NaN: [nan nan]" 自定义滑块导致的错误信息
- /opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN
- ffmpeg 管道从 numpy 数组或 bgr24 字节生成 h264 块
- Numpy Rolling Window with Min Periods & Max (类似于 Pandas Rolling but Numpy)
Python - 消除 numpy 数组或 Pandas 数据帧的每一行中的 NaN 值(numpy去掉a中一些数)
如何解决Python - 消除 numpy 数组或 Pandas 数据帧的每一行中的 NaN 值
我有一个目前看起来像这样的熊猫数据框
|Eriksson| NaN | Boeser | NaN |
| NaN | McDavid| NaN | NaN |
| ... | ... | ... | ... |
我不在乎它是转换为 Numpy 数组还是仍然是数据帧,但我想要一个输出对象,其中的行仅包含非 NaN 值,如下所示:
|Eriksson| Boeser|
|McDavid | NaN |
(NaN
因为尺寸不匹配。)有没有办法做到这一点?
解决方法
我认为这对你有用:
df.apply(lambda x: pd.Series(x.dropna().values),axis=1)
示例:
>>> df = pd.DataFrame(np.random.randn(5,4))
>>> df.iloc[1,2] = np.NaN
>>> df.iloc[0,1] = np.NaN
>>> df.iloc[2,0] = np.NaN
>>> df
0 1 2 3
0 -0.162388 NaN -0.299892 0.594846
1 3.165631 -1.190102 NaN -1.234934
2 NaN NaN 0.885439 -1.714365
3 -1.622833 -1.319395 -1.716550 -0.517699
4 0.688479 0.576763 0.645344 0.708909
>>> df.apply(lambda x: pd.Series(x.dropna().values),axis=1)
0 1 2 3
0 -0.162388 -0.299892 0.594846 NaN
1 3.165631 -1.190102 -1.234934 NaN
2 0.885439 -1.714365 NaN NaN
3 -1.622833 -1.319395 -1.716550 -0.517699
4 0.688479 0.576763 0.645344 0.708909
"CALayer position contains NaN: [nan nan]" 自定义滑块导致的错误信息
如何解决"CALayer position contains NaN: [nan nan]" 自定义滑块导致的错误信息
我有一个视频播放器开始播放视频(来自 firebase URL),在某些情况下(70% 的情况)我在物理设备上运行时收到此错误消息(异常)(但在模拟器中启动时没有问题) : “CALayer 位置包含 NaN:[nan nan]”
我发现当我评论“VideoPlayerControlsView()”时没有出现错误,所以我很确定问题是我的 CustomerSlider 对象位于这个 VideoPlayerControlsView 视图的内部。
我认为这可能是由加载远程视频引起的,因为视频没有加载,应用程序不知道 AVPlayer 对象的大小/边界,因此无法创建一些父视图(可能是 CustomerSlider)。 .
构建一个最小的可重现示例将是一场噩梦,我只是希望有些人能在我的代码/逻辑中发现错误..如果没有 - 当然会构建它。别无选择。
struct DetailedplayerView : View {
// The progress through the video,as a percentage (from 0 to 1)
@State private var videoPos: Double = 0
// The duration of the video in seconds
@State private var videoDuration: Double = 0
// Whether we''re currently interacting with the seek bar or doing a seek
@State private var seeking = false
private var player: AVPlayer = AVPlayer()
init(item: ExerciseItem,hVideoURL: URL?) {
if hVideoURL != nil {
player = AVPlayer(url: hVideoURL!)
player.isMuted = true
player.play()
} else {
print("[debug] hVideoURL is nil")
}
}
var body: some View {
ZStack {
//vstack {
VideoPlayerView(videoPos: $videoPos,videoDuration: $videoDuration,seeking: $seeking,//timeline: $timeline,//videoTimeline: videoTimeline,player: player)
.frame(width: UIScreen.screenHeight,height: UIScreen.screenWidth)
vstack {
Spacer()
VideoPlayerControlsView(videoPos: $videoPos,**<<-----------------------**
videoDuration: $videoDuration,player: player)
.frame(width: UIScreen.screenHeight - 2*Constants.scrollPadding,height: 20)
.padding(.bottom,20)
}
}
.ondisappear {
// When this View isn''t being shown anymore stop the player
self.player.replaceCurrentItem(with: nil)
}
}
}
struct VideoPlayerControlsView : View {
@Binding private(set) var videoPos: Double
@Binding private(set) var videoDuration: Double
@Binding private(set) var seeking: Bool
// @Binding private(set) var timeline: [Advice]
@State var shouldStopPlayer: Bool = false
@State var player: AVPlayer
//let player: AVPlayer
@State private var playerPaused = false
var body: some View {
HStack {
// Play/pause button
Button(action: togglePlayPause) {
Image(systemName: playerPaused ? "arrowtriangle.right.fill" : "pause.fill")
.foregroundColor(Color.mainSubtitleColor)
.contentShape(Rectangle())
.padding(.trailing,10)
}
// Current video time
if videoPos.isFinite && videoPos.isCanonical && videoDuration.isFinite && videoDuration.isCanonical {
Text(Utility.formatSecondsToHMS(videoPos * videoDuration))
.foregroundColor(Color.mainSubtitleColor)
}
// Slider for seeking / showing video progress
CustomSlider(value: $videoPos,shouldStopPlayer: self.$shouldStopPlayer,range: (0,1),knobWidth: 4) { modifiers in
ZStack {
Group {
Color(#colorLiteral(red: 1,green: 1,blue: 1,alpha: 0.5799999833106995))//Color((red: 0.4,green: 0.3,blue: 1)
.opacity(0.4)
.frame(height: 4)
.modifier(modifiers.barRight)
Color.mainSubtitleColor//Color(red: 0.4,blue: 1)
.frame(height: 4)
.modifier(modifiers.barLeft)
}
.cornerRadius(5)
vstack {
Image(systemName: "arrowtriangle.down.fill") // SF Symbol
.foregroundColor(Color.mainSubtitleColor)
.offset(y: -3)
}
.frame(width: 20,height: 20)
.contentShape(Rectangle())
.modifier(modifiers.knob)
}
}
.onChange(of: shouldStopPlayer) { _ in
if shouldStopPlayer == false {
print("[debug] shouldStopPlayer == false")
sliderEditingChanged(editingStarted: false)
} else {
if seeking == false {
print("[debug] shouldStopPlayer == true")
sliderEditingChanged(editingStarted: true)
}
}
}
.frame(height: 20)
// Video duration
if videoDuration.isCanonical && videoDuration.isFinite {
Text(Utility.formatSecondsToHMS(videoDuration))
.foregroundColor(Color.mainSubtitleColor)
}
}
.padding(.leading,40)
.padding(.trailing,40)
}
private func togglePlayPause() {
pausePlayer(!playerPaused)
}
private func pausePlayer(_ pause: Bool) {
playerPaused = pause
if playerPaused {
player.pause()
}
else {
player.play()
}
}
private func sliderEditingChanged(editingStarted: Bool) {
if editingStarted {
// Set a flag stating that we''re seeking so the slider doesn''t
// get updated by the periodic time observer on the player
seeking = true
pausePlayer(true)
}
// Do the seek if we''re finished
if !editingStarted {
let targetTime = CMTime(seconds: videoPos * videoDuration,preferredTimescale: 600)
player.seek(to: targetTime) { _ in
// Now the seek is finished,resume normal operation
self.seeking = false
self.pausePlayer(false)
}
}
}
}
extension Double {
func convert(fromrange: (Double,Double),toRange: (Double,Double)) -> Double {
// Example: if self = 1,fromrange = (0,2),toRange = (10,12) -> solution = 11
var value = self
value -= fromrange.0
value /= Double(fromrange.1 - fromrange.0)
value *= toRange.1 - toRange.0
value += toRange.0
return value
}
}
struct CustomSliderComponents {
let barLeft: CustomSliderModifier
let barRight: CustomSliderModifier
let knob: CustomSliderModifier
}
struct CustomSliderModifier: ViewModifier {
enum Name {
case barLeft
case barRight
case knob
}
let name: Name
let size: CGSize
let offset: CGFloat
func body(content: Content) -> some View {
content
.frame(width: (size.width >= 0) ? size.width : 0)
.position(x: size.width*0.5,y: size.height*0.5)
.offset(x: offset)
}
}
struct CustomSlider<Component: View>: View {
@Binding var value: Double
var range: (Double,Double)
var knobWidth: CGFloat?
let viewbuilder: (CustomSliderComponents) -> Component
@Binding var shouldStopPlayer: Bool
init(value: Binding<Double>,shouldStopPlayer: Binding<Bool>,range: (Double,knobWidth: CGFloat? = nil,_ viewbuilder: @escaping (CustomSliderComponents) -> Component
) {
_value = value
_shouldStopPlayer = shouldStopPlayer
self.range = range
self.viewbuilder = viewbuilder
self.knobWidth = knobWidth
}
var body: some View {
return GeometryReader { geometry in
self.view(geometry: geometry) // function below
}
}
private func view(geometry: GeometryProxy) -> some View {
let frame = geometry.frame(in: .global)
let drag = DragGesture(minimumdistance: 0)
.onChanged { drag in
shouldStopPlayer = true
self.onDragChange(drag,frame)
}
.onEnded { drag in
shouldStopPlayer = false
//self.updatedValue = value
print("[debug] slider drag gesture ended,value = \\(value)")
}
let offsetX = self.getoffsetX(frame: frame)
let knobSize = CGSize(width: knobWidth ?? frame.height,height: frame.height)
let barLeftSize = CGSize(width: CGFloat(offsetX + knobSize.width * 0.5),height: frame.height)
let barRightSize = CGSize(width: frame.width - barLeftSize.width,height: frame.height)
let modifiers = CustomSliderComponents(
barLeft: CustomSliderModifier(name: .barLeft,size: barLeftSize,offset: 0),barRight: CustomSliderModifier(name: .barRight,size: barRightSize,offset: barLeftSize.width),knob: CustomSliderModifier(name: .knob,size: knobSize,offset: offsetX))
return ZStack { viewbuilder(modifiers).gesture(drag) }
}
private func onDragChange(_ drag: DragGesture.Value,_ frame: CGRect) {
let width = (knob: Double(knobWidth ?? frame.size.height),view: Double(frame.size.width))
let xrange = (min: Double(0),max: Double(width.view - width.knob))
var value = Double(drag.startLocation.x + drag.translation.width) // knob center x
value -= 0.5*width.knob // offset from center to leading edge of knob
value = value > xrange.max ? xrange.max : value // limit to leading edge
value = value < xrange.min ? xrange.min : value // limit to trailing edge
value = value.convert(fromrange: (xrange.min,xrange.max),toRange: range)
//print("[debug] slider drag gesture detected,value = \\(value)")
self.value = value
}
private func getoffsetX(frame: CGRect) -> CGFloat {
let width = (knob: knobWidth ?? frame.size.height,view: frame.size.width)
let xrange: (Double,Double) = (0,Double(width.view - width.knob))
let result = self.value.convert(fromrange: range,toRange: xrange)
return CGFloat(result)
}
}
一些额外的代码展示了如何触发DetailedplayerView:
struct DetailedVideo: View {
var item: ExerciseItem
var url: URL
@Binding var isPaused: Bool
var body: some View {
ZStack {
DetailedplayerView(item: self.item,hVideoURL: url)
//.frame(width: 500,height: 500) //@@UPDATED: Apr 10
HStack {
vstack {
ZStack {
//Rectangle 126
RoundedRectangle(cornerRadius: 1)
.fill(Color(#colorLiteral(red: 0.3063802123069763,green: 0.3063802123069763,blue: 0.3063802123069763,alpha: 1)))
.frame(width: 2,height: 20.3)
.rotationEffect(.degrees(-135))
//Rectangle 125
RoundedRectangle(cornerRadius: 1)
.fill(Color(#colorLiteral(red: 0.3063802123069763,height: 20.3)
.rotationEffect(.degrees(-45))
}
.frame(width: 35,height: 35)//14.4
.contentShape(Rectangle())
.onTapGesture {
print("[debugUI] isPaused = false")
self.isPaused = false
}
.offset(x:20,y:20)
Spacer()
}
Spacer()
}
}
.ignoresSafeArea(.all)
}
}
@viewbuilder
var detailedVideoView: some View {
if self.hVideoURL != nil {
DetailedVideo(item: self.exerciseVM.exerciseItems[self.exerciseVM.currentIndex],url: self.hVideoURL!,isPaused: self.$exerciseVM.isPaused) // when is paused - we are playing detailed video?
.frame(width: UIScreen.screenHeight,height: UIScreen.screenWidth) //UPDATED: Apr 9,2021
.onAppear {
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue,forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
.ondisappear {
dispatchQueue.main.async {
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
} else {
EmptyView()
}
}
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN
这里记录一下犯过的及其傻帽的错误!!!!哈哈,无语,同时讨论一下NaN这个数据类型的处理
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: FutureWarning: elementwise comparison Failed; returning scalar instead,but in the future will perform elementwise comparison result = getattr(x,name)(y)
....................
TypeError: invalid type comparison
这里有一个优惠券的scv表:
import numpy as np
import pandas as pd
dfoff = pd.read_csv("datalab/4901/ccf_offline_stage1_train.csv")
dfofftest = pd.read_csv("datalab/4901/ccf_offline_stage1_test_revised.csv")
dfoff.head()
笔者这里的目的是想统计出 Coupon_id是非NaN(非空)且Date是NaN(空)的用户数(行数)
----------------------------------------------------------------------------------------------------------------------------------------------------------------
一般来说比如我们想筛选出 discount_rate是20:1且distance不是1.0的行数可以这么做:
dfoff.info()
print('数目是:',dfoff[(dfoff['discount_rate']=='20:1')&(dfoff['Date']!=1.0)].shape[0])
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
于是笔者这样做了筛选:
dfoff.info()
print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id']!='NaN')&(dfoff['Date']=='NaN')].shape[0])
结果报错:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1754884 entries,0 to 1754883
Data columns (total 7 columns):
User_id int64
Merchant_id int64
Coupon_id float64
discount_rate object
distance float64
Date_received float64
Date float64
dtypes: float64(4),int64(2),object(1)
memory usage: 93.7+ MB
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: FutureWarning: elementwise comparison Failed; returning scalar instead,but in the future will perform elementwise comparison
result = getattr(x,name)(y)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-c27c94978405> in <module>()
1 dfoff.info()
----> 2 print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id']!='NaN')&(dfoff['Date']=='NaN')].shape[0])
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in wrapper(self,other,axis)
877
878 with np.errstate(all='ignore'):
--> 879 res = na_op(values,other)
880 if is_scalar(res):
881 raise TypeError('Could not compare {typ} type with Series'
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x,y)
816 result = getattr(x,name)(y)
817 if result is NotImplemented:
--> 818 raise TypeError("invalid type comparison")
819 except AttributeError:
820 result = op(x,y)
TypeError: invalid type comparison
其实吧原因很简单,注意看上面笔者故意标红的地方,Coupon_id 和Date的数据类型都是float64,而代码中却用了dfoff['Coupon_id']!='NaN',这不是字符串嘛!!!!!!
print(type('NaN'))
<class 'str'>
float和str比较当然报错了是吧,哎!能这样直接去比较我也算是极品啦哈哈哈
于是可以使用其内置的方法解决:
dfoff.info()
print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id'].notnull())&(dfoff['Date'].isnull())].shape[0])
即使用了如下两个方法
.notnull()
.isnull()
其作用就是判断是否是空值,如果csv中的NaN的地方换成null同样适用
同时这里说一下怎么将NaN替换掉:例如替换成0.0
dfoff['Coupon_id']=dfoff['Coupon_id'].replace(np.nan,0.0)
-----------------------------------------------------------------------------------------------------------------------------------------------------------
下面来说一下NaN这个数据类型,它的全称应该是not a number,说到这里不得不提到另外一个数据类型inf
相同点:都是代表一个无法表示的数
不同点:inf代表无穷大,是一个超过浮点表示范围的浮点数,而NaN可以看成是缺少值或者是无理数
假设现在有一段程序:
def ConvertRate(row):
if row.isnull():
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
会发现报错:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-0aa06185ee75> in <module>()
7 else:
8 return float(row)
----> 9 dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
10 print(dfoff.head(3))
/opt/conda/lib/python3.6/site-packages/pandas/core/series.py in apply(self,func,convert_dtype,args,**kwds)
2549 else:
2550 values = self.asobject
-> 2551 mapped = lib.map_infer(values,f,convert=convert_dtype)
2552
2553 if len(mapped) and isinstance(mapped[0],Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-3-0aa06185ee75> in ConvertRate(row)
1 def ConvertRate(row):
----> 2 if row.isnull():
3 return 0
4 elif ':' in str(row):
5 rows = str(row).split(':')
AttributeError: 'float' object has no attribute 'isnull'
那它到底是什么数据类型呢?
print(type(np.nan))
print(type(np.inf))
<class 'float'>
<class 'float'>
NaN'就是表示一个普通的字符串,而np.nan就是代表真真的nan,那我们可不可以使用这样:
def ConvertRate(row):
if row==np.nan:
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
User_id Merchant_id Coupon_id discount_rate distance Date_received \
0 1439408 2632 NaN NaN 0.0 NaN
1 1439408 4663 11002.0 150:20 1.0 20160528.0
2 1439408 2632 8591.0 20:1 0.0 20160217.0
Date discount_rate
0 20160217.0 NaN
1 NaN 0.866667
2 NaN 0.950000
可以看到这里还是NaN,并不是0,说明还是不对
那试一下:
def ConvertRate(row):
if row==float('NaN'):
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
结果还是如上面,其实NaN数据类型就是一种特殊的float,这里相当于强制类型转化
那到底怎么办呢?其实判断是否是NaN可以使用如下方法:
row!=row
如果结果是真,那么就是NaN,假就代表不是NaN
可以看一下结果:
def ConvertRate(row):
if row!=row:
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
print(dfoff.head(3))
User_id Merchant_id Coupon_id discount_rate distance Date_received \
0 1439408 2632 NaN NaN 0.0 NaN
1 1439408 4663 11002.0 150:20 1.0 20160528.0
2 1439408 2632 8591.0 20:1 0.0 20160217.0
Date discount_rate
0 20160217.0 0.000000
1 NaN 0.866667
2 NaN 0.950000
于是笔者最开始的那个问题也可以这样解决:
print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id']==dfoff['Coupon_id'])&(dfoff['Date']!=dfoff['Date'])].shape[0])
有优惠券,但是没有使用优惠券购买的客户有 977900
---------------------------------------------------------------------------------------------------------------------------------------------------------------
有时候在使用apply的时候会报错,所以最好加一下:axis = 1意思是按列处理的
对应到上面就是吧:
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
改为:
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate,axis = 1)
------------------------------------------------------------------------------------------------------------------------------------------------------------
所以最后总结一下:
NaN和inf都是一种特殊的float数据类型
可以使用row!=row类似的形式来判断是否是NaN,如果是真就代表是NaN,假就代表不是NaN,换句话说也可以使用row==row来判断是否是NaN,只不过逻辑相反而已
报错记得加axis = 1
------------------------------------------------------------------------------------------------------------------------------------------------------
在使用pands加载数据的时候,其实我们是可以控制数据类型的,比如让缺省值变为null,而不是NAN,即让字段的数据类型不再是float,而是object,这里有一个例子:https://blog.csdn.net/weixin_42001089/article/details/85013073
ffmpeg 管道从 numpy 数组或 bgr24 字节生成 h264 块
如何解决ffmpeg 管道从 numpy 数组或 bgr24 字节生成 h264 块
如果有人可以帮助我使用 FFmpeg 管道从 NumPy 数组生成 h264 块,是否有可能
这就是我现在正在做的:
过程 = ( ffmpeg .input(''pipe:'',format=''rawvideo'',pix_fmt=''bgr24'',s=''{}x{}''.format(self._deeptwin.image_width,self._deeptwin.image_height)) .output(path,pix_fmt=''yuv420p'',vcodec=''libx264'',r=25) .overwrite_output() .run_async(pipe_stdin=True) )
for res in response:
st = datetime.datetime.Now()
print(f''{res.status} image {res.id} rendered at {datetime.datetime.Now()}'')
img = np.frombuffer(res.image,dtype=np.uint8)
img = img.reshape((int(self._deeptwin.image_height * 1.5),self._deeptwin.image_width))
img = cv2.cvtColor(img,cv2.COLOR_YUV2BGR_I420)
for frame in img:
start = datetime.datetime.Now()
process.stdin.write(
frame
.astype(np.uint8)
.tobytes()
)
end = datetime.datetime.Now()
stop = datetime.datetime.Now()
print(f''{res.id} conversion done at {stop-st}'')
process.stdin.close()
process.wait()
` 目前正在做的是生成一个 out.h264 文件。没问题,也能播放正确的视频。
但是,我需要实现的是:我想以下列方式生成h264帧块:1.h264,2.h264............n.h264
任何指导都会非常感谢和帮助。
提前致谢。
Numpy Rolling Window with Min Periods & Max (类似于 Pandas Rolling but Numpy)
如何解决Numpy Rolling Window with Min Periods & Max (类似于 Pandas Rolling but Numpy)
我正在寻找(无法在任何地方看到已经覆盖的内容)使用 numpy 而不是 pandas.rolling(主要是为了速度)创建一个滑动窗口。 但是,滑动窗口也必须是窗口中最小和最大实例数的函数,当无法构造窗口时返回NaN。这类似于为窗口大小(最大值)和 min_periods 设置参数的 pandas.rolling。例如:
设置 Min_periods = 3 和 Max_periods = 7,请参见下面的预期窗口示例:
index values intended_window
0 10 np.nan
1 11 np.nan
2 12 [10,11,12]
3 13 [10,12,13]
4 14 [10,13,14]
5 15 [10,14,15]
6 16 [10,15,16]
7 17 [11,16,17]
8 18 [12,17,18]
9 19 [13,18,19]
我看到了当滑动窗口没有最大值或最小值要求时如何构造此滑动窗口的示例,例如
def rolling_window(a,window):
shp = a.shape[:-1] + (a.shape[-1] - window + 1,window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a,shape=shp,strides=strides)
有谁知道我可以如何扩展它以返回上面示例中的窗口?
解决方法
请尝试以下操作。
,
def dataframe_striding(dataframe,window):
''''''
Parameters
----------
dataframe : Input Dataframe,in this case df with columns [''index'',''values''] present.
window : Tuple denoting the window size.
Returns
-------
dataframe : Pandas Dataframe
''''''
lower_end,upper_end = window
if lower_end > upper_end:
raise ValueError(''Check window size!'')
results = []
for i in range(dataframe.shape[0]):
l = [k for k in dataframe[''values''][:i+1]]
if len(l) < lower_end: # checks for minimum window length
l = np.nan
results.append(l)
elif lower_end <= len(l) <= upper_end: # checks for required window length
results.append(l)
else: # checks for maximum window length
l = l[-upper_end:]
results.append(l)
dataframe[''rolling_output''] = results # appends output to input dataframe
return dataframe
# run above function #
final_df = dataframe_striding(df,window = (4,6))
values = np.linspace(1,10,num=10)
window_column = []
for i in range(len(values)):
if i - 7 < 0:
t = 0
else:
t = i - 7
window = values[t:i]
if len(window) < 3:
window_column.append(np.nan)
else:
window_column.append(window)
我们今天的关于Python - 消除 numpy 数组或 Pandas 数据帧的每一行中的 NaN 值和numpy去掉a中一些数的分享已经告一段落,感谢您的关注,如果您想了解更多关于"CALayer position contains NaN: [nan nan]" 自定义滑块导致的错误信息、/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN、ffmpeg 管道从 numpy 数组或 bgr24 字节生成 h264 块、Numpy Rolling Window with Min Periods & Max (类似于 Pandas Rolling but Numpy)的相关信息,请在本站查询。
本文标签: