Skip to content

Latest commit

 

History

History
789 lines (610 loc) · 7.97 KB

File metadata and controls

789 lines (610 loc) · 7.97 KB
import numpy as np
array = np.array([1,2,3,4,5])
print(type(array))
array2 = array + 1
array2
array2 + array
array2 * array
array[0]
array[3]
array
array.shape
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
np.array([1,2,3,5,6])
np.array([1,2,3,5,6.0])
test_array = np.array([1,2,3,5,'6'])
test_array
type(test_array)
test_array.dtype
test_array.shape
test_array.itemsize
np.size(test_array)
np.shape(test_array)
test_array.ndim
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[0]
test_array[1:3]
test_array[-2:]
test_array = np.array([[1,2,3],[4,5,6],[7,8,9]])
test_array
test_array.shape
test_array.size
test_array.ndim
test_array[1,1]
test_array[1,1] = 10
test_array
test_array[1]
test_array[:, 1]
test_array[0,0:2]
test_array
test_array2 = test_array
test_array2
test_array2[1,1] = 100
test_array2
test_array
test_array2 = test_array.copy()
test_array2
test_array2[1,1] = 10
test_array2
test_array
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
test_array[mask]
random_array = np.random.rand(10)
random_array
mask = random_array > 0.5
mask
random_array[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.dtype
test_array.nbytes
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)
np.sum(test_array, axis=0)
np.sum(test_array, axis=1)
test_array.ndim
test_array.prod(axis=0)
test_array.prod(axis=1)
test_array.min()
test_array.min(axis=0)
test_array.min(axis=1)
test_array.max()
test_array.argmin()
test_array.argmin(axis=0)
test_array.argmin(axis=1)
test_array.argmax()
test_array.mean()
test_array.std()
test_array.std(axis=1)
test_array.var()
test_array
test_array.clip(2,4)
test_array = np.array([1.2,3.56,6.41])
test_array
test_array.round()
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)
np.sort(test_array, axis=0)
test_array
np.argsort(test_array)
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[index]
test_array = np.arange(10)
test_array
test_array.shape
test_array.shape = 2,5
test_array
test_array.reshape(1,10)
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 = test_array.squeeze()
test_array.shape
test_array.shape = 2,5
test_array
test_array.transpose()
test_array.T
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
c.shape
np.vstack((a,b))
np.hstack((a,b))
a
a.flatten()
np.array([1,2,3])
np.arange(10)
np.arange(2,20,2)
np.arange(2,20,2,dtype=np.float32)
np.linspace(0,10,10)
np.logspace(0,1,5)
x = np.linspace(-10,10,5)
x
np.zeros(3)
np.zeros((3,3))
np.ones((3,3))
np.ones((3,3)) * 8
np.ones((3,3), dtype = np.float32)
a = np.empty(6)
a.shape
a
a.fill(1)
a
test_array = np.array([1,2,3,4])
test_array
np.zeros_like(test_array)
np.ones_like(test_array)
np.identity(5)
x = np.array([5,5])
y = np.array([2,2])
np.multiply(x,y)
np.dot(x, y)
y.shape = 1,2
y
x.shape = 2,1
x
print(x)
print(y)
np.dot(x,y)
np.dot(y,x)
x = np.array([1,1,1,2])
y = np.array([1,1,1,4])
x == y
np.logical_and(x,y)
np.logical_or(x,y)
np.logical_not(x,y)
np.random.rand(3,2)
np.random.randint(10, size=(5,4))
np.random.rand()
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)
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')
data.keys()
data['a']