Python and numpy notes
import pandas as pd
import numpy as np
python_list = [0,1,2,3,4,5,6,7,8,9]
type(python_list)
numpy_array = np.array(python_list)
type(numpy_array)
numpy_array[::-1]
matrix = [[1,2,3],
[4,5,6],
[7,8,9]]
print(matrix)
print(type(matrix))
numpy_matrix = np.array(matrix)
print(numpy_matrix)
print(type(numpy_matrix))
numpy_matrix[1:,0:2]
Data type
float_array = np.array([1,2,3,4,5], dtype='float64')
print(float_array)
print(float_array.dtype)
print(type(float_array))
astype_array = float_array.astype(np.float64)
astype_array
boolean_array = astype_array.astype(np.bool_)
boolean_array
string_array = np.array([1,2,3,4,5])
string_array = string_array.astype(np.string_)
string_array
int_array = np.array([0,1,2,3,4,5])
int_array = int_array.astype(np.int8)
int_array
Dimentions
scalar = np.array(42)
print(scalar)
scalar.ndim
vector = np.array([1,2,3])
print(vector)
vector.ndim
three_dimensional_matrix = np.array([
[[1,2,3],
[4,5,6],
[7,8,9]],
[[1,2,3],
[4,5,6],
[7,8,9]]
])
print(three_dimensional_matrix)
three_dimensional_matrix.ndim
ten_vector = np.array([1,2,3],ndmin=10)
print(ten_vector)
ten_vector.ndim
expand = np.expand_dims(np.array([1,2,3]), axis=1)
print(expand)
expand.ndim
print(ten_vector, ten_vector.ndim)
second_vector = np.squeeze(ten_vector)
print(second_vector, second_vector.ndim)
Creating arrays
list(range(0,10))
np.arange(1,20,2)
np.zeros(3)
np.zeros((10,10))
np.ones((10,10))
np.linspace(1,10,100)
np.eye(4)
np.random.rand(5,5)
np.random.randint(1,20)
np.random.randint(1,100,(10,10))