【问题标题】:Why does %config line give me syntax error in Python 3.7?为什么 %config 行在 Python 3.7 中会出现语法错误?
【发布时间】:2019-08-12 06:16:16
【问题描述】:

我正在创建一个系统来预测地震。这段代码中有一行%config InlineBackend.figure_format = 'retina'。 每当我运行我的python代码时,它都会给出错误("% invalid syntax")

早些时候我遇到了%matplotlib 声明的问题,这是我通过谷歌发现的。但是,这个问题让我很头疼。

https://www.kaggle.com/pablocastilla/predict-earthquakes-with-lstm("获取代码的来源")

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from subprocess import check_output
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from sklearn.cross_validation import  train_test_split
import time #helper libraries
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from numpy import newaxis
from geopy.geocoders import Nominatim
from IPython import get_ipython

get_ipython().run_line_magic('matplotlib', 'inline')

import warnings
warnings.filterwarnings('ignore')

%config InlineBackend.figure_format = 'retina'

# Any results you write to the current directory are saved as output.
from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))

所以它在单词 config 突出显示之前显示“error invalid syntax”和% 符号。

预期的输出是我不应该收到错误,并且会给出写入目录的任何结果。

【问题讨论】:

  • 您是否在 jupyter/ipython 中运行此代码?
  • 任何以% 开头的东西在 Python 中都是无效的语法。正如@MatiasValdenegro 所怀疑的那样,您的问题可能与您的 IDE 有关。

标签: python pandas keras


【解决方案1】:

% 语法定义了一个 IPython 魔术命令。阅读:Magic commands

试试这个

from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')

取自Configure the backend of Ipython to use retina display mode with code

您需要安装 Ipython 库才能执行此操作。此外,如果您收到“没有名为 ipykernel 的模块”,请安装 ipykernel。

    pip install ipython
    pip install ipykernel

编辑:但我强烈建议使用 jupyter 笔记本,例如试试 google colab:https://colab.research.google.com/notebooks/welcome.ipynb

【讨论】:

    【解决方案2】:

    以 % 开头的命令不是有效的 python 命令,它们仅受 ipython/jupyter 解释器支持,官方 python 解释器不支持。

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多