首页 \ 问答 \ 在ipython中运行粘贴代码的错误(Errors running pasted code in ipython)

在ipython中运行粘贴代码的错误(Errors running pasted code in ipython)

我想从这里使用基本的sklearn iris数据集代码:

http://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html

注意:为避免在第二行上出现编码问题,我将其从Ga {accent-e} l Varoquaux更改为Gael

print(__doc__)
# Code source: Gael Varoquaux  
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2]  # we only take the first two features.
Y = iris.target

x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5

plt.figure(2, figsize=(8, 6))
plt.clf()

# Plot the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())

# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
X_reduced = PCA(n_components=3).fit_transform(iris.data)
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=Y,
           cmap=plt.cm.Paired)
ax.set_title("First three PCA directions")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])

plt.show()

如果我在中间选择一个任意点将代码分成两部分并分别剪切并粘贴 - 那么代码工作正常。

在这里输入图像描述

但是,当粘贴整个片段时,后面的行会出现乱码:

In [267]: ax.set_title("First three PCA directions")
Out[267]: <matplotlib.text.Text at 0x113726250>

tor")68]: ax.seax.seax.seax.seax.seax.seax.se.wax.seax.seax.seax.seax.seax.seax.se.wal("2nd eigenvec
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-268-81e33d4e9263> in <module>()
----> 1 ax.seax.seax.seax.seax.seax.seax.se.wax.seax.seax.seax.seax.seax.seax.se.wal("2nd eigenvector")

AttributeError: 'Axes3D' object has no attribute 'seax'

那么在这里有一个技巧能够将更多的文本粘贴到ipython吗? 请注意代码中没有选项卡 。 这是用于python 2.7.3 ipython 4.2.1另外:我在OS / X上使用iterm2 :不确定这是否会起作用。

根据(良好)建议进行更新 ,这也是通过%cpaste尝试的。 结果相同:将整个数量作为单个单位粘贴时,文本会被损坏。


I am trying to use the basic sklearn iris datasets code from here:

http://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html

NOTE: to avoid encoding issues on the 2nd line I changed to Gael from Ga{accent-e}l Varoquaux

print(__doc__)
# Code source: Gael Varoquaux  
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2]  # we only take the first two features.
Y = iris.target

x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5

plt.figure(2, figsize=(8, 6))
plt.clf()

# Plot the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())

# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
X_reduced = PCA(n_components=3).fit_transform(iris.data)
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=Y,
           cmap=plt.cm.Paired)
ax.set_title("First three PCA directions")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])

plt.show()

If I pick an arbitrary point in the middle to split the code into two pieces and cut and paste them separately - then the code works fine.

enter image description here

But instead when pasting the entire snippet at one time the latter lines get garbled:

In [267]: ax.set_title("First three PCA directions")
Out[267]: <matplotlib.text.Text at 0x113726250>

tor")68]: ax.seax.seax.seax.seax.seax.seax.se.wax.seax.seax.seax.seax.seax.seax.se.wal("2nd eigenvec
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-268-81e33d4e9263> in <module>()
----> 1 ax.seax.seax.seax.seax.seax.seax.se.wax.seax.seax.seax.seax.seax.seax.se.wal("2nd eigenvector")

AttributeError: 'Axes3D' object has no attribute 'seax'

So is there a trick here to be able to paste larger amounts of text into ipython ? Note there are no tabs in the code. This is for ipython 4.2.1 with python 2.7.3 Also: i'm using iterm2 on OS/X: not sure whether that comes into play or not.

Update based on a (good) suggestion this was also attempted via %cpaste. The results are the same: text gets corrupted when pasting the entire amount as a single unit.


原文:https://stackoverflow.com/questions/42461707
更新时间:2022-03-13 18:03

最满意答案

尝试更换

m = (Message)response.getItems().get(i);

m = service.getMessage(response.getItems().get(i).getItemId());

Try to replace

m = (Message)response.getItems().get(i);

with

m = service.getMessage(response.getItems().get(i).getItemId());

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)