我的目标是在 ggplot 的箱线图中绘制每个子组的平均值。目前,我可以在箱线图中绘制 x 轴(即 max_depth 和 eta)中每个组的均值。但是,我想绘制四个子组的平均值(参数 * 级别)。我怎样才能完成这个任务?
这是一个可重现的示例:
toy_data
best_F1 Parameter Level
0.5660661 max_depth 1
0.6168498 eta 1
0.5727848 max_depth -1
0.6050284 eta -1
0.5730897 eta -1
0.6289212 max_depth 1
0.5087209 eta -1
0.5863454 eta 1
0.5599393 eta -1
0.6027165 max_depth 1
0.5389856 eta -1
0.5663977 max_depth -1
0.5171256 eta -1
0.5540444 max_depth -1
0.5313243 eta -1
0.586357 eta 1
0.5700416 eta 1
0.5658863 eta -1
0.6131883 eta 1
0.519945 eta 1
ggplot(toy_data, aes(x = Parameter, y …Run Code Online (Sandbox Code Playgroud) 我正在运行 pandas 文档中的示例(https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html),但出现意外异常:
index = ['Firefox', 'Chrome', 'Safari', 'IE10', 'Konqueror']
df = pd.DataFrame({
'http_status': [200,200,404,404,301],
'response_time': [0.04, 0.02, 0.07, 0.08, 1.0]},
index=index)
df.reindex(['http_status', 'user_agent'], axis="columns")
TypeError: reindex() got an unexpected keyword argument "axis"
Run Code Online (Sandbox Code Playgroud) 我正在使用distplot()Seaborn的功能,我将两个密度图并置 - 每个在同一图中具有不同的颜色 - 我想标记它们。
我使用函数文档中提到的参数“标签”。
我的代码是:
Response4_mask = train_with_response['Response4'] == 1
not_Response4_mask = train_with_response['Response4'] != 1
plt.figure()
sns.distplot(a = train_imp_with_response[Response4_mask]['Family_Hist_4'], hist = True, color = 'red', label = 'Response4')
sns.distplot(a = train_imp_with_response[not_Response4_mask]['Family_Hist_4'], hist = True, label = 'not_Response4')
plt.title('Family_Hist_4')
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)
输出如下。里面没有标签:
我想在 2 x 2 网格中排列四个 Seaborn 图。我尝试了以下代码,但出现异常。我还想知道如何在子图中设置标题和 xlabel、ylabel 以及整个网格图的标题。
一些玩具数据:
df
'{"age":{"76":33,"190":30,"255":36,"296":27,"222":19,"147":39,"127":23,"98":24,"168":29,"177":39,"197":27,"131":36,"36":30,"219":28,"108":38,"198":34,"40":32,"246":24,"109":26,"117":47,"20":26,"113":24,"279":35,"120":35,"7":26,"119":28,"272":24,"66":28,"87":28,"133":28},"Less_than_College":{"76":1,"190":1,"255":0,"296":1,"222":1,"147":1,"127":0,"98":0,"168":1,"177":1,"197":0,"131":1,"36":0,"219":0,"108":0,"198":0,"40":0,"246":0,"109":1,"117":1,"20":0,"113":0,"279":0,"120":0,"7":0,"119":1,"272":0,"66":1,"87":0,"133":0},"college":{"76":0,"190":0,"255":0,"296":0,"222":0,"147":0,"127":1,"98":1,"168":0,"177":0,"197":1,"131":0,"36":1,"219":1,"108":0,"198":1,"40":1,"246":0,"109":0,"117":0,"20":1,"113":1,"279":0,"120":1,"7":1,"119":0,"272":0,"66":0,"87":1,"133":1},"Bachelor":{"76":0,"190":0,"255":1,"296":0,"222":0,"147":0,"127":0,"98":0,"168":0,"177":0,"197":0,"131":0,"36":0,"219":0,"108":1,"198":0,"40":0,"246":1,"109":0,"117":0,"20":0,"113":0,"279":1,"120":0,"7":0,"119":0,"272":1,"66":0,"87":0,"133":0},"terms":{"76":30,"190":15,"255":30,"296":30,"222":30,"147":15,"127":15,"98":15,"168":30,"177":30,"197":15,"131":30,"36":15,"219":15,"108":30,"198":7,"40":30,"246":15,"109":15,"117":15,"20":15,"113":15,"279":15,"120":15,"7":15,"119":30,"272":15,"66":30,"87":30,"133":15},"Principal":{"76":1000,"190":1000,"255":1000,"296":1000,"222":1000,"147":800,"127":800,"98":800,"168":1000,"177":1000,"197":1000,"131":1000,"36":1000,"219":800,"108":1000,"198":1000,"40":1000,"246":1000,"109":1000,"117":1000,"20":1000,"113":800,"279":800,"120":800,"7":800,"119":1000,"272":1000,"66":1000,"87":1000,"133":1000}}'
fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)
ax = fig.add_subplot(2, 2, 1)
ax.sns.distplot(df.Principal)
ax = fig.add_subplot(2, 2, 2)
ax.sns.distplot(df.terms)
ax = fig.add_subplot(2, 2, 3)
ax.sns.barplot(data = df[['Less_than_College', 'college', 'Bachelor', ]])
ax = fig.add_subplot(2, 2, 4)
ax.sns.boxplot(data = df['age'])
plt.show()
AttributeError: 'AxesSubplot' object has no attribute 'sns'
Run Code Online (Sandbox Code Playgroud) 我想使用 Pandas 中的 .query() 方法创建 isna() 子句
但我收到错误。
对于可重现的示例:
import pandas as pd
import seaborn as sns
mpg = sns.load_dataset('mpg')
mpg[mpg['cylinders'].isna()] # This works
mpg.query('cylinders.isna()') # This raises an exception
TypeError: 'Series' objects are mutable, thus they cannot be hashed
Run Code Online (Sandbox Code Playgroud) 我有一个带有以下日期时间索引的 Pandas 数据框:
DatetimeIndex(['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07',
'2020-01-08', '2020-01-09', '2020-01-10', '2020-01-13',
'2020-01-14', '2020-01-15',
...
'2020-01-17', '2020-01-21', '2020-01-22', '2020-01-23',
'2020-01-24', '2020-01-27', '2020-01-28', '2020-01-29',
'2020-01-30', '2020-01-31'],
dtype='datetime64[ns]', name='Date', length=49098, freq=None)
Run Code Online (Sandbox Code Playgroud)
我想获取与以下日期时间索引相交的行:
DatetimeIndex(['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07',
'2020-01-08', '2020-01-09', '2020-01-10'],
dtype='datetime64[ns]', name='Date', freq=None)
Run Code Online (Sandbox Code Playgroud)
最自然(又名“Pythonic”)的方法是什么?
当我尝试在 Python 3.7 中使用 f-string 语法组合字符串时出现错误。
我的代码如下:
i = 1
site_id= 0
meter = 0
model_id = i
target_name = 'log1p_meter_reading_corrected2'
f'model_site_id_{str(site_id)}_meter_{str(meter)}_{target_name}_model_id_{str(model_id)}_11_12_19.hdf5'
Run Code Online (Sandbox Code Playgroud)
返回错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-68-1ebe1c78d868> in <module>
6 target_name = 'log1p_meter_reading_corrected2'
7
----> 8 f'model_site_id_{str(site_id)}_meter_{str(meter)}_{target_name}_model_id_{str(model_id)}_11_12_19.hdf5'
TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)
是什么导致了错误,我应该如何更正我的代码?
我已经使用 conda 安装了 jupyter_contrib_nbextensions,即:conda install -c conda-forge jupyter_nbextensions_configurator
这是我所看到的:
我没有看到我期望看到的扩展toc2。
事实上,我正在尝试配置我的 JupyterLab Notebook 以提供以下功能:
当我选择感兴趣的功能时,“启用”按钮变暗。
我的 JupterLab 是这样的:
我缺少什么?
我正在尝试导入 torchvision,但出现错误。我在网上研究了这个话题,后来的版本似乎有问题。提供了不同的解决方案——主要是重新安装模块——我尝试过但没有结果我发现的最新建议是使用以下我成功执行的命令安装 torch 和 torchvision:
pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html
尽管如此,错误仍然存在:
(tf2) C:\Users\Alienware>python
Python 3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 22:22:21) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torchvision
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Alienware\Anaconda3\envs\tf2\lib\site-packages\torchvision\__init__.py", line 3, in <module>
from torchvision import models
File "C:\Users\Alienware\Anaconda3\envs\tf2\lib\site-packages\torchvision\models\__init__.py", line 5, in <module>
from .inception import *
File …Run Code Online (Sandbox Code Playgroud) 我想可视化由 3 个形状 (8, 3) 的 numpy 数组定义的 3 个多面体。
我正在寻找类似的东西:
我的数据如下:
A = np.array([[0.92523719, 0.26843252, 0.77794309],
[0.73156748, 0.27794309, 0.57476281],
[0.62113842, 0.37886158, 0.87886158],
[0.72205691, 0.07476281, 0.76843252],
[0.57476281, 0.23156748, 0.72205691],
[0.77794309, 0.42523719, 0.73156748],
[0.87886158, 0.12113842, 0.62113842],
[0.76843252, 0.22205691, 0.92523719]])
B = np.array([[0.23156748, 0.72205691, 0.57476281],
[0.26843252, 0.77794309, 0.92523719],
[0.12113842, 0.62113842, 0.87886158],
[0.22205691, 0.92523719, 0.76843252],
[0.27794309, 0.57476281, 0.73156748],
[0.37886158, 0.87886158, 0.62113842],
[0.07476281, 0.76843252, 0.72205691],
[0.42523719, 0.73156748, 0.77794309]])
C = np.array([[0.73156748, 0.77794309, 0.42523719],
[0.62113842, 0.87886158, 0.12113842],
[0.77794309, 0.92523719, 0.26843252],
[0.57476281, 0.73156748, 0.27794309],
[0.87886158, 0.62113842, …Run Code Online (Sandbox Code Playgroud) pandas ×5
python ×5
python-3.x ×4
matplotlib ×2
seaborn ×2
3d ×1
boxplot ×1
datetime ×1
density-plot ×1
f-string ×1
ggplot2 ×1
grid ×1
indexing ×1
jupyter-contrib-nbextensions ×1
jupyter-lab ×1
label ×1
mean ×1
r ×1
reindex ×1
string ×1
torchvision ×1
typeerror ×1