我想在 IPython 中创建一个钩子,当我执行 Cntl-D、%Exit、%Quit 或 %exit 和 %quit 并确认时运行。
动机是我希望有一个线程在后台无限期地在 IPython 中运行,直到我按 Cntl-D 退出,然后我想告诉该线程退出。我想我将使用钩子向我的后台线程发送信号。
我想一种方法是包装 IPython::iplib::InteractiveShell::exit。这可能吗?有没有更好的方法来做我想做的事情?谢谢
请帮助这让我发疯。我正在使用 Ipython Shell,并拼命尝试接收不那么冗长的错误消息。对于每个小错误,我都会收到 40 多行。我认为我需要做的是将 xmode 设置为普通。我起初尝试在启动 ipython 时在命令提示符下执行此操作,但我认为 xmode 是一个魔术函数,因此应该在 Ipython shell 中调用,因此我不会在命令提示符中粘贴我尝试的内容。在外壳中,我尝试过:
xmode('Plain')
xmode('plain')
xmode(plain)
xmode(Plain)
以上所有内容都带有 % 前缀。我也试过
xmode<('Plain')>
以及大约十亿种其他排列。
有人可以告诉我我做错了什么......?
非常感谢
您好,我已经训练并测试了数据。我正在尝试使用sklearn的特征相关性Seelct K Best来选择相关特征并在之后绘制条形图。但是我收到这个错误:
ValueError: could not convert string to float: B
但我开始认为我的数据集中确实有一列看起来像这样,这可能是问题所在:
CancellationCode:
A
B
C
D
如果此列导致问题,我该如何解决此错误 这是我的代码如下:
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
import matplotlib.pyplot as plt
selector = SelectKBest(f_classif, k=13)
selector.fit(X_train, y_train)
scores_select = selector.pvalues_
print scores_select
# Plotting the bar Graph to visually see the weight of each feature
plt.bar(range(len(scores_select)), scores_select, align='center')
plt.xticks(range(len(features_columns)), features_columns, rotation='vertical')
plt.show()
我的问题是关于交互式 Jupyter 笔记本。
我想显示 JavaScript 按钮并在单击按钮时将某些内容打印到单元格。
我们可以使用 ipywidgets 轻松做到这一点:
def OnClick(b=None): print 'qq'
b = widgets.Button(description='Button')
b.on_click(OnClick)
display(b)
但是当我们跳转到纯 JavaScript 时,事情就出错了。即我有 JavaScript 按钮,在 on_click() 事件处理程序中我使用内核交互,如:
var kernel = IPython.notebook.kernel;
kernel.execute('onClick()');         
回调被调用成功(我通过哔声检查),但打印在单元格中没有输出,其中显示按钮。所以我想应该有一些魔法(就像 Python 世界中的一切!)来访问打印区域,你能帮我施展它吗?
ipython ipython-notebook jupyter jupyter-notebook ipywidgets
这是一个奇怪的问题,我不完全确定如何恰当地提出问题,但我会尽力而为。
我有一个自定义类,它基本上是一个 API 的包装器,它在每次调用时使用新数据更新 SQLite 数据库(我无法将其添加到问题中,因为它是庞大且私密的)。
奇怪的是,似乎有些信息正在被缓存(我认为这是不可能的,但这是它让我想起的唯一一件事,比如当你在 web dev 中进行编辑但他们没有更新时)因为它首先起作用时间,但是当我尝试重新初始化对象并再次运行它时,它不会向数据库添加任何新数据(当我知道有新数据要添加时)。
我知道代码有效,因为如果我重新启动内核并再次运行它,它更新没有问题。
我尝试删除对象 ( del InitializedClass)、重新初始化并使用不同的值进行初始化,但似乎没有任何效果。除非重新启动内核,否则它不会更新数据库。
有没有人遇到过这样的问题?如果这还不够,我很乐意提供更多信息,但我不知道如何描述它。
谢谢!!
编辑
下面的伪代码基本上就是正在发生的事情
from something import SomeClass    
while True:
    obj = SomeClass() #      <---------  How can I "reset" this on each loop?
    obj.get_new_data_from_api()
    obj.update_raw_db()
    obj.process_raw_data()
    obj.update_processed_db()
    # i tried different combinations of deleting the object
    del obj
    del SomeClass
    from something import SomeClass
