Showing posts with label Learning. Show all posts
Showing posts with label Learning. Show all posts

Thursday, January 29, 2015

Being regular

1. Regularity is everything. Learning something for 2 hours for 25 alternate days is far, far superior to learning the same thing for 10 hours each on the first 5 days and then not coming back to it. On day 51, you will be in a much better position by following the first strategy.

2. If you don't intend to keep using a skill (WebDev, ML - anything) at least 2 to 3 times a week, for at least a couple of years, you might as well not learn it. You will unlearn it in as little as two to three months, and if you need that skill again you will have to start from the very beginning, making you question why you spent all that time learning it in the first place.

3. Set all non-focus but essential things on a reminder until it becomes an autopilot thing. This includes things such as exercising - essential, yes, but should not occupy your mental space and time. The time you spend actually exercising is all the time you should devote to it, nothing more. 

Basic R revision - Part 2

Factors

Factors are used to store categorical variables, where categorical variables are those whose value can only be one amongst a well-defined, discrete set of values. For example factor_gender is a factor that stores variables that can contain elements: "male" and "female".

To construct a factor variable out of a vector of values, just wrap the vector using factor(). For example:

> gender_vector = c("Male", "Female", "Female", "Male", "Male")
> factor_gender_vector = factor(gender_vector)
> factor_gender_vector
[1] Male   Female Female Male   Male 
Levels: Female Male

Categorical variables are of two types: nominal and ordinal.
factor_gender would be nominal as there is no grading from lower to higher between male and female unless you are a sexist asshole.
factor_bondratings would be ordinal as there is a natural grading, where we know :



AAA > AA > A > BBB > BB > CCC > CC > C > D

In R, the assumption in for the categorical nominal variable to be nominal. If you wish to specify ordinal, use the order and levels keywords:



temperature_vector = c("High","Low","High","Low","Medium")
factor_temperature_vector = factor(temperature_vector, order=TRUE, levels=c("Low","Medium","High"))
> factor_temperature_vector
[1] High   Low    High   Low    Medium
Levels: Low < Medium < High

Renaming the elements of a factor variable

Use the levels() function to do this.



> survey_vector = c("M", "F", "F", "M", "M")
> factor_survey_vector = factor(survey_vector)
> factor_survey_vector
[1] M F F M M
Levels: F M

> levels(factor_survey_vector) = c("Female", "Male")
> factor_survey_vector
[1] Male   Female Female Male   Male 
Levels: Female Male

Note that it is important to follow the correct order while naming. Using
levels(factor_survey_vector) = c("Female", "Male")
would have been incorrect, since I had run the code earlier to see the unnamed output being "Levels: F M"

Using summary()

summary() is a general R function but it's very useful with factors. For example:



> summary(factor_survey_vector)
Female   Male
     2      3

If a factor is nominal, then the comparison operator > becomes invalid. See the following (continuation) code for my favorite proof for the equality of sexes:



> # Battle of the sexes:
> # Male
> factor_survey_vector[1]
[1] Male
Levels: Female Male
> # Female
> factor_survey_vector[2]
[1] Female
Levels: Female Male
> # Male larger than female?
> factor_survey_vector[1] > factor_survey_vector[2]
'>' not meaningful for factors

Comparison operators meaningful for ordinal categorical variables. See:



> speed_vector = c("Fast", "Slow", "Slow", "Fast", "Ultra-fast")
> # Add your code below
> factor_speed_vector = factor(speed_vector, order = TRUE, levels = c("Slow", "Fast", "Ultra-fast"))
> # Print
> factor_speed_vector
[1] Fast       Slow       Slow       Fast       Ultra-fast
Levels: Slow < Fast < Ultra-fast
> # R prints automagically in the right order
> summary(factor_speed_vector)
      Slow       Fast Ultra-fast
         2          2          1

> compare_them = factor_speed_vector[2] > factor_speed_vector[5]
> # Is data analyst 2 faster than data analyst 5?
> compare_them
[1] FALSE

So Analyst 2 is not faster than Analyst 5.

Wednesday, January 28, 2015

Basic R revision - Part 1



A random useful function

To get the data type of of variable in R, use the function class().

my_numeric = 42
my_character = "forty-two"
my_logical = FALSE

> class(my_numeric)
[1] "numeric"
> class(my_character)
[1] "character"
> class(my_logical)
[1] "logical"

Always remember: Python/C++ vector indices start with 0, R vector indices start with 1
 Subset a vector in R, use vectorname[c(starting index: ending index)]
If disparate (non-adjacent elements): vectorname[c(index1, index2, index3 ..)]

Compare to Python:
Subset a vector in Python, use vectorname[starting index: ending index + 1]
Note that index numbers will be defined as per Python convention
Suppose from a vector v = ['P','O','K','E','R'], we need to output ['O','K','E']
In Python, use v[1:4]
In R, use v[c(2:4)] or just v[2:4]

Also to get all elements in Python a way is to do Mymatrix[3, : ] (gets row 3)
To do the same exercise in R the way to do is Mymatrix[3,  ]

Comparison Operators in R vs VBA and Python

Comparison Operators in R and C++
<, >, >=, ==, !=

Comparison operators in VBA
<, >, <=, =, <>
Python supports both != and
<>

For equality, Python supports == (like R and C++)

In R you can use comparison operator between a vector and a number and get a binary vector which compares each element of the vector to that number.
(Not sure if you can do that in Python. Will check later and update.) Also, you can use that binary vector as an index to get a subset of the original vector.

Matrix in R: To construct a matrix in R you need to add a matrix() wrapper to a vector. e.g. matrix(c(1:9), byrow=TRUE, nrow=3)

