Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np\n",
- "from tqdm import tqdm"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "def calc_meanstd(groups):\n",
- " means = [np.mean(x) for x in groups]\n",
- " mean = np.mean(means)\n",
- " meanstd = 0\n",
- " for i in means:\n",
- " meanstd+=np.abs(i-mean)\n",
- " return float(meanstd)\n",
- "def split_group(group, split_num, number):\n",
- " result = []\n",
- " for i in range(split_num):\n",
- " result.append(group[i*number:(i+1)*number])\n",
- " return result\n",
- "def make_group(group_list, split_num, try_num):\n",
- " print('人数:',len(group_list),'分割数:',split_num)\n",
- " if len(group_list)%split_num!=0:\n",
- " print('均等に割り切れません')\n",
- " return 0\n",
- " group_num = int(len(group_list)/split_num)\n",
- " \n",
- " result = {}\n",
- " for _ in tqdm(range(try_num)):\n",
- " np.random.shuffle(group_list)\n",
- " tmp_result = split_group(group_list,split_num,group_num)\n",
- " result[calc_meanstd(tmp_result)] = tmp_result\n",
- " sort_result = sorted(result.items())\n",
- " return sort_result[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "人数: 24 分割数: 3\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|█████████████████████████████████| 10000/10000 [00:00<00:00, 16167.84it/s]\n"
- ]
- }
- ],
- "source": [
- "a=make_group(list(range(1,25)),3, 10000)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([100, 100, 100])"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.sum(a[1], axis=1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [],
- "source": [
- "test = [8, 9, 9, 3, 5, 8, 9, 9, 8, 10, 10, 10, 9, 10, 10, 10, 6, 7, 10, 9, 10]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "人数: 21 分割数: 3\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████| 100000/100000 [00:06<00:00, 16259.33it/s]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "(0.1904761904761898,\n",
- " [[9, 6, 10, 10, 8, 8, 9], [10, 9, 10, 10, 9, 9, 3], [10, 8, 10, 10, 5, 7, 9]])"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "make_group(test,3, 100000)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment