小编Gad*_*Gad的帖子

python中的isinstance(对象,类型)

我只是通过一些python帮助文档,并遇到了以下代码:

isinstance(object, type)
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释上述陈述中的类型意味着什么?

谢谢,Vineel

python python-3.x

10
推荐指数
1
解决办法
4万
查看次数

scikit-learn 管道中的 TargetEncoder(“来自category_encoders”)导致“GridSearchCV”索引错误

我正在对数据集中的某些功能使用目标编码。我的完整管道是这样的:

from sklearn.compose import ColumnTransformer

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler

from category_encoders.target_encoder import TargetEncoder

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split

numeric_features = ['feature_1']
numeric_pipeline = Pipeline(steps=[('scaler', StandardScaler())])

ohe_features = ['feature_2', 'feature_3', 'feature_4']
ohe_pipeline = Pipeline(steps=[('ohe', OneHotEncoder())])

te_features = ['feature_5', 'feature_6']
te_pipeline = TargetEncoder()

preprocessor = ColumnTransformer(transformers=[
                                ('numeric', numeric_pipeline, numeric_features), 
                                ('ohe_features', ohe_pipeline, ohe_features), 
                                ('te_features', te_pipeline, te_features)
                                ]
               )

clf_lr = Pipeline(steps=[
                 ('preprocessor', preprocessor), 
                 ('classifier', LogisticRegression())
                 ]
         )

X_train, X_test, y_train, y_test = …
Run Code Online (Sandbox Code Playgroud)

python-3.x pandas scikit-learn

5
推荐指数
0
解决办法
3080
查看次数

如何在两个值之间随机选择?

所以基本上我试图让一段代码在两个值 -40 和 40 之间随机选择。

为此,我正在考虑使用古老的数学,例如 -

random_num = ((-1)^value)*40,其中 value = {1, 2}。

random_num,顾名思义应该是一个随机数。

有什么帮助吗?

我正在使用 python,使用库的解决方案是可以接受的。

python numpy

4
推荐指数
2
解决办法
2万
查看次数

Python中有没有办法像JS中那样编写像lambda这样的对象

JS中,我可以这样定义一个对象:

foo = {"foo": 42, "bar": function(){/* do something */}}
Run Code Online (Sandbox Code Playgroud)

有没有办法在Python中做同样的事情?

javascript python

4
推荐指数
2
解决办法
89
查看次数

如何在UICollectionView中处理后台点击

我有一个UICollectionView在我的应用程序,我想处理它的背景点击做一些很酷的东西,但我尝试了几个解决方案,他们都没有太好.

我试过的事情:

  • 在后面添加背景视图UICollectionViewCells并处理其上的点击
  • 添加UITapGestureRecognizerUICollectionView视图(与其collectionView属性相同)

两种情况下的问题是,虽然它可以完美地处理背景的点击,但是当它处于a时它也处理点击UICollectionViewCell,所以当用户选择一个项目时,但是在这种情况下它不应该因为这两个事物有不同的动作在我的应用程序中

iphone objective-c ios uicollectionview ios7

3
推荐指数
1
解决办法
2481
查看次数

没有音频的视频会导致 AVMutableComposition() 上的应用程序崩溃

我的应用程序从 URL 获取视频,并允许您向其中添加文本等。当视频没有任何音频开始时,它似乎崩溃了,似乎无法弄清楚这一点。

这是我在制作视频时所拥有的:

let asset = AVAsset(url: URL(string: self.videoURL)!)
let mixComposition = AVMutableComposition()
let videoTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
try! videoTrack?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: asset.duration), of: asset.tracks(withMediaType: .video)[0], at: CMTime.zero)
let audioTrack = mixComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
       try audioTrack!.insertTimeRange(CMTimeRangeMake(start: .zero, duration: asset.duration), of: asset.tracks(withMediaType: .audio)[0], at: CMTime.zero)
   } catch {
       print("error")
   }
Run Code Online (Sandbox Code Playgroud)

它继续insertTimeRangeindexPath超出范围。

 [__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray
Run Code Online (Sandbox Code Playgroud)

avfoundation avmutablecomposition avasset swift

2
推荐指数
1
解决办法
528
查看次数