我试图估计我的 keras 模型的预测时间并意识到一些奇怪的事情。除了正常情况下相当快之外,模型每隔一段时间需要很长时间才能做出预测。不仅如此,这些时间也会随着模型运行的时间而增加。我添加了一个最小的工作示例来重现错误。
import time
import numpy as np
from sklearn.datasets import make_classification
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
# Make a dummy classification problem
X, y = make_classification()
# Make a dummy model
model = Sequential()
model.add(Dense(10, activation='relu',name='input',input_shape=(X.shape[1],)))
model.add(Dense(2, activation='softmax',name='predictions'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X, y, verbose=0, batch_size=20, epochs=100)
for i in range(1000):
# Pick a random sample
sample = np.expand_dims(X[np.random.randint(99), :], axis=0)
# Record the prediction time 10x and then take the average
start = time.time() …Run Code Online (Sandbox Code Playgroud) 我最近发现自己不得不在没有互联网连接的机器上安装 python 项目的所有依赖项(20+)。我曾经pip download ...获取所有*.whl文件并手动传输它们。直到现在我才完全欣赏 pip 的天才,以及它如何自行计算依赖树并设法以正确的顺序安装每个包。例如,包依赖于requests包,而包本身又依赖于urllib3包等等。
我想要一种使用命令控制台或 python 本身在机器上安装所有这些依赖项的自动化方式,所以我转向 StackOverflow 并找到了这些解决方案: 如何在 cmd 中安装多个 whl 文件
几乎所有建议的解决方案都对我有用,但缺点是必须多次运行它们,直到不再安装失败!这是由于脚本/命令按字母顺序对软件包进行排序并尝试按该顺序安装它们(例如,在到位requests之前尝试安装urllib3)。
有没有更聪明的方法来做到这一点,只按时执行脚本/命令?
我想VotingClassifier用多个不同的模型(决策树、SVC 和 Keras 网络)构建一个 sklearn集成。它们都需要不同类型的数据预处理,这就是我为它们每个都制作了管道的原因。
# Define pipelines
# DTC pipeline
featuriser = Featuriser()
dtc = DecisionTreeClassifier()
dtc_pipe = Pipeline([('featuriser',featuriser),('dtc',dtc)])
# SVC pipeline
scaler = TimeSeriesScalerMeanVariance(kind='constant')
flattener = Flattener()
svc = SVC(C = 100, gamma = 0.001, kernel='rbf')
svc_pipe = Pipeline([('scaler', scaler),('flattener', flattener), ('svc', svc)])
# Keras pipeline
cnn = KerasClassifier(build_fn=get_model())
cnn_pipe = Pipeline([('scaler',scaler),('cnn',cnn)])
# Make an ensemble
ensemble = VotingClassifier(estimators=[('dtc', dtc_pipe),
('svc', svc_pipe),
('cnn', cnn_pipe)],
voting='hard')
Run Code Online (Sandbox Code Playgroud)
的Featuriser,TimeSeriesScalerMeanVariance而Flattener类是一些定制变压器,所有雇用fit,transform和 …
我有一个相当大的 DataFrame(~500 列和 >5000 行)。我想为前 15 列添加一个前缀。我找到了一个函数add_prefix(),它可以一次为所有列设置前缀。我尝试了以下方法:
df[df.columns[range(0,15)]] = df[df.columns[range(0,15)]].add_prefix('f_')
Run Code Online (Sandbox Code Playgroud)
与'f_'作为前缀,我想补充。然而,输出似乎没有改变。
>>>
mean std var ... 525 526 label
0 -2.546261 17.827072 317.804485 ... -0.314016 -0.310878 0.0
1 -2.338710 17.915556 320.967136 ... -0.345603 -0.343088 0.0
2 -2.095051 17.539407 307.630788 ... -0.323596 -0.324990 0.0
3 -1.685209 18.257797 333.347150 ... -0.310060 -0.320796 0.0
4 -1.846169 17.240523 297.235618 ... -0.318660 -0.322732 0.0
Run Code Online (Sandbox Code Playgroud)
我想要的是:
>>>
f_mean f_std f_var ... 525 526 label
0 -2.546261 17.827072 317.804485 ... -0.314016 -0.310878 0.0 …Run Code Online (Sandbox Code Playgroud) 我在网上找到了这个漂亮的图表(显然是用 plotly 制作的),并想用 seaborn 重新创建它。

到目前为止,这是我的代码:
import pandas as pd
import seaborn as sns
data = ...
flierprops = dict(marker='o', markersize=3)
sns.boxplot(x="label", y="mean",palette="husl", data=data,saturation=1,flierprops=flierprops)
Run Code Online (Sandbox Code Playgroud)
这是迄今为止的结果:
我已经很高兴了,但我想调整线条和异常值颜色以匹配husl调色板。我怎样才能做到这一点?(以及附加:我将如何更改线宽?)
我有一个很长的字符串作为 xticks 的 matplotlib boxplot。有没有办法自动将它们分成多行以使情节更干净?我正在使用 seaborn 函数barplot来创建图形。
我用来创建它的代码:
plt.figure(figsize=(15,13))
sns.barplot(x="Component",y="TTTR",color = "C0",data=new,dodge=False,edgecolor="black",zorder=3)
plt.xticks(rotation=90)
plt.grid(axis='y',zorder=0)
plt.title("10 most impactful components",size=30,y=1.04,**pfont)
plt.ylabel("Impact (Sum TTR in h)")
plt.xlabel('Component')
plt.tight_layout()
Run Code Online (Sandbox Code Playgroud) python ×6
keras ×2
seaborn ×2
dataframe ×1
matplotlib ×1
pandas ×1
performance ×1
pip ×1
pipeline ×1
scikit-learn ×1
tensorflow ×1