array = np.array([1,2,3,4,5])
print(type(array))
array2 = array + 1
array2
np.array([[1,2,3],[4,5,6]])
np.array([[[1,2,3],[4,5,6]]])
np.array([[[1,2,3],[4,5,6]]]).shape
test_array = np.array([1,2,3,5,'6'])
test_array
test_array = np.array([1,2,3,4,5])
test_array
test_array.fill(0)
test_array
test_list = [1,2,3,5,6]
test_array = np.array(test_list)
test_array
test_array = np.array([[1,2,3],[4,5,6],[7,8,9]])
test_array
test_array[1,1] = 10
test_array
test_array2 = test_array
test_array2
test_array2[1,1] = 100
test_array2
test_array2 = test_array.copy()
test_array2
test_array2[1,1] = 10
test_array2
test_array = np.arange(0,100,10)
test_array
mask = np.array([0,0,0,1,1,1,0,0,1,1],dtype=bool)
mask
random_array = np.random.rand(10)
random_array
mask = random_array > 0.5
mask
import os
print(os.path.abspath('.'))
test_array = np.array([10,20,30,40,50])
test_array > 30
np.where(test_array > 30)
test_array[np.where(test_array > 30)]
test_array = np.array([1,2,3,4,5],dtype=np.float32)
test_array
test_array = np.array([1,10,3.5,'str'],dtype=np.object)
test_array
test_array = np.array([1,2,3,4,5])
test_array
test_array.astype(np.float32)
test_array = np.array([[1,2,3],[4,5,6]])
test_array
np.sum(test_array, axis=0)
np.sum(test_array, axis=1)
test_array.argmin(axis=0)
test_array.argmin(axis=1)
test_array = np.array([1.2,3.56,6.41])
test_array
test_array.round(decimals=1)
test_array = np.array([[1.5,1.3,7.5],[5.6,7.8,1.2]])
test_array
np.sort(test_array, axis=0)
test_array = np.linspace(0,10,10)
test_array
values = np.array([2.5,6.5,9.5])
values
np.searchsorted(test_array,values)
test_array = np.array([[1,0,6],[1,7,0],[2,3,1],[2,4,0]])
test_array
index = np.lexsort([-1*test_array[:,0],test_array[:,2]])
index
test_array = np.arange(10)
test_array
test_array.shape = 2,5
test_array
test_array = np.arange(10)
test_array.shape
test_array = test_array[np.newaxis,:]
print(test_array)
test_array.shape
test_array = np.arange(10)
test_array.shape
test_array = test_array[:,np.newaxis]
print(test_array)
test_array.shape
test_array = test_array[:,np.newaxis,np.newaxis]
test_array.shape
test_array = test_array.squeeze()
test_array.shape
test_array.shape = 2,5
test_array
a = np.array([[123,456,678],[3214,456,134]])
a
b = np.array([[1235,3124,432],[43,13,134]])
b
c = np.concatenate((a,b))
c
np.concatenate((a,b), axis=0)
c = np.concatenate((a,b), axis=1)
c
np.arange(2,20,2,dtype=np.float32)
x = np.linspace(-10,10,5)
x
np.ones((3,3), dtype = np.float32)
test_array = np.array([1,2,3,4])
test_array
np.zeros_like(test_array)
x = np.array([5,5])
y = np.array([2,2])
x = np.array([1,1,1,2])
y = np.array([1,1,1,4])
x == y
np.random.randint(10, size=(5,4))
np.random.random_sample()
np.random.randint(0,10,3)
mu, sigma = 0, 0.1
np.random.normal(mu, sigma, 10)
np.set_printoptions(precision=2)
mu, sigma = 0, 0.1
np.random.normal(mu, sigma, 10)
test_array = np.arange(10)
test_array
np.random.shuffle(test_array)
np.random.seed(100)
mu, sigma = 0, 0.1
np.random.normal(mu, sigma, 10)
data = []
with open('./data/test1.txt') as f:
for line in f.readlines():
fileds = line.split()
cur_data = [float(x) for x in fileds]
data.append(cur_data)
data = np.array(data)
data
data = np.loadtxt('./data/test1.txt')
data
data = np.loadtxt('./data/test2.txt', delimiter=',')
data
data = np.loadtxt('./data/test3.txt',delimiter=',',skiprows=1)
data
test_array = np.array([[1,2,3],[4,5,6]])
test_array
np.savetxt('./data/test4.txt', test_array)
np.savetxt('./data/test4.txt', test_array, fmt='%d')
np.savetxt('./data/test4.txt', test_array, fmt='%d', delimiter=',')
np.savetxt('./data/test4.txt', test_array, fmt='%.2f', delimiter=',')
test_array = np.array([[1,2,3],[4,5,6]])
np.save('./data/test_array.npy', test_array)
test_array2 = np.load('./data/test_array.npy')
test_array2
test_array2 = np.arange(10)
test_array2
np.savez('./data/test.npz', a=test_array, b=test_array2)
data = np.load('./data/test.npz')