-
Notifications
You must be signed in to change notification settings - Fork 0
Commands Overview
Nori12 edited this page Aug 9, 2020
·
41 revisions
- Import
- Declare an ndarray
- Shape of matrix
- Identity matrix
- Ones matrix
- Zeros matrix
- Sequence matrix
- Range
- Linear space matrix
- Reshape
- Transpose matrix
- Cross Product
- Squeeze
- Logarithms
- Enumerate
- Ravel
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6]])
# [[1 2 3]
# [4 5 6]]
data.shape
# (2, 3)
data = np.eye(2)
# [[1. 0.]
# [0. 1.]]
data = np.ones(4)
# [1. 1. 1.]
data = np.zeros(4)
# [1. 1. 1.]
# Return evenly spaced values within a given interval.
# (start, end, interval)
x = np.arange(4, 8, 2)
# [4 6]
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
# (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. ]
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. ]]
data = np.array([1, 2, 3]).reshape(-1,1)
# [[1], [2], [3]]
a = np.array([1, 2])
b = np.array([[2], [1]])
c = a.dot(b)
# c = [4] -> Still an ndarray although looks like a scalar
# Radians
# Possible to set a condition with more arguments
data = np.sin(x)
data = np.cos(x)
data = np.tan(x)
Remove single-dimensional entries from the shape of an array.
x = np.array([[[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]])
# x.shape = (1, 1)
np.squeeze(x)
# array(1234) # 0d array
np.squeeze(x).shape
# ()
my_list = ['apple', 'banana', 'grapes', 'pear']
for counter, value in enumerate(my_list):
print counter, value
# 0 apple
# 1 banana
# 2 grapes
# 3 pear
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])
Return a contiguous flattened array. (What does the order parameter really means?)
x = np.array([[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])
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
import matplotlib.pyplot as plt
- Complete plot
plt.plot(x, y, marker="x")
plt.show()
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