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 09:44:56 浏览: 163
这个报错可能是因为你最后将 `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 pickle import time from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.model_selection import train_test_split from sklearn.naive_bayes import MultinomialNB from sklearn.metrics import classification_report import numpy as np with open('体育.pkl', 'rb') as f: sports_news = pickle.load(f) # 加载行政新闻数据集 with open('从政.pkl', 'rb') as f: politics_news = pickle.load(f) # 将体育新闻和行政新闻合并成一个数据集 data = np.concatenate([politics_news, sports_news]) def load_variavle(filename): f = open(filename, 'rb') r = pickle.load(f) f.close() return r news = np.concatenate([politics_news, sports_news]) print(news.data[0]) print(news.target_names) print(news.target[0:20]) X_train,X_test,y_train,y_test=train_test_split(news.data,news.target,test_size=0.25,random_state=520) vec =TfidfVectorizer(stop_words='english') X_train=vec.fit_transform(X_train) X_test=vec.transform(X_test) time_start = time.perf_counter() mnb=MultinomialNB() mnb.fit(X_train,y_train) y_predict=mnb.predict(X_test) print('训练时间',time_start) print('准确率:',mnb.score(X_test,y_test)) print(classification_report(y_test,y_predict,target_names=news.target_names))
这段代码主要是导入一些Python第三方库,包括pickle、time、sklearn等。同时使用了TfidfVectorizer、train_test_split、MultinomialNB和classification_report等模块,用于数据的处理、模型的训练和评估。最后从名为“体育.pkl”的文件中读取了数据,并将其存储在变量sports_news中。
给下面代码每一行给上注释 with open(r'mat_pickle/init_mat.pkl', "rb") as f0: init_mat = np.array(list(pickle.load(f0).values())) with open(r'mat_pickle/trans_mat.pkl', "rb") as f1: init_trans_mat = np.array(list(pickle.load(f1).values())) with open(r'mat_pickle/emit_mat.pkl', "rb") as f2: init_emit_mat = pickle.load(f2) catalog = list(init_emit_mat) trans_mat = []
```python
# This is a Python code with comments explaining each line
# Import the necessary library
import pickle
import numpy as np
# Load the initial matrix from the pickle file
with open(r'mat_pickle/init_mat.pkl', "rb") as f0:
init_mat = np.array(list(pickle.load(f0).values()))
# Load the initial transition matrix from the pickle file
with open(r'mat_pickle/trans_mat.pkl', "rb") as f1:
init_trans_mat = np.array(list(pickle.load(f1).values()))
# Load the initial emission matrix from the pickle file
with open(r'mat_pickle/emit_mat.pkl', "rb") as f2:
init_emit_mat = pickle.load(f2)
# Get the list of states
catalog = list(init_emit_mat)
# Initialize the transition matrix
trans_mat = []
```
注释如下:
```python
# 导入必要的库
import pickle
import numpy as np
# 从 pickle 文件中加载初始矩阵
with open(r'mat_pickle/init_mat.pkl', "rb") as f0:
init_mat = np.array(list(pickle.load(f0).values()))
# 从 pickle 文件中加载初始转移矩阵
with open(r'mat_pickle/trans_mat.pkl', "rb") as f1:
init_trans_mat = np.array(list(pickle.load(f1).values()))
# 从 pickle 文件中加载初始发射矩阵
with open(r'mat_pickle/emit_mat.pkl', "rb") as f2:
init_emit_mat = pickle.load(f2)
# 获取状态列表
catalog = list(init_emit_mat)
# 初始化转移矩阵
trans_mat = []
```
阅读全文
相关推荐

















