#coding = utf-8
import numpy as np
import random
t1 = np.array([1,2,3])
print(t1)
print(type(t1)
t2 = np.arange(10)#equals to np.array(range(10))
print(t2)
print(t2)
print(t2.dtype)#data type int32 (32bits) == 4 Bytes
#dtype is data type
t3 = np.array(range(1,4),dtype = float)
print(t3)
print(t3.dtype)
t4 = np.array([1,1,0,1,0,0],dtype = bool)
print(t4)#True or False
print(t4.dtype)#type bool
t5 = t4.astype("int8)
print(t5)#1 or 0
print(t5.dtype)#type int8
#generate the random nums
np.random.seed(10)#as long as you don't change the num it is fake random
#numpy.random.randiant(low,high = None, size = None,dtype = int)
t6 = np.array([random.random() for i in range(10)])
print(t6)
print(t6.dtype)
#语法 np.round( , ) contain two nums after the point
#".2f"%random.random() also have this impact
t7 = np.round(t7,2)
print(t7)
[1 2 3]
<class 'numpy.ndarray'>
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
int32
[1. 2. 3.]
float64
[ True True False True False False]
bool
[1 1 0 1 0 0]
int8
[0.34737531 0.2109624 0.40065232 0.5406254 0.23745021 0.79878323
0.14224723 0.24471062 0.25272947 0.39527407]
float64
[0.35 0.21 0.4 0.54 0.24 0.8 0.14 0.24 0.25 0.4 ]
import numpy as np
t1 = np.arange(12)
print(t1)
print(t1.shape)#print the shape of t1
#because t1 just has 1 dimension so the result if (len(t1),)
#Normally the output can reflect the dimensions of t
print(t1.reshape(3,4))#it must be 3*4==12 it's copy not replace
t2 = np.array([[1,2,3],[4,5,6]])#the data inside must be an array
print(t2)
print(t2.shape)#row column
print(t2.flatten())#aftering unfolding
t3 = np.array([[[1,2,3,12],[4,5,6,11],[7,8,9,10]]]) #3 dimensions
print(t3)
print(t3.shape)
#tips: just look how many [ after ( is how many dimensions
#after looking delete [ you get the number of next dimension
t4 = np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[1,2],[9,10]]])
print(t4)
print(t4.shape)
[ 0 1 2 3 4 5 6 7 8 9 10 11]
(12,)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[1 2 3]
[4 5 6]]
(2, 3)
[1 2 3 4 5 6]
[[[ 1 2 3 12]
[ 4 5 6 11]
[ 7 8 9 10]]]
(1, 3, 4)
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 1 2]
[ 9 10]]]
(3, 2, 2)
import numpy as np
t1 = np.arange(24)
t1 = t1.reshape(4,6)
print(t1)
print(t1.transpose()) #== t1.T == t1.swapaxes(1,0) means exchange axis1 and axis0
print(t1+3) #every element add 3 it's copy not replace
t2 = np.arange(6)
print(t2)
t2 = t2.reshape(6,)
#the array with 1 dimension equals to the 2 dimensions array that every row is identical
print(t1+t2)#every corresponsing position add
t3 = np.arange(4).reshape(4,1)#4 rows 1 column
#equals to the the same dimension with the array t3 calculate with
#every column is identical
print(t3)
print(t1+t3)
#Broadcast :low same dimension can transmit
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
[[ 0 6 12 18]
[ 1 7 13 19]
[ 2 8 14 20]
[ 3 9 15 21]
[ 4 10 16 22]
[ 5 11 17 23]]
[[ 3 4 5 6 7 8]
[ 9 10 11 12 13 14]
[15 16 17 18 19 20]
[21 22 23 24 25 26]]
[0 1 2 3 4 5]
[[ 0 2 4 6 8 10]
[ 6 8 10 12 14 16]
[12 14 16 18 20 22]
[18 20 22 24 26 28]]
[[0]
[1]
[2]
[3]]
[[ 0 1 2 3 4 5]
[ 7 8 9 10 11 12]
[14 15 16 17 18 19]
[21 22 23 24 25 26]]
4394029,329953,5931,46245
7860119,185853,26679,0
5845909,576597,39774,170708
import numpy as np
#1 dimension:0axis:row 2 dimension:0axis:row 1axis:column
#3 dimension: 0axis:block 1axis:row 2axis:column
#t1 = np.loadtxt(frame.dtype=np.float,delimite =None,skiprows=0,usecols=None,unpack=False)
t1 = np.loadtxt("code\data_4.csv",delimiter=",",dtype="int",unpack=True)#转置了
print(t1)
# get one row
print(t1[2]) #t1[2,:] get row 3
# get successive row
print(t1[2:]) #t2[2:,:]
# get not continuous rows
print(t1[[1,3]]) #t1[[1,3],:]
print('\n')
# get one column
print(t1)
print(t1[:,1])
# get successive column
print(t1[:,1:])
# get not continuous columns
print(t1[0:3,1:4])# left is closed right is open
# get different points
print(t1[[0,2,2],[0,1,2]])#equals to t1[0,0] t1[2,1] t1[2,2]
# row[0,2,2] column[0,1,2] (1,1),(3,2) (3,3)
[[4394029 7860119 5845909]
[ 329953 185853 576597]
[ 5931 26679 39774]
[ 46245 0 170708]]
[ 5931 26679 39774]
[[ 5931 26679 39774]
[ 46245 0 170708]]
[[329953 185853 576597]
[ 46245 0 170708]]
[[4394029 7860119 5845909]
[ 329953 185853 576597]
[ 5931 26679 39774]
[ 46245 0 170708]]
[7860119 185853 26679 0]
[[7860119 5845909]
[ 185853 576597]
[ 26679 39774]
[ 0 170708]]
[[7860119 5845909]
[ 185853 576597]
[ 26679 39774]]
[4394029 26679 39774]
4394029,329953,5931,46245
7860119,185853,26679,0
5845909,576597,39774,170708
import numpy as np
t1 = np.loadtxt(r"code\data_4.csv",delimiter=",",dtype="int",unpack=True)#转置了
# r 来表示原始字符串,这样 Python 将不会解释反斜杠后面的字符为特殊字符。
print(t1)
print(t1<10000)#bool index if return is True print
t1[t1<100000] = 3 #bool index is place
print(t1)
print(t1[t1>1000])# print the values >1000 to array
#t1[t1>1000] return a array t1>1000 return False or True
#(less than 10) = 10 (more than 18) = 18
print(np.where(t1<=3,100,300))#copy() not replace
#(less than 10) = 10 (more than 18) = 18
print(t1.clip(10,18))
t1 = t1.astype(float)#change into float type
#Only float can change into Nan
print(t1)
t1[1,1] = np.nan
print(t1)
[[4394029 7860119 5845909]
[ 329953 185853 576597]
[ 5931 26679 39774]
[ 46245 0 170708]]
[[False False False]
[False False False]
[ True False False]
[False True False]]
[[4394029 7860119 5845909]
[ 329953 185853 576597]
[ 3 3 3]
[ 3 3 170708]]
[4394029 7860119 5845909 329953 185853 576597 170708]
[[300 300 300]
[300 300 300]
[100 100 100]
[100 100 300]]
[[18 18 18]
[18 18 18]
[10 10 10]
[10 10 18]]
[[4.394029e+06 7.860119e+06 5.845909e+06]
[3.299530e+05 1.858530e+05 5.765970e+05]
[3.000000e+00 3.000000e+00 3.000000e+00]
[3.000000e+00 3.000000e+00 1.707080e+05]]
[[4.394029e+06 7.860119e+06 5.845909e+06]
[3.299530e+05 nan 5.765970e+05]
[3.000000e+00 3.000000e+00 3.000000e+00]
[3.000000e+00 3.000000e+00 1.707080e+05]]
import numpy as np
t1 = np.arange(12).reshape(2,6)
print(t1)
t2 = np.arange(12,24).reshape(2,6)
print(t2)
#np.vstack((t1,t2)) to integrate the two arrays
print(np.vstack((t1,t2)))#vertical direction prolong
print(np.hstack((t1,t2)))#horional direction prolong
t = np.arange(24,36).reshape(3,4)
print(t)
#change row
t[[1,2],:] = t[[2,1],:]#change row 2,3
print(t)
#change column
t[:,[0,2]] = t[:,[2,0]]#change column 1,3
print(t)
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
[[12 13 14 15 16 17]
[18 19 20 21 22 23]]
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
[[ 0 1 2 3 4 5 12 13 14 15 16 17]
[ 6 7 8 9 10 11 18 19 20 21 22 23]]
[[24 25 26 27]
[28 29 30 31]
[32 33 34 35]]
[[24 25 26 27]
[32 33 34 35]
[28 29 30 31]]
[[26 25 24 27]
[34 33 32 35]
[30 29 28 31]]
uk_data.csv
1,2,3
4,5,6
7,8,9
us_data.csv
10,11,12
13,14,15
16,17,18
import numpy as np
#give the national data
us_data = np.loadtxt(r"code\us_data.csv",delimiter = ",",dtype = int)
uk_data = np.loadtxt(r"code\uk_data.csv",delimiter = ",",dtype = int)
# r expresses the initive string in this way Python won't illustrate the
# chars after / into special char
#add national infor
#formulate the data that are all 0
zeros_data = np.zeros((us_data.shape[0],1)).astype(int)
#np.zeros((row,column)).astype(int)
print(zeros_data)
ones_data = np.ones((us_data.shape[0],1)).astype(int)
#If you don't astype you will get float
print(ones_data)#also row
#add a column of 0 and 1 respectively
us_data = np.hstack((us_data,zeros_data))
uk_data = np.hstack((uk_data,ones_data))
#integrate the 2 array
final_data = np.vstack((us_data,uk_data))
print(final_data)
[[0]
[0]
[0]]
[[1]
[1]
[1]]
[[10 11 12 0]
[13 14 15 0]
[16 17 18 0]
[ 1 2 3 1]
[ 4 5 6 1]
[ 7 8 9 1]]
import numpy as np
print(np.ones((2,3)).astype(int))
print(np.zeros((3,4)).astype(int))
#(row,column)
#Diagonal matrix
print(np.eye(10).astype(int))
t1 = np.eye(4)
print(t1)
print(np.argmax(t1,aixs = 0))# 0 refers to row means along row's direction
#find the index of max element and return a array
t1[t1==1] = -1 #bool index
t[2][2] = 6#equals to t[2,2] = 6
t[1][1] = -3
#These are replace not copy
print(t1)
print(np.argmin(t1,axis=1))#1 refers to column means along column's direciton
#find the index of min element and return a array
[[1 1 1]
[1 1 1]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[1 0 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0]
[0 0 0 1 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 0 1]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[0 1 2 3]
[[-1. 0. 0. 0.]
[ 0. -3. 0. 0.]
[ 0. 0. 6. 0.]
[ 0. 0. 0. -1.]]
[0 1 0 3]
import numpy as np
t1 = np.array([[3, 3, 3, 3, 3, 3],
[3, 3, 3, 3, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, np.nan, 20, 20]])
print(t1)
# column 1
t1[:,0] = 0
print(t1)
#count the element that is not 0
print(np.count_nonzero(t1))
#return type bool False or True
# There is only nan != nan
print((t1 != t1)) #(t1 != t1) equals to np.isnan(t1)
print(np.count_nonzero((t1!=t1)))# an array full of True and False then choose the 1
print(np.sum(t1)) #nan calculate with any nums is nan
t3 = np.arange(12).reshape((3,4))
print(t3)
print(np.sum(t3))
print(np.sum(t3,axis=0))#0 refers to row along the row's direction
print(np.sum(t3,axis=1))#1 refers to column along the column's direction
[[ 3. 3. 3. 3. 3. 3.]
[ 3. 3. 3. 3. 10. 11.]
[12. 13. 14. 15. 16. 17.]
[18. 19. 20. nan 20. 20.]]
[[ 0. 3. 3. 3. 3. 3.]
[ 0. 3. 3. 3. 10. 11.]
[ 0. 13. 14. 15. 16. 17.]
[ 0. 19. 20. nan 20. 20.]]
20
[[False False False False False False]
[False False False False False False]
[False False False False False False]
[False False False True False False]]
1
nan
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
66
[12 15 18 21]
[ 6 22 38]