import numpy as np
#convert to and create numpy nd(imensional)arrays
data1 = [10, 20, 30]
print(f"data1 is {type(data1)}")
data1 = np.array(data1)
print(f"data1 is now {type(data1)}")
#one dimensional
data2 = np.array([5, 10, 15, 20])
print(f"data2=\n{data2}")
print(f"data2 dimensions: {data2.shape}")
#two dimensional
data3 = np.array([[11,22],
[33,44],
[55,66]])
print(f"data3=\n{data3}")
print(f"data3 dimensions: {data3.shape}")
data4 = np.array([[[1,2,3], [4,5,6]],
[[1,2,3], [4,5,6]]])
print(f"data4=\n{data4}")
print(f"data4 dimensions: {data4.shape}")
data1 is <class 'list'> data1 is now <class 'numpy.ndarray'> data2= [ 5 10 15 20] data2 dimensions: (4,) data3= [[11 22] [33 44] [55 66]] data3 dimensions: (3, 2) data4= [[[1 2 3] [4 5 6]] [[1 2 3] [4 5 6]]] data4 dimensions: (2, 2, 3)
# positive indexing
# 0 1 2 3 4
# [11,22,33,44,55]
# -5 -4 -3 -2 -1
# negative indexing
data1 = np.array([11,22,33,44,55])
#indexing one dimensional array
print(f"data1={data1}")
print(f"data1[0]={data1[0]}",f"data1[1]={data1[1]}")
print(f"data1[-1]={data1[-1]}",f"data1[-2]={data1[-2]}",f"data1[-5]={data1[-5]}")
data1=[11 22 33 44 55] data1[0]=11 data1[1]=22 data1[-1]=55 data1[-2]=44 data1[-5]=11
data2 = np.array([[11,22],
[33,44],
[55,66]])
#indexing two dimensional array
print(f"data2=\n{data2}")
#element (0,0)
print(f"data2[0,0]={data2[0,0]}")
#1st row
print(f"1st row of data2[0,]={data2[0,]}")
data2= [[11 22] [33 44] [55 66]] data2[0,0]=11 1st row of data2[0,]=[11 22]
# positive indexing
# 0 1 2 3 4
# positive slicing
# 0 1 2 3 4 5
# [11,22,33,44,55]
# negative indexing
# -5 -4 -3 -2 -1
# negative slicing
# -5 -4 -3 -2 -1
#
# slicing (from left to right): array[index1:index2], array[index1:], array[:index1]
print(f"data1={data1}")
print("")
#access all data in an array
print(f"data1[:]={data1[:]}")
print("")
#positive slicing of a subset of a one-dimensional array
print(f"data1[:1]={data1[:1]}")
print(f"data1[0:1]={data1[0:1]}")
print(f"data1[0:2]={data1[0:2]}")
print(f"data1[2:5]={data1[2:5]}")
print(f"data1[2:]={data1[2:]}")
print("")
#negative slicing of a one-dimensional array
print(f"data1[-1:]={data1[-1:]}")
print(f"data1[-3:]={data1[-3:]}")
print(f"data1[-5:-3]={data1[-5:-3]}")
data1=[11 22 33 44 55] data1[:]=[11 22 33 44 55] data1[:1]=[11] data1[0:1]=[11] data1[0:2]=[11 22] data1[2:5]=[33 44 55] data1[2:]=[33 44 55] data1[-1:]=[55] data1[-3:]=[33 44 55] data1[-5:-3]=[11 22]
#Split input (X) and variables (y)
data = np.array([
[11, 22, 33],
[44, 55, 66],
[77, 88, 99]])
print(f"array=\n{data}")
# separate data
X, y = data[:, :-1], data[:, -1]
print(f"X=\n{X}")
print(f"y=\n{y}")
X, y = (data[:,:2],data[:,2])
print(f"X=\n{X}")
print(f"y=\n{y}")
array= [[11 22 33] [44 55 66] [77 88 99]] X= [[11 22] [44 55] [77 88]] y= [33 66 99] X= [[11 22] [44 55] [77 88]] y= [33 66 99]
# split train and test data
split = 2
train,test = data[:split,:],data[split:,:]
print(f"train data=\n{train}")
print(f"test data=\n{test}")
train data= [[11 22 33] [44 55 66]] test data= [[77 88 99]]
data = np.array([
[ 0, 1, 2, 3, 4],
[10,11,12,13,14],
[20,21,21,22,24],
[30,31,32,33,34]])
print(data)
[[ 0 1 2 3 4] [10 11 12 13 14] [20 21 21 22 24] [30 31 32 33 34]]
# items 11,12,21,22
data[1:3,1:3]
array([[11, 12],
[21, 21]])
#rows 0,2 , all columns
data[[0,2],:]
array([[ 0, 1, 2, 3, 4],
[20, 21, 21, 22, 24]])
#all rows, columns 2-3
data[:,2:4]
array([[ 2, 3],
[12, 13],
[21, 22],
[32, 33]])
#all rows, columns 2,4
data[:,[2,4]]
array([[ 2, 4],
[12, 14],
[21, 24],
[32, 34]])
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(a)
[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
#column 0
a[:,0]
array([1, 5, 9])
#column 1
a[:,1]
array([ 2, 6, 10])
#column 1-2
a[:,1:3]
array([[ 2, 3],
[ 6, 7],
[10, 11]])
#column 0, 2
a[:,[0,2]]
array([[ 1, 3],
[ 5, 7],
[ 9, 11]])
#row 1
a[1,:]
array([5, 6, 7, 8])
#row 1-2
a[1:3,:]
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
# column 3
a[:,3]
array([ 4, 8, 12])
#column 3 where condition
a[:,3]>=8
array([False, True, True])
#rows where condition in column
a[a[:,3]>=8]
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
#rows where condition in column
a[a[:,0] > 3]
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
a[a[:,0] > 3][:,np.array([True, True, False, True])] #select/filter columns
array([[ 5, 6, 8],
[ 9, 10, 12]])
a = np.array(
[[0,4],
[0,5],
[3,5],
[6,8],
[9,1],
[6,1]]
)
mask = a[:, 0] == 6
print(mask)
print((a[mask, :]))
print("")
print(a[:,1] == 6)
print(a[a[:,0] == 6])
print(a[a[:,0] == 6, :])
[False False False True False True] [[6 8] [6 1]] [False False False False False False] [[6 8] [6 1]] [[6 8] [6 1]]
x = np.array([5, 2, 3, 1, 4, 5])
y = np.array(['f','o','o','b','a','r'])
y[(1 < x) & (x < 5)]
array(['o', 'o', 'a'], dtype='<U1')
x = np.arange(10)
condlist = [x<3, x>5]
choicelist = [x, x**2]
np.select(condlist, choicelist)
array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
a = np.arange(10)
np.where(a < 5, a, 10*a)
array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
np.where([[True, False], [True, True]],
[[1, 2], [3, 4]],
[[9, 8], [7, 6]])
array([[1, 8],
[3, 4]])
matrix = np.array([[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.],
[ 11., 12., 13., 14., 15.],
[ 16., 17., 18., 19., 20.]])
rez = matrix[np.where((1 <= matrix[:,0]) & (matrix[:,0] <= 6)
& (2 <= matrix[:,1]) & (matrix[:,1] <= 7))]
print(rez)
mask = ((1 <= matrix[:,0]) & (matrix[:,0] <= 6)
& (2 <= matrix[:,1]) & (matrix[:,1] <= 7))
rez = matrix[mask,:]
print(rez)
[[ 1. 2. 3. 4. 5.] [ 6. 7. 8. 9. 10.]] [[ 1. 2. 3. 4. 5.] [ 6. 7. 8. 9. 10.]]
data = np.array([
[1,10,1],
[3,10,0],
[1.8,2.0,0],
[-1,-1,1],
[-2,10,1],
])
data[:,0]
array([ 1. , 3. , 1.8, -1. , -2. ])
data[:,[0]]
array([[ 1. ],
[ 3. ],
[ 1.8],
[-1. ],
[-2. ]])
a = np.arange(20).reshape((5,4))
# Returns the rows you want
a[[0,1,3], :]
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[12, 13, 14, 15]])
# Selects the columns you want as well
a[[0,1,3], :][:, [0,2]]
array([[ 0, 2],
[ 4, 6],
[12, 14]])
m = np.array([[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11],
[ 4, 8, 12]])
#rows where col 0 = 1 and 4
condition = (m[:,0]==1) + (m[:,0]==4)
print(condition)
[ True False False True]
m[condition]
array([[ 1, 5, 9],
[ 4, 8, 12]])
data = np.array([[11, 22],
[33, 44],
[55, 66]])
print(f"data=\n{data}")
print(f"data dimensions: {data.shape}")
print(f"Rows: {data.shape[0]}")
print(f"Cols: {data.shape[1]}")
data= [[11 22] [33 44] [55 66]] data dimensions: (3, 2) Rows: 3 Cols: 2
# reshape 1D array to 2D
data = np.array([11, 22, 33, 44, 55])
print(data)
print("shape",data.shape)
data = data.reshape((data.shape[0], 1))
print(data)
print("shape",data.shape)
[11 22 33 44 55] shape (5,) [[11] [22] [33] [44] [55]] shape (5, 1)
# reshape 2D array to 3D
data = np.array([[11, 22],
[33, 44],
[55, 66]])
print(data)
print("dim->",data.shape)
# reshape
data = data.reshape((data.shape[0], data.shape[1], 1))
print(data)
print("dim->",data.shape)
[[11 22] [33 44] [55 66]] dim-> (3, 2) [[[11] [22]] [[33] [44]] [[55] [66]]] dim-> (3, 2, 1)