data = np.r_[1:1000] data = list(data) print(data) # 每次抽取1000个数值,共抽取10次 samples = [] for i in range(2): sample = np.random.choice(data, 10) samples.append(sample) print(sample) data.remove(sample) print(data)请帮我修改这个代码
时间: 2023-11-29 15:04:59 浏览: 71
好的,这里有几个需要修改的地方:
1. 在第一行代码中,np.r_应该是np.arange才能生成从1到999的数列。所以应该将np.r_改为np.arange。
2. 在第二行代码中,我们要将生成的数列转换为列表,可以使用tolist()函数实现。
3. 在第七行代码中,remove()函数的参数应该是sample而不是[sample],因为sample是一个数组。
修改后的代码如下:
```python
import numpy as np
data = np.arange(1, 1000)
data = data.tolist()
print(data)
# 每次抽取1000个数值,共抽取10次
samples = []
for i in range(10):
sample = np.random.choice(data, 10)
samples.append(sample)
for s in sample:
data.remove(s)
print(samples)
print(data)
```
这样修改后的代码可以正确地抽取10次样本,并将已选中的样本从数据集中移除。
相关问题
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 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.
这个报错可能是因为你最后将 `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` 中移除。
阅读全文