More code in Jupyter Notebook

I have uploaded a second PDF as a continuation of coding in this environment. You can copy paste the code and follow along if you’d like.

Introductory Code in Python_cont

A screenshot of the environment is depicted below:

2017-10-22 15_13_49-Introductory Code in Python_cont.png
Inside the Jupyter Notebook
Advertisements

Lists and Data Frames in R

Copy and paste the following code to your R Studio platform or R version:

# Creating a list

name = “Harvey”
student.id = “1992”
subjects = c(“English”, “Law”, “Mathematics”, “Computer Science”)
marks = c(70, 95, 85, 90)

name
student.id
subjects
marks

student.info <- list(name, student.id, subjects, marks)
student.info

# Creating a smaller list
student.info2 <- list(c(“Alan”, “Denny”), 28)
student.info2

# It is a good habit to name the elements of a list
names(student.info) <- c(“Name”, “StudentID”, “Subjects”, “Marks”)
student.info

# Accessing the elements of a list
student.info[3]
student.info$Subjects
student.info$Name

student.info[[3]][2]
student.info[[3]][4]

# Creating a new element in the list
student.info[5] <- “83.75%”
names(student.info)[5] <- “Percentage”
student.info

# Removing an element
student.info$Percentage <- NULL
student.info

# Merging lists
print(student.info)
print(student.info2)

merged.list <- c(student.info, student.info2)
merged.list

names(merged.list)[5:6] <- c(“OldNames”, “OldMark”)
merged.list

# Data Frames

Names <- c(“Harvey”, “Alan”, “James”)
Age <- c(22, 23, 24)
Height <- c(1.70, 1.75, 1.80)

D.frame <- data.frame(Names, Age, Height)
D.frame

D.frame$Salary <- c(5000, 6000, 8000)
D.frame
D.frame$Names

# Loading the library to obtain the Boston data set
library(MASS)
head(Boston)
dim(Boston)
names(Boston)
str(Boston)

head(Boston[[1]])
head(Boston[[“crim”]])

# Observe the difference in the output

head(Boston[1])
head(Boston[“crim”])

25 Incredible Facts About Albert Einstein

If you ever ask anyone to name a physicist, I can all but guarantee you they will name Albert Einstein. The physicist was revolutionary, changing the way we thought about the entire world both visible and invisible. He helped win the war and discovered our universe. Here we have 25 facts about the scientific legend, Albert Einstein.

Source: 25 Incredible Facts About Albert Einstein

Jupyter, Anaconda and Python

I have to admit that my journey with Python is starting with a lot of excitement and knowledge spreading. There were so many concepts and tools that I was unaware of, but, I will try to document the most important ones here, as a means to facilitate knowledge storage, spread and discovery. I started writing Python code using the Jupyter notebook, as provided by the Anaconda distribution.

IPython Notebook is a web-based interactive computational environment for creating IPython notebooks. An IPython notebook is a JSON document containing an ordered list of input/output cells which can contain code, text, mathematics, plots and rich media.

IPython notebooks can be converted to a number of open standard output formats, like HTML, presentation slides, markdown and Python. 

In 2014, Fernando Pérez announced a spin-off project from IPython called Project Jupyter.[11] IPython will continue to exist as a Python shell and a kernel for Jupyter, while the notebook and other language-agnostic parts of IPython will move under the Jupyter name. Jupyter added support for Julia, R, Haskell and Ruby.

Source: Wikipedia IPython

Here is the link to the download site:

The Jupyter Notebook

2017-10-21 23_14_25-Project Jupyter _ Install.png
The instructions of how to install Jupyter using the Anaconda Distribution

 

You can get many functionalities, including the Jupyter Notebook, by the Anaconda Distribution, in this link:

Anaconda

I have uploaded, as a PDF, my first attempt in this environment and attaching it here. You can copy paste the code and follow along if you’d like.

Introductory Code in Python

A screenshot of the environment is depicted below:

2017-10-21 23_12_32-Introductory Code in Python.png
Inside the Jupyter Notebook

Matrices in R

Copy and paste the following code to your R Studio platform or R version:

# Matrices in R

?matrix

mat1 <- matrix(c(10, 9, 8, 7, 6, 5), nrow = 3, ncol = 2, byrow = TRUE)
# apparently the number of elements in the vector should be equal to the
# number we obtain when we multiply the nrow with the ncol number

print(mat1)
View(mat1)

# Accessing the matrix elements
mat1[2,2] # row, column in the matrix
mat1[3,1]
mat1[2, c(1,2)] # go to the second row and print the elements 1 and 2

mat2 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2, byrow = FALSE)
mat2

mat3 <- matrix(c(1:9), nrow = 3, ncol = 3, byrow = TRUE)
mat3
mat3[ ,3]
mat3[2, ]
mat3[ ,c(1,3)]

my.data <- 1:20
my.data

A <- matrix(my.data, 4, 5)
A
A[2,3]

B <- matrix(my.data, 4, 5, byrow = TRUE)
B
B[2,5]

 

# rbind()

r1 <- c(“I”, “am”, “happy”)
r2 <- c(“What”, “a”, “day”)
r3 <- c(1, 2, 3)
C <- rbind(r1, r2, r3)
C

# cbind()

c1 <- 1:5
c2 <- -1:-5
D <- cbind(c1, c2)
D
D[1,2]

# Named vectors

Charlie <- 1:5
Charlie

names(Charlie)
names(Charlie) <- c(“N1”, “N2”, “N3”, “N4”, “N5”)
Charlie
Charlie[“N4”]
names(Charlie)

# Clear names

names(Charlie) <- NULL
Charlie

# Naming matrix dimensions

temp.vec <- rep(c(“James”, “Harvey”, “Alan”), each = 3)
# instead of times, we replicate the names 3 times each
temp.vec

bravo <- matrix(temp.vec, nrow = 3, ncol = 3)
bravo

row.names(bravo) <- c(“First”, “Second”, “Third”)
colnames(bravo) <- c(“Name1”, “Name2”, “Name3”)
bravo
bravo[“First”, ]
row.names(bravo)
bravo[“Third”, “Name1”] <- “Denny”
bravo

# Matrix Operations
mat3
mat3 <- mat3 – 1
mat3

# checking the dimensions of a matrix
dim(mat3) # number of rows and columns

mat2

mat4 <- mat2 + mat3
# not the same number of rows and columns in the individual matrices
mat4 <- mat3 + mat3
mat4

# interchanging the rows and columns in a matrix is known as transpose
print(mat4)
mat5 <- t(mat4)
mat5

# combining matrices:
# columnwise, only when we have the same number of rows
# rowise, only when we have the same number of columns

mat6 <- matrix(c(10, 11, 12), nrow = 3, ncol = 1)
mat6

combined.c <- cbind(mat5, mat6)
combined.c

combined.r <- rbind(mat4, mat5)
combined.r