Naming elements of a vector and rows/cols of a matrix

Naming can often be useful later. Syntax is simple:
For vector:
vectorv = c(2,3,4)
names(vectorv)=c("a","b","c")
Now, vectorv[“a”]=2
Now, vectorv[“a”]=2

For matrix:
new_hope = c( 460.998007, 314.4)
empire_strikes = c(290.475067, 247.900000)
return_jedi = c(309.306177,165.8)
# Construct the matrix
star_wars_matrix = matrix(c(new_hope,empire_strikes,return_jedi), nrow=3, byrow=TRUE)
# Add your code here such that rows and columns of star_wars_matrix have a name!
rownames(star_wars_matrix) = c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
colnames(star_wars_matrix)= c("US", "non-US")

Another way can be to include these in the matrix definition itself:
movie_names = c("A New Hope","The Empire Strikes Back","Return of the Jedi")
col_titles = c("US","non-US")
star_wars_matrix = matrix(box_office_all, nrow=3, byrow=TRUE, dimnames=list(movie_names,col_titles))
Summing all elements of entire rows or columns, or summing all elements of any vector

To do row sums or column sums in R for a matrix just use rowSums(matrixname) or colSums(matrixname). Note that it is important to capitalize S in rowSums or colSums. Another way can be to reference the needed vector by using something like Mymatrix[3, ] and then wrapping sum() around it.

Combining/Appending functions

cbind(vectorname) can append a vector to an existing matrix as a new column, provided vector's length is same as number of matrix rows. Similarly, rbind. Note the similarity to c() wrapper to construct any vector.

Arithmetic Operators

+,-,*,/ work in an elementwise way for both vectors and matrices
matrix1 * matrix2 does elementwise multiplication, not matrix multiplication as in Linear Algebra for which we use %*% in R

Tuesday, January 27, 2015

Carbs vs Fats

In a comparison of macronutrients, the often misunderstood fats are way better than carbohydrates. Fats, especially the unsaturated ones, provide several essential functions such as protecting our inner organs, maintaining good cholesterol levels and reducing bad cholesterol. Outside of them, Omega 3 fats found in foods such as Tuna, Walnuts and Beans are one of the healthiest things you can consume for your brain function - improving memory, fighting depression, bipolar disorder and ADHD. In addition, it is good for your cardiovascular system and bone joints. Even some saturated fats, such as those found in Desi Ghee, help break down other hard to digest food and reduce the negative effects of other fried and spicy food you eat, making them easily digestible. The one category of fat that is unequivocally unhealthy is trans fats, which would be all fatty food items with a good shelf life - biscuits, chips, pies, donuts, cake etc. In comparison, there are no “essential” carbohydrates, that is, there are no essential body functions that require carbohydrates. So the only function carbs serve is to provide energy, which is provided in just as much quantity by more functional nutrients like fats and proteins.

Indian diet is very heavy on carbs, which provide energy but aid no body function, except that a basic amount is needed to aid digestion. You would be surprised that that basic amount is so little as 3 slices of bread a day for a full grown adult, and that is if that were all the carbs he were consuming. Of course, an average adult will also be consuming carbs in decent quantities from vegetables, beans and fruits.
__


Disclaimer: This is a log for my personal use and does not constitute medical advice from me. I am not qualified to give medical advice to others and you should consult your doctor before making any nutritional or medicinal changes. 

Tuesday, January 20, 2015

World changing developments

I read a lot of history stuff at the beginning of this year, where my focus was on breadth rather than depth. I did not study in great detail about any one period, wanting instead to get some sort of a hang of everything. Of course, I failed miserably. But I did come out at the end of those five or six days subconsciously wondering about the developments, from a very wide angle camera, that I thought really changed the world the most. Even though a lot of entries on my list are so obvious when I look at the list ex-post that I wonder why I needed to spend those days to arrive at it, but, in any case, here's my list:

1) Discovery of fire - Because duh uh.

2) Discovery that seeds can be cultivated - This was the single most important developments that changed humans from hunting gathering nomads to co-existing people in civilizations.

3) Mass Production and standardized parts - Pioneered by the development of the Chinese crossbow

4) Concrete - Changed the way structures were built forever. Introduced by the Roman emperor Claudius while getting Aqua Claudia built, an aqueduct colossal even by modern standards.

5) Printing Press - Because it changed literacy from 2-3% to 70-80% in the western world in a matter of decades. That is a great leap for mankind.

6) Steam Engine - Because it paved the way for the Industrial revolution which would give us trains, cars, other engines, heavy machinery, and pretty much everything.

6) Malaria and Polio vaccines - Because you can now expect to celebrate your kindergarten birthday party.

7) Internet - Because I say so.

And now, some trivia that I found fascinating. Two of the greatest empires - the Egyptian and the Roman - were decimated by people one would never guess were capable of it. The Egyptian one, the largest empire of its time and perhaps the most long lasting one ever with a period of continuous rule between 1000 to 1500 years, was brought down by people nobody knew at that time even existed, and historians still don't know where they came from. They were people whom the Egyptians called "People of the Sea", because they came from the sea. The Roman empire, the biggest  ever, was brought down, once again, by a small tribe of germanic people called The Vandals under the leadership of Gaiseric - not by another big empire of that time such as the Persian or the Chinese.

Finally, next on my history reading is "Guns, Germs and Steel", again a book that focuses on breadth (history of the world from pre-civilization to today) rather than depth into a particular place or era, although it does supposedly go into great depths vis-a-vis outlining some specific themes that pervade all of history. I have heard good things about the book, but I cannot say when I will get around to it.