Python 如何从数据集创建初始总体?
我有一个包含单词列表的文本文件。我想输入这些单词作为我的初始人口100。我总共大约有200个字。我不确定是否使用“导入随机”。任何帮助都将不胜感激。我使用Python 如何从数据集创建初始总体?,python,pandas,Python,Pandas,我有一个包含单词列表的文本文件。我想输入这些单词作为我的初始人口100。我总共大约有200个字。我不确定是否使用“导入随机”。任何帮助都将不胜感激。我使用df.sample来完成类似的任务。例如: data = ['stargaze', 'stephanotis', 'twelfth', 'speedup', 'harness', 'pole', 'seagull', 'examination', 'enforceable', 'fescue', 'trumpery',
df.sample
来完成类似的任务。例如:
data = ['stargaze', 'stephanotis', 'twelfth', 'speedup', 'harness', 'pole',
'seagull', 'examination', 'enforceable', 'fescue', 'trumpery',
'mow', 'Marvin', 'important', 'monastic', 'shark', 'Sophoclean',
'Bella', 'modicum', 'corpse', 'insuppressible', 'Lancelot',
'redstart', 'Emory', 'place', 'Kristin', 'q', 'heathen', 'nasal',
'Constantinople', 'Alameda', 'lonesome', 'thong', 'spline',
'mayonnaise', 'smile', 'hasty', 'camilla', 'perpendicular', 'writ',
'berate', 'poplin', 'Martian', 'seventieth', 'grief', 'doorknob',
'quicksand', 'Pawtucket', 'mercury', 'loris', 'here', 'emolument',
'dynasty', 'woodrow', 'tramway', 'Ralph', 'package', 'coverall',
'Werner', 'ordinal', 'lead', 'fibrin', 'Lancashire', 'elusive',
'taste', 'Christ', 'whom', 'Roberta', 'virus', 'jostle',
'fidelity', 'cell', 'slice', 'witch', 'Antioch', 'Kelvin',
'audiovisual', 'end', 'jumble', 'dental', 'scrounge', 'onus',
'picosecond', 'squint', 'Stromberg', 'beacon', 'Byrne',
'extenuate', 'fasciculate', 'thunderstorm', 'Longfellow',
'Goldwater', 'Moloch', 'seethe', 'antarctic', 'mouthful',
'condominium', 'abduct', 'burbank', 'Sancho', 'estrus', 'guardian',
'buckeye', 'orthography', 'noble', 'commissary', 'cutout', 'new',
'walkout', 'Chungking', 'Breton', 'svelte', 'ore', 'persecutory',
'lycopodium', 'Bali', 'deflect', 'accusative', 'Perle', 'Scranton',
'diminish', 'awl', 'bilateral', 'triptych', 'inflorescent',
'Ptolemy', 'academician', 'gravitometer', 'integer', 'fluffy',
'ameliorate', 'thigh', 'infamous', 'Harvey', 'vacuole',
'competitor', 'filth', 'refrain', 'schoolgirl', 'hepatitis',
'Northrop', 'lodgepole', 'infelicity', 'slap', 'Helene',
'evaluate', 'sanicle', 'Bellatrix', 'regurgitate', 'glen',
'multifarious', 'triumph', 'talkative', 'acrophobia',
'lexicographer', 'enunciable', 'birthplace', 'Stamford',
'monastery', 'Zaire', 'tenacity', 'prologue', 'colloquial', 'owly',
'scam', 'summer', 'valeur', 'capsule', 'Koppers', 'photogenic',
'legerdemain', 'Heusen', 'symposium', 'jalopy', 'myoglobin',
'Ellsworth', 'preemptor', 'Euphrates', 'wore', 'Middletown',
'griffin', 'pansy', 'warfare', 'grub', 'repugnant', 'playa',
'deliverymen', 'homestead', 'midnight', 'brainchild', 'churchyard',
'frolicking', 'periphery', 'size', 'immersion', 'Greece',
'through', 'uppercut', 'pro', 'col']
df = pd.DataFrame(data={"words":data})
df_init = df.sample(n=100)
你的确切问题是什么?是否要从存储为.txt文件的200个项目列表中随机选择100个项目(单词)?如果是这样,您可以这样做:
import numpy as np
with open("word_list.txt", "r") as word_list:
words = word_list.readlines()
initial_population = np.random.choice(words, 4)
但是,我建议使用anon01提到的方法。您需要清楚地理解测试总体的概念,然后阅读
随机
软件包的教程或文档。堆栈溢出不是为了替换现有的文档和教程。我们希望您在发布问题之前完成此研究。