0% found this document useful (0 votes)
2 views

Machine Learning-Intro

The document outlines a Machine Learning course taught by Ya-Mei Chang, including contact information, office hours, and grading criteria. It lists the main textbook and a reference book, along with an introduction to R programming, covering basic commands, functions, and data manipulation techniques. Additionally, it provides examples of generating random numbers, computing statistics, and creating graphics in R.

Uploaded by

hubertkuo418
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Machine Learning-Intro

The document outlines a Machine Learning course taught by Ya-Mei Chang, including contact information, office hours, and grading criteria. It lists the main textbook and a reference book, along with an introduction to R programming, covering basic commands, functions, and data manipulation techniques. Additionally, it provides examples of generating random numbers, computing statistics, and creating graphics in R.

Uploaded by

hubertkuo418
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Machine Learning

Lecturer :Ya-Mei Chang

Office: Room 446, ext 66117

[email protected]
Textbook
Title: An Introduction to Statistical Learning: with Applications in R, 2021
Authors: G. James, D. Witten, T. Hastie and R. Tibshirani

Reference Book
Title: The Elements of Statistical Learing: Data mining, Inference and Prediction
Authors: D. Hastie, R. Tibshirani and J. Friedman

Grading:
⚫ Attendance 10%
⚫ Mark of usual 30%
⚫ Midterm Exam 30%
⚫ Final Report 30%

Office hours:
Tue. 10:00~11:00
Thr. 10:00~11:00
Introduction to R
Basic Commands: vector

x <- c(1, 3, 2, 5)
x

## [1] 1 3 2 5

We can also save things using = rather than <-:

x = c(1, 6, 2)
x

## [1] 1 6 2

y = c(1, 4, 3)

We can tell R to add two sets of numbers together. It will then add the first number
from x to the first number from y, and so on. However, x and y should be the same
length. We can check their length using the length() function.

length (x)

## [1] 3

length (y)

## [1] 3

x + y

## [1] 2 10 5

The ls() function allows us to look at a list of all of the objects, such as data and
functions, that we have saved so far. The rm() function can be used to delete any
that we don’t want.

ls ()

## [1] "x" "y"

rm(x, y)
ls ()

## character(0)
It’s also possible to remove all objects at once:

rm( list = ls ())

We can create a simple matrix.

x <- matrix (c(1, 2, 3, 4) , nrow = 2, ncol = 2)


x

## [,1] [,2]
## [1,] 1 3
## [2,] 2 4

Alternatively, the byrow = TRUE option can be used to populate the matrix in order
of the rows.

x <- matrix (c(1, 2, 3, 4) , 2, 2, byrow = TRUE)


x

## [,1] [,2]
## [1,] 1 2
## [2,] 3 4

The sqrt() function returns the square root of each element of a vector or matrix.
The command x^2 raises each element of x to the power 2; any powers are possible,
including fractional or negative powers.

sqrt (x)

## [,1] [,2]
## [1,] 1.000000 1.414214
## [2,] 1.732051 2.000000

x^2

## [,1] [,2]
## [1,] 1 4
## [2,] 9 16
The rnorm() function generates a vector of random normal variables, with the first
argument n the sample size. Each time we call this function, we will get a diferent
answer. Here we create two correlated sets of numbers, x and y, and use the cor()
function to compute the correlation between them.

x <- rnorm (50)


y <- x + rnorm (50 , mean = 50, sd = .1)
cor (x, y)

## [1] 0.9943504

By default, rnorm() creates standard normal random variables with a mean of 0 and
a standard deviation of 1. Sometimes we want our code to reproduce the exact same
set of random numbers; we can use the set.seed() function to do this.

set.seed (1303)
rnorm (50)

## [1] -1.1439763145 1.3421293656 2.1853904757 0.5363925179 0.06


31929665
## [6] 0.5022344825 -0.0004167247 0.5658198405 -0.5725226890 -1.11
02250073
## [11] -0.0486871234 -0.6956562176 0.8289174803 0.2066528551 -0.23
56745091
## [16] -0.5563104914 -0.3647543571 0.8623550343 -0.6307715354 0.31
36021252
## [21] -0.9314953177 0.8238676185 0.5233707021 0.7069214120 0.42
02043256
## [26] -0.2690521547 -1.5103172999 -0.6902124766 -0.1434719524 -1.01
35274099
## [31] 1.5732737361 0.0127465055 0.8726470499 0.4220661905 -0.01
88157917
## [36] 2.6157489689 -0.6931401748 -0.2663217810 -0.7206364412 1.36
77342065
## [41] 0.2640073322 0.6321868074 -1.3306509858 0.0268888182 1.04
06363208
## [46] 1.3120237985 -0.0300020767 -0.2500257125 0.0234144857 1.65
98706557
The mean() and var() functions can be used to compute the mean and variance of a
vector of numbers. Applying sqrt() to the output of var() will give the standard
deviation. Or we can simply use the sd() function.

set.seed (3)
y <- rnorm (100)
mean (y)

## [1] 0.01103557

var (y)

## [1] 0.7328675

sqrt ( var (y))

## [1] 0.8560768

sd(y)

## [1] 0.8560768

Graphics

x <- rnorm (100)


y <- rnorm (100)
plot (x, y)
plot (x, y, xlab = " this is the x- axis ",ylab = " this is the y- axi
s ",main = " Plot of X vs Y")

The function seq() can be used to create a sequence of numbers.

x <- seq (1, 10)


x

## [1] 1 2 3 4 5 6 7 8 9 10

x <- 1:10
x

## [1] 1 2 3 4 5 6 7 8 9 10

x <- seq (-pi , pi , length = 50)

Indexing Data

A <- matrix (1:16 , 4, 4)


A

## [,1] [,2] [,3] [,4]


## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16

A[2, 3]

## [1] 10
A[c(1, 3) , c(2, 4)]

## [,1] [,2]
## [1,] 5 13
## [2,] 7 15

A[1:3 , 2:4]

## [,1] [,2] [,3]


## [1,] 5 9 13
## [2,] 6 10 14
## [3,] 7 11 15

A[1:2 , ]

## [,1] [,2] [,3] [,4]


## [1,] 1 5 9 13
## [2,] 2 6 10 14

A[, 1:2]

## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8

A[1, ]

## [1] 1 5 9 13

A[-c(1, 3) , ]

## [,1] [,2] [,3] [,4]


## [1,] 2 6 10 14
## [2,] 4 8 12 16

A[-c(1, 3) , -c(1, 3, 4)]

## [1] 6 8

dim (A)

## [1] 4 4

You might also like