# Get the current timestamp
t1 = time.time()
a = []
for x in range(1000000):
a.append(x**2)
t2 = time.time()
print(t2-t1)
t3 = time.time()
b = np.arange(1000000)**2
t4 = time.time()
print(t4-t3)
b = np.array([1,2,3,4])
print(b)
print(type(b))
arr2 = np.arange(0,10,2)
print(arr2)
print(type(arr2))
arr3 = np.random.random(10)
print(arr3)
print(type(arr3))
a1 = np.zeros((2,2))
a2 = np.ones((3,2))
a3 = np.full((2,2),8) # Generate a that all elements are 8 Of 2 That's ok 2 Array of columns
a4 = np.eye(3) # Generate an element on an oblique square that is 1, The rest of the elements are 0 Of 3*3 Matrix
print(a1)
print(a2)
print(a3)
print(a4)
bool,int8,int16,int32,int64,uint8,uint16,uint32,uint64
object_,string_,unicode_
c = np.array([1,2,3,4],dtype=np.float16)
print(c)
print(c.dtype)
import numpy as np
a1 = np.array([1,2,3])
print(a1.dtype) # windows To think in silence int32
a2 = a1.astype(np.int8) #astype The array itself will not be modified , 20. Return the modified data results
print(a2)
shape: View array dimensions
ndim: View array dimensions
size: Check the number of array elements
itemsize: View the size of each element in the array , Unit is byte
a1 = np.array([1,2,3,4])
a2 = np.array([[1,2,3,4],[5,6,7,8]])
a3 = np.array([[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]]])
print(a1.shape)
print(a2.shape)
print(a3.shape)
reshape: Dimension transformation , The array itself will not be modified
resize: Dimension transformation , Will modify the array itself
flatten: Convert dimensions to one dimension , Returns a copy of the original array
ravel: Dimension is converted to one dimension , Returns the original array reference
a4 = a3.reshape((4,4))
print(a4)
print(a4.shape)
a1 = np.random.randint(0,5,size=(3,2,2))
a2 = np.random.randint(0,5,size=(2,2))
print(a1.flatten())
print("========")
print(a1)
print("========")
print(a2.ravel())
print("========")
print(a2)
print("========")
print(a1+a2)
a5 = a3.reshape((16,))
print(a5)
print(a5.shape)
arr1 = np.arange(0,6).reshape(2,3)
arr2 = np.arange(6,12).reshape(2,3)
print(arr1+arr2)
1、 Arrays are usually very complex when they reach three dimensions , It's not convenient to calculate , So we usually put 3 An array of dimensions or more is converted to 2 Dimension group
2、ndarray.ndim: View array dimensions
3、ndarray.shape: View array shapes ,shape It's a tuple , There are several elements in the array that represent several dimensions
4、ndarray.reshape: Modify the shape of the array , The total number of elements before and after modification must be consistent
5、ndarray.size: See the total number of elements in the array
6、ndarray.itemsize: View the memory size of each element of the array
Include :[ The starting position : End position : step ]
a6 = np.arange(10)
print(a6)
print(a6[1])
print(a6[0:4])
print(a6[0:6:2])
print(a6[::-1])
Multidimensional arrays :
Brackets a value : On behalf of the line
Two values in brackets : Comma separated , Everything is good. The front is OK , After the comma is the column , The result is a one-dimensional array
a7 = np.random.randint(0,10,size=(4,6))
print(a7)
print("============")
print(a7[1])
print("============")
print(a7[0:2])
print("============")
print(a7[[0,2,3]])
print("============")
print(a7[0:2,0])
Boolean index :<,>,<=,>=,==,!=,&,|
a8 = [(a7 < 5) & (a7>3)]
print(a8)
print("============")
print(a7[a8])
Boolean index : Through... On the same array True still False To extract
Multiple extraction conditions : & Represents and , | For or on behalf of , Each condition should be enclosed in parentheses
a7[a7 < 3] = 1
a7
np.where( Conditions ,0,1)
result = np.where(a7 > 5 ,0,1)
result
a1 = np.arange(10).reshape(2,5)
print(a1)
a2 = a1 + 10
print(a2)
(1) Same structure operation
Direct additive
a1 = np.arange(10).reshape(2,5)
print(a1)
print("=======")
a2 = a1 + 10
print(a2)
print("=======")
a3 = a1 + a2
print(a3)
print("=======")
(2) Different structural operations
1、 The length of one of the two arrays is 1, Then the broadcast is compatible
2、 The axis length of the dimension from the end of the two arrays is consistent , Then the broadcast is compatible
a1 = np.random.randint(0,5,size=(3,8,2))
a2 = np.random.randint(0,5,size=(8,1))
print(a1)
print("========")
print(a2)
print("========")
print(a1+a2)
a1 = np.random.randint(0,5,size=(3,2,2))
a2 = np.random.randint(0,5,size=(2,2))
print(a1)
print("========")
print(a2)
print("========")
print(a1+a2)
(1) Stack vertically
vstack: The array is stacked vertically ,
Prerequisite : The number of array columns must be the same
a1 = np.random.randint(0,5,size=(2,2))
a2 = np.random.randint(0,5,size=(2,2))
a3 = np.vstack([a1,a2])
print(a3)
(2) Stack horizontally
hstack: Stack the array horizontally
Prerequisite : The rows of the array must be the same to overlay
a1 = np.random.randint(0,5,size=(2,2))
a2 = np.random.randint(0,5,size=(2,2))
a3 = np.hstack([a1,a2])
print(a3)
(3) Specify manually
concatenate: Stack the two arrays , The specific direction depends on the parameters axis
a1 = np.random.randint(0,5,size=(2,2))
a2 = np.random.randint(0,5,size=(2,2))
a3 = np.concatenate([a1,a2],axis=None)
a4 = np.concatenate([a1,a2],axis=1)
a5 = np.concatenate([a1,a2],axis=0)
print(a3)
print(a4)
print(a5)
(1) Cut horizontally
hsplit: Cut horizontally
a1 = np.random.randint(0,5,size=(2,2))
np.hsplit(a1,2)
(2) Vertical cutting
vsplit: Split vertically
a1 = np.random.randint(0,5,size=(2,2))
np.vsplit(a1,2)
(3) Specify the direction
split: Cut in the specified direction , Parameters axis0 For the line ,1 Column
a1 = np.random.randint(0,5,size=(2,2))
a2 = np.split(a1,2,axis=0)
print(a2)
a3 = np.split(a1,2,axis=1)
print(a3)
Transposition : Matrix inner product calculation
a1 = np.random.randint(0,5,size=(2,2))
print(a1)
print("=====")
print(a1.T)
print("=====")
print(a1.dot(a1.T))
No copy : Simple assignments do not copy
Shallow copy : Variable copy , Points to the same memory space
Deep copy :
a = np.random.randint(0,5,size=(2,2))
b = a
print(b is a)
print("======")
c = a.view()
print(c is a)
c[[0],[0]] = 100
print(c)
print(a)
print("======")
d = a.copy()
d[[1],[1]] = 10
print(d)
print(a)
numpy File format :npy perhaps npz ending
np.savetxt: Save file format , Top mark , Separator
a = np.random.randint(0,100,size=(4,4))
np.savetxt("a.csv",a,delimiter=",",header="a,b,c,d")
np.loadtxt: data type , Separator , Jump
data = np.loadtxt("b.csv",delimiter=",")
data
np.load: Read non text file format , It can be used for three-dimensional arrays
np.load("a.npy")
np.save: Save non text file format , It can be used for three-dimensional arrays
a = np.random.randint(0,100,size=(4,4,4))
np.save("a.npy",a)
with open( File name , read , Encoding mode ) as Object name :
reader = csv.reader(fp)
reader = csv.DictReader(fp)
CSV File features
:1、 Pure text , Use a character set , such as ASCII、Unicode
:2、 It's made up of records
:3、 Each record is divided into fields by a delimiter ( Typical delimiters are commas , A semicolon , tabs )
:4、 Each field has the same field sequence
import csv
with open('b.csv','r') as fp:
#reader It's an iterator
reader = csv.reader(fp)
#next Take out the data pointed to by the first pointer , Then move down
titles = next(reader)
print(titles)
print("=========")
for x in reader:
print(x)
print("========")
a = x[0]
b = x[1]
print(a+b+" result ")
with open("b.csv",'r') as fp:
reader = csv.DictReader(fp)
for x in reader:
print(x)
with open( File name , write in , Coding format ):
writer = csv.writer(fp)
writer.writerow # Write to a row
writer.writerows # Write multiple rows
headers = {
"username","age","height"}
values = [(' Zhang San ',18,200),(' Li Si ',19,180)]
with open('c.csv','w',encoding='utf-8') as fp:
writer = csv.writer(fp)
writer.writerow(headers)
writer.writerows(values)
NAN:Not A Number, It's not a number , It belongs to floating point type
INF:Infinity, For infinity , Divisor is 0 When , Also with floating point types
:1、NAN and NAN It's not equal , such as np.NAN 1= np.NAN This condition is true
:2、NAN And any value , The result is NAN
(1) Delete missing value
data[~np.isnan(data)] : Delete null , Convert an array to a one-dimensional array
np.delete(data,np.where(np.isnan(data))[0],axis=0) : Delete as NAN The line of
import numpy as np
# 1、 Delete all NAN Value , Because the array doesn't know how to change after deleting the value , So it becomes a one-dimensional array
data = np.random.randint(0,10,size = (3,5)).astype(np.float)
data[0:1] = np.nan
data = data[~np.isnan(data)]
print(data)
import numpy as np
# 1、 Delete all NAN Value , Because the array doesn't know how to change after deleting the value , So it becomes a one-dimensional array
data = np.random.randint(0,10,size = (3,5)).astype(np.float)
# take 0,1 and 1,2 The two values are set to NAN
data[[0,1],[1,2]] = np.nan
# Get which rows have NAN
lines = np.where(np.isnan(data))[0]
# Use delete Method to delete the specified row ,axis = 0 Said to delete ,lines A symbol indicating deletion
data1 = np.delete(data,lines,axis=0)
print(data1)
(2) Replace with other values
1、 Replace the null value with 0
2、 Replace null with mean
#CSV The file vacancy value is an empty string , Limit string format when reading
scores = np.loadtxt(" academic record .csv",delimiter = ',',encoding='utf-8',skiprows=1,dtype=np.str)
# Converts an empty string to NAN
scores[scores == ""] = np.NAN
scores.astype(np.float)
# Convert all strings to floating point type
scores.astype(np.float)
Query results
matters needing attention
Read CSV Null value in text :
:1、 First read as a string ,
:2、 The empty string is converted to NAN
:3、 Convert string to floating-point type
(1) Random number seed seed
identical seed value : Generate the same random number every time
Not set up seed value : Select this value according to the system time
np.random.seed(1)
print(np.random.rand()) # Print fixed values
print(np.random.rand()) # Print other values , Because the seed of random number only affects the next generation of random number
(2) Arrays of random numbers
print(np.random.rand(2,3,4)) # Generate two 3 That's ok 4 Array of columns , Values in 0-1 Between
(3) Arrays of random numbers - Standard normal distribution
print(np.random.randn(2,3)) # Generate a 2 That's ok 3 Array of columns , The values in the array are normally distributed
(4) Arrays of random numbers - Specified scope
print(np.random.randint(10,size=(3,6)))
(5) Random sampling
Prerequisite : Must be a one-dimensional array
data = [1,2,3,4,5,6,7,8,9,10]
result1 = np.random.choice(data,3)
result2 = np.random.choice(data,size=(2,2))
print(result1)
print("========")
print(result2)
data = np.random.randint(10,size=(3,4))
print(np.any(data == 5))
print(np.all(data == 5))
data = np.random.randint(10,size=(3,4))
data1 = np.sort(data)
data2 = np.sort(data,axis=0)
data3 = np.argsort(data)
print(data1)
print("========")
print(data2)
print("========")
print(data3)
print("========")