我想在我的iOS上使用Swift 3处理从麦克风读取的字节.我目前使用的是AVAudioEngine.
print(inputNode.inputFormat(forBus: bus).settings)
print(inputNode.inputFormat(forBus: bus).formatDescription)
Run Code Online (Sandbox Code Playgroud)
这给了我以下输出:
["AVNumberOfChannelsKey": 1, "AVLinearPCMBitDepthKey": 32, "AVSampleRateKey": 16000, "AVLinearPCMIsNonInterleaved": 1, "AVLinearPCMIsBigEndianKey": 0, "AVFormatIDKey": 1819304813, "AVLinearPCMIsFloatKey": 1]
<CMAudioFormatDescription 0x14d5bbb0 [0x3a5fb7d8]> {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
mSampleRate: 16000.000000
mFormatID: 'lpcm'
mFormatFlags: 0x29
mBytesPerPacket: 4
mFramesPerPacket: 1
mBytesPerFrame: 4
mChannelsPerFrame: 1
mBitsPerChannel: 32 }
cookie: {(null)}
ACL: {(null)}
FormatList Array: {(null)}
}
extensions: {(null)}
}
Run Code Online (Sandbox Code Playgroud)
问题是我想要发送数据的服务器不期望32位浮点数而是16位无符号整数.我想我必须改变mFormatFlags.有谁知道我怎么能这样做,什么价值是正确的?
生成的字节流应该等同于我在android上使用的字节流
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLES_PER_SECOND,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
recordSegmentSizeBytes);
Run Code Online (Sandbox Code Playgroud)
我试过这个:
let cfmt = AVAudioCommonFormat.pcmFormatInt16
inputNode.inputFormat(forBus: bus) = AVAudioFormat(commonFormat: cfmt, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个小的python脚本来测试一些东西.后来我想用它来创建gnuplot的资源使用情节,但首先是几个测试.
脚本看起来像
import subprocess
result = subprocess.check_output("top -b -n 1 -c", shell=True).split("\n")
head = result[:5]
body = [x for x in result[7:] if x] #removes empty strings
for line in head:
print line
csum = 0.0
for line in body:
print line
csum += float(line.split()[8])
print "CPU usage of all processes added up", csum, "%"
Run Code Online (Sandbox Code Playgroud)
多次运行几乎总是导致CPU使用率> 100%.有时甚至> 200%.怎么会这样?
它运行在具有两个核心的虚拟机(virtualbox,ubuntu 14.04 64位)中.主机也有两个核心.
所有正在运行的进程的使用值总和不应该总是低于100%吗?我在同一时间运行htop,这显示了每个核心大约50%的负载....
可能问题是,某些进程可能启动了其他进程,并且两者都显示在top的输出中,而父进程也显示了子进程的cpu使用情况?==>孩子被计算两次?
该文件说,继承UIAlertController是坏
UIAlertController类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不能修改。
那么,推荐一种不仅显示标题,消息和某些按钮,还显示其他东西(例如ProgressBars,List等)的警报的方法是什么?
在我的特殊情况下,我希望有两种不同的警报,一种显示进度条,另一种显示错误消息列表。
目前,我正在尝试手动添加ProgressView并设置约束:
func getProgressAlert(onAbort: @escaping () -> ()) -> UIAlertController {
let alert = UIAlertController(title: "Test", message: "Test", preferredStyle: .alert)
let abort = UIAlertAction (title: "Abort", style: UIAlertActionStyle.cancel) { _ in
onAbort()
}
alert.addAction(abort)
let margin:CGFloat = 8.0
let rect = CGRect(x:margin, y:72.0, width: alert.view.frame.width - margin * 2.0 , height:2.0)
self.progressView = UIProgressView(frame: rect)
self.progressView!.setProgress(0.0, animated: false)
self.progressView!.tintColor = UIColor.blue
alert.view.addSubview(self.progressView!)
self.progressView!.translatesAutoresizingMaskIntoConstraints = false
self.progressView!.widthAnchor.constraint(equalTo: alert.view.widthAnchor, multiplier: 1.0).isActive = true
self.progressView!.heightAnchor.constraint(equalToConstant: 5.0).isActive = true …Run Code Online (Sandbox Code Playgroud) 我尝试将句子映射到向量,以使句子相互比较。为了测试 gensim 的 Doc2Vec 模型,我下载了 sklearn 的新闻组数据集并在其上训练模型。
为了比较两个句子,我使用 model.infer_vector() 并且我想知道为什么使用同一句子的两个调用会为我提供不同的向量:
model = Doc2Vec(vector_size=100, window=8, min_count=5, workers=6)
model.build_vocab(documents)
epochs=10
for epoch in range(epochs):
print("Training epoch %d" % (epoch+1))
model.train(documents, total_examples=len(documents), epochs=epochs)
v1 = model.infer_vector("I feel good")
v2 = model.infer_vector("I feel good")
print(np.linalg.norm(v1-v2))
Run Code Online (Sandbox Code Playgroud)
输出:
训练时期 1
0.41606528
训练时期 2
0.43440753
训练纪元 3
0.3203116
训练纪元 4
0.3039317
训练纪元 5
0.68224543
训练时期 6
0.5862567
训练纪元 7
0.5424634
训练纪元 8
0.7618142
训练纪元 9
0.8170159
训练纪元 10
0.6028216
如果我设置 alpha 和 min_alpha = 0,我会得到“我感觉很好”和“我感觉很好”的一致向量,但模型在每个时期都给我相同的向量,所以它似乎没有学到任何东西:
训练时期 1 …
我正在使用 gensim 的 doc2vec 实现,并且我有几千个标有四个标签的文档。
yield TaggedDocument(text_tokens, [labels])
Run Code Online (Sandbox Code Playgroud)
我正在训练一个带有这些TaggedDocument列表的 Doc2Vec 模型。但是,我不确定如何推断训练期间未看到的文档的标签。我看到有一个 infer_vector 方法可以返回嵌入向量。但是我怎样才能从中得到最有可能的标签呢?
一个想法是推断我拥有的每个标签的向量,然后计算这些向量与我想要分类的新文档的向量之间的余弦相似度。这是要走的路吗?如果是这样,我怎样才能获得四个标签中每一个的向量?
我正在参加泰坦尼克号 Kaggle 比赛,目前正在尝试估算缺失Age值。
这个想法是计算训练集上每组的Age平均值,然后使用该信息来替换训练集和测试集。[Pclass, Sex]NaN
这是我到目前为止所拥有的:
meanAgeTrain = train.groupby(['Pclass', 'Sex'])['Age'].transform('mean')
for df in [train, test]:
df['Age'] = df['Age'].fillna(meanAgeTrain)
Run Code Online (Sandbox Code Playgroud)
问题是,这仍然在测试集中留下了一些 NaN 值,同时消除了训练集中的所有 Nan。我认为这与指数有关。
我需要的是:
如何使用 Pandas 正确完成此操作?
编辑:
感谢您的建议。@Reza 的那个可以工作,但我不能 100% 理解它。所以我正在尝试提出自己的解决方案。
这是可行的,但我是 Pandas 新手,想知道是否有更简单的方法来实现它。
trainMeans = self.train.groupby(['Pclass', 'Sex'])['Age'].mean().reset_index()
def f(x):
if x["Age"] == x["Age"]: # not NaN
return x["Age"]
return trainMeans.loc[(trainMeans["Pclass"] == x["Pclass"]) & (trainMeans["Sex"] == x["Sex"])]["Age"].values[0]
self.train['Age'] = self.train.apply(f, axis=1)
self.test['Age'] = …Run Code Online (Sandbox Code Playgroud)