Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sample Dog Cat Tarsier
- A47 1 7 2
- A48 3 3 4
- A51 2 1 8
- A53 0 0 0
- A54 1 7 2
- A57 0 0 10
- Cat Tarsier
- A47 7 2
- struct row{
- char *name;
- int animal[3]; //id 0 is dog, 1 is cat and 2 is tarsier
- }
- int i;
- float sum=0; //we need float to force the result to be float
- for (i=0;i<3;i++){
- sum += row.animal[i]; //count the total row population
- }
- for (i=0;i<3;i++){ //for every animal
- if (row.animal[i]/sum <= 0.1){ //if this animal is equal or less than 10% of the row population
- row.animal[i]=0; //set his population to 0
- }
- }
- import csv
- def getvals(file):
- """
- gets the val's from a file of whitespace separated values, and
- turns them into easy to use Python var's
- """
- samples = csv.reader(open(file))
- s = []
- n = 0
- for row in samples:
- r = [row[0].split()]
- s += r
- n+=1
- return s
- [
- ['Sample', 'Dog', 'Cat', 'Tarsier'],
- ['A47', '1', '7', '2'],
- ['A48', '3', '3', '4'],
- ['A51', '2', '1', '8'],
- ['A53', '0', '0', '0'],
- ['A54', '1', '7', '2'],
- ['A57', '0', '0', '10']
- ]
- >>> data = np.genfromtxt('data.txt', delimiter="t", names=True, dtype=None)
- data = array([('A47', 1, 7, 2), ('A48', 3, 3, 4), ('A51', 2, 1, 8),
- ('A53', 0, 0, 0), ('A54', 1, 7, 2), ('A57', 0, 0, 10)],
- dtype=[('Sample', '|S3'), ('Dog', '<i8'), ('Cat', '<i8'), ('Tarsier', '<i8')])
- >>> data[["Cat","Tarsier"]]
- array([(7, 2), (3, 4), (1, 8), (0, 0), (7, 2), (0, 10)],
- dtype=[('Cat', '<i8'), ('Tarsier', '<i8')])
- >>> data[[0,2]]
- array([('A47', 1, 7, 2), ('A51', 2, 1, 8)],
- dtype=[('Sample', '|S3'), ('Dog', '<i8'), ('Cat', '<i8'), ('Tarsier', '<i8')])
- >>> data["Dog"].mean()
- 1.1666667
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement