import numpy as np import pickle f = open(r'C:\Users\sdnugeo\Desktop\target2.pkl','rb') bi = pickle.load(f) # bi = np.array(bi) data = np.arange(0, 156) print(data) data = data.tolist() # print(data) # 每次抽取1000个数值,共抽取10次 samples = [] c = [] for i in range(11): if len(data) > 15: sample = np.random.choice(data, 15, replace=False) else: sample = np.random.choice(data, 6, replace=False) # continue # print(type(sample)) # sample2 = np.array(sample) b = [bi[j] for j in list(sample)] # d = np.array(b) print(type(b)) c.append(b) # c = bi[0] print(sample) print(b) samples.append(sample) # sample = [s for s in sample if s in data] for s in sample: data.remove(s) samples = np.array(samples).reshape(-1) print(samples) print(data),报错setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (11,) + inhomogeneous part.
时间: 2024-03-16 14:44:56 浏览: 20
这个报错可能是因为你最后将 `samples` 转换成了 NumPy 数组,而 `samples` 中的元素是列表而非数值,导致形状不均匀。你可以将 `samples` 中的元素先转换成 NumPy 数组,再将整个 `samples` 数组转换成一维数组,如下所示:
```python
samples = [np.array(s) for s in samples]
samples = np.concatenate(samples, axis=0)
```
这样就可以将 `samples` 转换成一维的 NumPy 数组。另外,你在循环中的最后一次循环中没有把 `sample` 中的元素移除 `data` 中,可能会导致后续的循环中重复抽取相同的元素。你可以将最后一次循环中的 `for` 循环移到 `if` 语句的内部,这样就可以确保每次循环都会将 `sample` 中的元素从 `data` 中移除。
相关问题
import numpy as np import pickle import math f = open(r'C:\Users\sdnugeo\Desktop\target2.pkl','rb') bi = pickle.load(f) # bi = np.array(bi) data = np.arange(0, 156) print(data) data = data.tolist() # print(data) # 每次抽取1000个数值,共抽取10次 samples = [] c = [] a = 11 r = math.ceil(len(data)/a) print(r) for i in range(a): if len(data) > r : sample = np.random.choice(data, r, replace=False) else: sample = np.random.choice(data, len(data), replace=False) # for s in sample: # data.remove(s) # continue # print(type(sample)) # sample2 = np.array(sample) b = [bi[j] for j in list(sample)] # d = np.array(b) print(type(b)) c.append(b) # c = np.concatenate(c, axis=0) # c = bi[0] print(sample) # print(b) samples.append(sample) for s in sample: data.remove(s) # sample = [s for s in sample if s in data] samples = [np.array(s) for s in samples] samples = np.concatenate(samples, axis=0) c = [np.array(e) for e in c] c = np.concatenate(c, axis=0) # samples = np.array(samples).reshape(-1) print(samples) print(c.shape) 请帮我把这段代码修改成可调用的函数
好的,你想要将这段代码封装成一个函数,可以尝试以下的实现:
```python
import numpy as np
import pickle
import math
def sample_data(filepath):
f = open(filepath, 'rb')
bi = pickle.load(f)
data = np.arange(0, 156)
data = data.tolist()
samples = []
c = []
a = 11
r = math.ceil(len(data)/a)
for i in range(a):
if len(data) > r:
sample = np.random.choice(data, r, replace=False)
else:
sample = np.random.choice(data, len(data), replace=False)
b = [bi[j] for j in list(sample)]
c.append(b)
samples.append(sample)
for s in sample:
data.remove(s)
samples = [np.array(s) for s in samples]
samples = np.concatenate(samples, axis=0)
c = [np.array(e) for e in c]
c = np.concatenate(c, axis=0)
return samples, c
```
你可以将这个函数保存在一个.py文件中,然后在其他的python文件中,通过import导入并调用该函数。调用方法如下:
```python
from your_module import sample_data
filepath = r'C:\Users\sdnugeo\Desktop\target2.pkl'
samples, c = sample_data(filepath)
```
其中,filepath是你想要读取的pickle文件的路径。函数返回两个变量,分别为samples和c。
import pickle, numpy as np IndentationError: unexpected indent
在Python中,IndentationError: unexpected indent错误通常是由于代码缩进不正确引起的。这个错误表示在代码的某一行缩进了不应该缩进的地方。要解决这个错误,你可以按照以下步骤进行操作:
1. 检查代码缩进:确保代码块中的每一行都有正确的缩进。Python使用缩进来表示代码块的层次结构,因此缩进必须是一致的。通常情况下,使用4个空格或一个制表符作为缩进。
2. 检查冒号:在Python中,代码块通常以冒号(:)开始,并且下一行的代码必须缩进。确保在需要缩进的地方使用了冒号。
3. 检查代码格式:有时候,错误的缩进可能是由于代码格式不正确引起的。确保代码中没有多余的空格或制表符,并且每一行都是以正确的方式缩进的。
以下是一个示例代码,演示了如何导入pickle和numpy模块,并且避免了IndentationError错误:
```python
import pickle
import numpy as np
# 正确的缩进示例
data = [1, 2, 3, 4, 5]
with open('data.pkl', 'wb') as f:
pickle.dump(data, f)
# 错误的缩进示例
# with open('data.pkl', 'wb') as f:
# pickle.dump(data, f)
```