Generate 4 columns of data such that each row sum to 100

2024/11/16 13:33:43

How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?

Answer
>>> import numpy as np
>>> A = np.random.rand(10,4)
>>> A /= A.sum(axis=1)[:,np.newaxis]
>>> A *= 100
>>> Aarray([[ 52.65020485,   8.39068184,   4.89730114,  34.06181217],[ 58.32667159,   8.99338257,  13.7326809 ,  18.94726494],[  8.23847677,  36.27990343,  14.73440883,  40.74721097],[ 37.10408209,   5.31467062,  39.47977538,  18.10147191],[ 21.5697797 ,  14.80630725,  12.69891923,  50.92499382],[ 15.46006657,  24.62499701,  37.37736874,  22.53756768],[  6.66777748,  25.62326117,  11.80042839,  55.90853296],[ 38.81602256,  26.74457165,   3.4365655 ,  31.00284028],[  5.67431732,   7.57571558,  44.01330459,  42.73666251],[ 33.09837171,  26.66421892,  10.90188895,  29.33552043]])

This generates positive real numbers as you asked. They will be random in the uniform distribution. If you want a different distribution, you can find several other choices in np.random.

https://en.xdnf.cn/q/120321.html

Related Q&A

Nan to Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a NaN. I know that to convert an array where there are numbers that are NaN you…

Class constructor able to init with an instance of the same class object

Can python create a class that can be initialised with an instance of the same class object?Ive tried this:class Class(): def __init__(self,**kwargs):print selfself = kwargs.get(obj,self)print selfif …

Python Logical Operators

I am currently learning Python3, but I dont understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that…

Partial Pivoting In Pandas SQL Or Spark

Partial Pivoting In Pandas SQL Or Spark Make the Year remain as Rows, and have the States Transpose to columns Take Pecentage value of a Gender Male Race White, InputOutput

Python to create a find-replace dictionary from a word vba macro

I have a very big macro Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.Find.Text = "asa".Replacement.Text = "fsa".Forward = True.Wrap = wdFin…

Converting many .txt files into csv and combining them

I have many .txt files. I want to convert a few files ending with specific names into csv and combine them into one csv. ### Folder Name: text_files python_gramm.py aadd01.txt aaxx02.txt aaff03.txt hhd…

Calculation between two columns in Python?

When I tried to do some calculation between two columns (like division), I get an error: column_ratio[x]=(float(column1[y]))/(float(column2[z])) TypeError: tuple indices must be integers, not str. C…

Why does input() always return a string?

Here is my code:age = input("How old are you?: ") print (age) print (type(age))Result:How old are you?: 3535class str <<--- This is problem!But, If I use.. age = int(input("How …

Windowed mode cannot run

Why does pyinstaller exe not run in windowed mode but fine without it? I have changed over to a windows OS from Linux. Never had any issue before hand, how do I correct this.

inserting a variable into an fstring using .replace()

I have a code something similar to bellow. name = Dave message = f<name> is a really great guy! message = message.replace(<name>, {name}) print(message)the variables are a little more compl…