对于一个“普通”的Python类,我习惯于可以任意添加额外的属性。例如,我可以执行以下操作: # Create a class
class MyClass: pass
# Create an object of this class
my_object = MyClass()
# Add any attribute I want
my_object.my_new_attribute = "Hello world!"
# Now it has this attribute and I can use it:
print(my_object.my_new_attribute
尝试使用np.select或np.where对DataFrame列运行方法。无论采用哪种方法,我都会收到错误消息,说“对象没有'select‘属性(如果我使用np.where,则是'where’)。当我转换列并检查特定的列类型时,它会返回一个numpy.ndarray。根据numpy文档,np.where和np.select都应该在numpy数组上工作。 检查转换为numpy数组的Series上的类型,在检查类型后,我尝试该方法并收到错误: type(np.array(daily_std_df['Daily_Std']))
numpy.ndarray
typ
当使用自定义类的numpy array时:
class TestClass:
active = False
如何使用内联掩码(布尔索引数组),如下所示:
直接尝试失败:
items = np.array([TestClass() for _ in range(10)])
items[items.active]
AttributeError: 'numpy.ndarray' object has no attribute 'active'
有什么建议吗?
我试着用下面的代码.But来绘制子图,我正在获取'AttributeError: 'numpy.ndarray' object has no attribute 'boxplot'。
但更改plt.subplots(1,2)时,它正在用索引错误绘制方框图。
import matplotlib.pyplot as plt
import seaborn as sns
fig = plt.Figure(figsize=(10,5))
x = [i for i in range(100)]
fig , axes = plt.subplots(2,2)
在我的实验中,我有一个numpy对象的实例,我需要设置它的__hash__方法。
我尝试过不同的方法(实际上看起来是一样的):
import numpy as np
x = np.array([1, 2, 3])
x.flags.writeable = False # set the array immutable
setattr(x, '__hash__', lambda self: 0) # doesn't work without self either
AttributeError: 'numpy.ndarray' object attri
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,2*np.pi,500)
y = np.sin(x**2)
fig, axes = plt.subplots(2,2)
for current_axis in axes:
current_axis.plot(x,y)
结果是:
AttributeError: 'numpy.ndarray' object has no attribute 'plot'
如果我不迭代,但执行下面的,它可以工作
axes[0, 0].plo
我有一个二维的物体数组。我想迭代这个数组,并打印每个对象的一些属性。下面是我的代码:
import numpy as np
class example:
def __init__(self):
self.number = 1
a = example()
b = example()
c = example()
d = example()
array = np.array([[a,b],[c,d]],dtype=np.object)
for x in np.nditer(array,["refs_ok"]):
print x
错误消息:AttributeError
我有点迷失了用海运relplot向我的标记添加标签的最佳方法。我在matplotlib文档中看到有一个axes.text()方法看起来是正确的方法,但是这个方法似乎并不存在。在这个意义上,seaborn的行为是否与matplotlib不同?正确的方法是什么? 错误: AttributeError: 'numpy.ndarray' object has no attribute 'text' 代码: line_minutes_asleep = sns.relplot(
x = "sleep_date",
y = "minu
我和一起工作。关于这个例子
import csv as csv
import numpy as np
csv_file_object = csv.reader(open('train.csv', 'rb'))
header = csv_file_object.next()
data = []
for row in csv_file_object:
data.append(row)
data = np.array(data)
我遇到了以下错误:
回溯(最近一次调用):
文件"C:/Users/Prashant/Desktop/data mi
这是我的尝试,但我得到了错误"AttributeError: 'numpy.ndarray' object has no attribute 'contains'"。
如果你们中的任何人能帮助我理解如何在这段代码中定义'y',让它是一个数字数组,1对应于包含指定点的多边形的索引,0对应于不包含指定点的多边形的索引,我将不胜感激。
polygonss=[]
for i in range(0, len(verts)):
polygonc = Polygon(verts[i])
print (polygonc)
我创建了子图,并且我想修改每个子图的xlim。为此,我编写了以下代码: import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 3, figsize=(20, 10))
for ax in np.nditer(axs, flags=['refs_ok']):
ax.set_xlim(left=0.0, right=0.5) 但是我得到了以下错误: AttributeError: 'numpy.ndarray' object has no attrib
下面是一个场景:
我有以下变量:
val = [('am', '<f8'), ('fr', '<f8')] # val is type numpy.recarray
am = [12.33, 1.22, 5.43, 15.23] # am is type numpy.ndarray
fr = [0.11, 1.23, 2.01, 1.01] # fr is type numpy.ndarray
我需要的是检测am = 12.33和am = 15.23的索引,一旦提取(在本例中索引是[0]和[
我试图重写getattr方法,据我理解,下面的代码段中应该有无限循环,因为默认情况下调用object.__getattribute__(self,attr),它将调用过高的getattr方法,因为名称空间中不存在属性'notpresent‘,这个过程将继续重复。有人能帮我弄清楚为什么这里没有人观察到这种行为。
此外,当我们试图在方法中显式调用getattribute时,当使用点表示法访问属性时,当执行对getattribute的隐式调用时,我无法理解为什么不引发AttributeError。
class Test(object):
#Act as a fallback and
我有一份谱图。它是129行x n列。我想“把”栏切成20栏,然后我会这样做:
if spectrogram.shape[1] > 20:
for row in spectrogram:
i = spectrogram.index(row)
row = row[:20]
spectrogram[i] = row
但是它使用.index()抛出了一个错误,所以我尝试使用.where(),就像我在SOF上看到的那样,但是发生了另一个错误:
我正在尝试从for循环中访问数组soc+。在for循环之外,它只给出最后一个值。如何从for循环中访问整个soc阵列?
如果我使用追加方法,它会出现以下错误“'numpy.ndarray‘对象没有属性' append’”
谢谢。
这是我代码的一部分
for k in range(1,len(t)):
soc+=i[k]*(t[k]-t[k-1])/3600*1/(cell_capacity)
soc = soc.append(k)
我尝试使用追加方法,但是它给出了错误“'numpy.ndarray‘对象没有属性' append’”
在sklearn中实现的随机森林中有apply(X)函数-是否有与GBRT等效的函数?
编辑:
for estimator in gbrt.estimators_:
estimator.tree_.apply(X)
提供:
File "<pyshell#29>", line 2, in <module>
estimator.tree_.apply(Z)
AttributeError: 'numpy.ndarray' object has no attribute 'tree_'
import scipy as sp
import numpy as np
a=sp.sparse.coo_matrix(np.random.randint(0,9,[4,5]))
b=sp.sparse.coo_matrix(np.random.randint(0,9,[4,2]))
sp.hstack([a,b]).toarray()
就是给我
AttributeError: 'numpy.ndarray' object has no attribute 'toarray'
你能帮我解决我的愚蠢错误吗?
我想在某些数据上运行sklearn的RandomForestClassifier,这些数据被打包成一个稀疏的numpy.ndarray。调用fit会给出ValueError: setting an array element with a sequence.。从其他帖子中我了解到,随机森林无法处理稀疏数据。
我期望对象有一个todense方法,但它没有。
>>> X_train
array(<1443899x1936774 sparse matrix of type '<class 'numpy.float64'>'