Commands Overview - Nori12/Machine-Learning-Tutorial GitHub Wiki

Machine Learning Tutorial

Numpy

Import:

import numpy as np

Declare an ndarray

data = np.array([1, 2, 3], [4, 5, 6](/Nori12/Machine-Learning-Tutorial/wiki/1,-2,-3],-[4,-5,-6))

# [[1 2 3]
#  [4 5 6]]

Shape of matrix

data.shape

# (2, 3)

Identity matrix

data = np.eye(2)

# [[1. 0.]
#  [0. 1.]]

Ones matrix

data = np.ones(4)

# [1. 1. 1.]

Zeros matrix

data = np.zeros(4)

# [1. 1. 1.]

Sequence matrix

# Return evenly spaced values within a given interval.
# (start, end, interval)
x = np.arange(4, 8, 2)

# [4 6]

Range

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

x = range(3, 11, 2)
# x = 3, 5, 7, 9

Linear space matrix

# (start value, end value, number of samples)
data = np.linspace(-1, 1, 11)

# [-1.  -0.8 -0.6 -0.4 -0.2  0.   0.2  0.4  0.6  0.8  1. ]

Reshape

array = np.linspace(0, 1, 6)
print("array: {}\n".format(line))

# array: [0.  0.2 0.4 0.6 0.8 1. ]

new_array = array.reshape(-1, 2) # -1 here means: it doesn't matter how many columns, but put in 2 columns each
print("new_array: {}\n".format(line2))

# new_array: 
# [[0.  0.2]
#  [0.4 0.6]
#  [0.8 1. ]]

Transpose matrix

data = np.array([1, 2, 3]).reshape(-1,1)

# [1], [2], [3](/Nori12/Machine-Learning-Tutorial/wiki/1],-[2],-[3)

Cross Product

a = np.array([1, 2])
b = np.array([2], [1](/Nori12/Machine-Learning-Tutorial/wiki/2],-[1))
c = a.dot(b)

# c = [4] -> Still an ndarray although looks like a scalar

Trigonometric functions

# Radians
# Possible to set a condition with more arguments
data = np.sin(x)
data = np.cos(x)
data = np.tan(x)

Squeeze

Remove single-dimensional entries from the shape of an array.

x = np.array([[0], [1], [2](/Nori12/Machine-Learning-Tutorial/wiki/[0],-[1],-[2)])
# x.shape = (1, 3, 1)

np.squeeze(x).shape
# (3,)

np.squeeze(x, axis=0).shape
# (3, 1)

np.squeeze(x, axis=1).shape
# Error: cannot select an axis to squeeze out which has size not equal to one.

np.squeeze(x, axis=2).shape
# (1, 3)

x = np.array([1234](/Nori12/Machine-Learning-Tutorial/wiki/1234))
# x.shape = (1, 1)

np.squeeze(x)
# array(1234)  # 0d array

np.squeeze(x).shape
# ()

Enumerate

my_list = ['apple', 'banana', 'grapes', 'pear']

for counter, value in enumerate(my_list):
    print counter, value

# 0 apple
# 1 banana
# 2 grapes
# 3 pear

Logarithms

The natural logarithm is logarithm in base e. log(exp(x)) = x. Log in base 10 log10(). Log in base 2 log2().

np.log([1, np.e, np.e**2, 0])
# array([  0.,   1.,   2., -Inf])

Ravel

Return a contiguous flattened array. (What does the order parameter really means?)

x = np.array([1, 2, 3], [4, 5, 6](/Nori12/Machine-Learning-Tutorial/wiki/1,-2,-3],-[4,-5,-6))
np.ravel(x)
# array([1, 2, 3, 4, 5, 6])

x.reshape(-1)
# array([1, 2, 3, 4, 5, 6])

SciPy

from scipy import sparse
  • CSR Matrix
eye_sparse_matrix = sparse.csr_matrix(eye)

#   (0, 0)        1.0
#   (1, 1)        1.0
#   (2, 2)        1.0
  • COO Matrix
eye_coo = sparse.coo_matrix((data, (row_indices, col_indices)))

#   (0, 0)        1.0
#   (1, 1)        1.0
#   (2, 2)        1.0

Matplotlib

import matplotlib.pyplot as plt
  • Complete plot
plt.plot(x, y, marker="x")
plt.show()

Pandas

import pandas as pd
  • Create a simple dataset
data = {'Name': ["John", "Anna", "Peter", "Linda"],
        'Location': ["New York", "Paris", "Berlin", "London"],
        'Age': [24, 13, 53, 33]}

data_pandas = pd.DataFrame(data)

data_pandas[data_pandas.Age > 30]

#     Name Location  Age
# 2  Peter   Berlin   53
# 3  Linda   London   33