编辑2:
所以正如大家所提到的,这是类本身的问题,但我仍然不明白为什么会发生错误。基本上,end当我将datetime.now()函数调用作为默认 kwarg(即使在删除类并创建新实例之后,这并没有)更新)。该问题如下图所示:
class SomeBrokenClass():
    def __init__(self):    
        pass
    def get_endpoint(self, start, end):
        return 'https://some.api.com?start_date=%s&end_date=%s' % (start, …我inspect.getsource用来检查我导入的库:
In[52]: from django.views.generic import View
In[53]: view_code = inspect.getsource(View)
In[54]: len(view_code)
Out[54]: 3242
检索格式化代码
In[55]: print(view_code)
class View(object):
    """
    Intentionally simple parent class for all views. Only implements
    dispatch-by-method and simple sanity checking.
    """
    http_method_names = ['get', 'post', 'put',
                        'patch', 'delete', 'head', 'options', 'trace']
    def __init__(self, **kwargs):
我想将代码存储到我的笔记中以供进一步参考,
为此,我必须滚动浏览整个代码以进行复制。
代码太长就不方便了。
如何print以直接的方式将 的输出复制到剪贴板?
我正在尝试在 jupyter notebook 中显示 youtube 视频。但是当我将代码置于条件中时,它不会显示。但是,如果我删除 if 条件,它会起作用。
from IPython.display import HTML
a = input()
if a=="a":
  HTML('<iframe src="https://www.youtube.com/embed/S_f2qV2_U00?rel=0&controls=0&showinfo=0" width="560" height="315" frameborder="0" allowfullscreen></iframe>')
else:
  print('erro')
上面的代码不会显示视频的框。但是如果我像这个一样删除 if 条件,
from IPython.display import HTML
a = input()
HTML('<iframe src="https://www.youtube.com/embed/S_f2qV2_U00?    rel=0&controls=0&showinfo=0" width="560" height="315"  frameborder="0" allowfullscreen></iframe>')
它会工作得很好。
我有一个 Python 项目的文件夹结构,如下所示:
proj/
??? cars
?   ??? honda.py
?   ??? volvo.py
??? trucks
?   ??? chevy.py
?   ??? ford.py
??? main.py
??? params.py
内容params.py:
""" Parameters used by other files. """
serial = '12-411-7843'
内容honda.py:
""" Information about Honda car. """
from params import serial
year = 1988
s = serial
print('year is', year)
print('serial is', s)
在proj/文件夹中,我可以使用 iPython 运行脚本:
$ cd path/to/proj/
$ ipython
In [1]: run cars/honda.py
year is 1988
serial …我在目录中有一些 jpeg。我想在窗口中按行和列显示它们。例如,如果我有 10 张图片,我想将它们显示为 2 行 x 5 列的表格。
MATLAB 和 Octave 中有一个 subplot(m, n, k) 命令。我如何在 python 中做类似的事情?    
我已经尝试过使用 PIL.Image 和 show() 方法的枕头,但它非常有限并且只显示 1 个图像。
1-如何在本机(不在浏览器中)执行此操作?
2-如何使用 matplotlib 做到这一点?
3-如何使用 Jupyter 在浏览器中执行此操作?
如何在“无花果”中子绘制“pie1”,使其位于“第一个”位置。这就是我的做法,但它不起作用
    import pandas as pd
    import numpy as np
    import seaborn as sns
    import plotly.offline as pyp
    import plotly.graph_objs as go
    from plotly import tools
    import plotly.plotly as py
    from plotly.offline import iplot,init_notebook_mode
    from IPython.core.display import HTML
    import plotly.io
    df1=pd.read_excel('file.xlsx',sheet_name='sheet1',index=False)
    con_pivot=pd.pivot_table(con,index='Category',values=('Payment'),aggfunc='sum',margins=True,margins_name='Total')
    fig = tools.make_subplots(rows=2, cols=2, subplot_titles=('The first','3','2','4'))
    pie1=go.Pie(labels=con_pivot.index,values=con_pivot.values)
    fig.append_trace(pie1,1,1)
    pyo.plot(fig)
任何帮助将不胜感激。谢谢
ipython ×10
python ×7
python-3.x ×2
class ×1
image ×1
ipywidgets ×1
jupyter ×1
matplotlib ×1
pandas ×1
plotly ×1
python-2.7 ×1
scikit-learn ×1
subplot ×1