Python-Numpy

introduce:

The NumPy system is an open source array computing extension to Python. This tool can be used to store and process large matrices much more efficiently than Python's own nested list structure (which can also be used to represent matrices).

Commonly used:

1、genfromtxt()

import numpy

world_alcohol =numpy.genfromtxt("world_alcohol.txt",delimiter=",",dtype=str)

print(type(world_alcohol))

print(world_alcohol)

print(help(numpy.genfromtxt))

 

2. #The numpy.array() array

vector = numpy.array([5,10,15,20])

matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])

print(vector)

print(matrix)

 

3、#ndarray.shape  figuer out how many element are in the array

vector = numpy.array([1,2,3,4])

print(vector.shape)

#for matrices the shape property contains atuple with a element

matrix =numpy.array([[5,10,15],[20,25,30]])

print(matrix.shape)

 

4. Change one of the values, all the values ​​in it will change (the data type in the array should be the same)

import numpy

numbers = numpy.array([1,2,3,4.0])

print(numbers)

numbers.dtype

5、matrix = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])
print(matrix[:1])

Note:  X[:,1] is to take the first data of all rows

X[1,:] takes all the values ​​of the elements with the subscript 1 in the first dimension, and outputs the result:

6、matrix = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])

print(matrix[:,0:2])

7、import numpy
vector = numpy.array([5,10,15,20])

vector == 10

8. Determine whether a number matches the number in the matrix

matrix = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])
matrix == 25

9, and or operation

import numpy
vector = numpy.array([5,10,15,20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)

print (equal_to_ten_and_five)

import numpy
vector = numpy.array([5,10,15,20])
equal_to_ten_and_five = (vector == 10) | (vector == 5)

print (equal_to_ten_and_five)


10、import numpy
matrix = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])
second_column_25 = matrix[:,1] == 25
print(second_column_25)
matrix[second_column_25, 1] = 10

print (matrix)

11、#修改类型
import numpy
vector = numpy.array(["1","2","3"])
print(vector.dtype)
print(vector)
vector = vector.astype(float)
print (vector.dtype)

print(vector)


12. #View the maximum and minimum values
​​import numpy
vector = numpy.array([5,10,15])
#vector.min()

vector.max()

13. #Sum by row
import numpy
matrix = numpy.array([
    [ 5,10,15], [20,25,30]
    ,
    [35,40,45]
])

matrix.sum(axis=1)

14. #Sum by column
import numpy
matrix = numpy.array([
    [ 5,10,15], [20,25,30]
    ,
    [35,40,45]
])

matrix.sum(axis=0)

15. #The overall transformation becomes a matrix with 3 rows and 5 columns
import numpy as np# Specify the alias np instead of numpy
print (np.arange(15))
a = np.arange(15).reshape(3,5)

a

16. # How many rows and columns are

a.shape

#2D matrix

a.ndim

#value type

a.dtype.name

# How many elements are there currently

a.size

#Initialize matrix 3*4

np.zeros((3,4))

#construction value is all 1

np.ones((2,3,4),dtype=np.int32)

# get the sequence result

np.arange(10,30,5)#From 10, every 5 is added, less than 30

#Specify the structure, get the sequence result

np.arange(0.,2,0.3)#Start from 0 and add 0.3 each time, less than 2

#structure

np.arange(12).reshape(4,3)

17. #random function gets matrix 2*3
np.random.random((2,3))#random function gets matrix 2*3
18. from numpy import pi

np.linspace(0,2*pi,100)#Specify an interval to take 100 numbers between 0~2*pi

e.g. sin (e.g. linspace (0.2 * pi, 100))

20. #numpy math operations
a = np.array([20,30,40,50])
b = np.arange(4)
print(a)
print(b)
#b
c = ab
print(c)
c = c -1
print(c)
b**2
print(b**2)

print(a<35)#Determine whether the value is less than 35

#Matrix multiplication
A = np.array([[1,1],
             [0,1]])
B = np.array([[2,0],
            [3,4]])
print(A)
print(' ------')
print(B)
print('------')
print(A*B)# Multiply the corresponding position
print('------')
print(A. dot(B))#Multiplication row*column of matrix
print('------')

print(np.dot(A,B))#A and B perform matrix multiplication

21. #Other operations
import numpy as np
B = np.arange(3)
print(B)
print (np.exp(B))#Calculate how many powers of B

print (np.sqrt(B))#Find the root of the array

#round down floor 3.5---3 , 2.1 ---- 2
a = np.floor(10*np.random.random((3,4)))
print(a)
print('---- --')
#a.shape
##flatten the array
print (a.ravel())#The matrix is ​​flattened into a vector [ 6. 6. 3. 2. 3. 4. 2. 4. 4. 9. 5. 7 .]
print('------')
a.shape = (6,2)#shape 6*2
print(a)
print('------')
print(aT) #transpose

#a.reshape(3,-1)#行数

#Matrix splicing
import numpy as np
a = np.floor(10*np.random.random((2,2)))#rounding, 2*2 structure
b = np.floor(10*np.random.random( (2,2)))
print(a)
print('---')
print(b)
print('---')
print(np.vstack((a,b)))#vstack is spliced ​​vertically, hstack stitching sideways

#np.hstack((a,b))

#Split
a = np.floor(10*np.random.random((2,12)))
print (a)
print('---')
print (np.hsplit(a,3))#According to the line Horizontal split a (to be split), split into 3 parts
print('---')
#hsplit vertical split
print (np.hsplit(a,(3,4)))#Specify the split position 3-4
a = np.floor(10*np.random.random((12,2)))
print ('----')
print (2)

np.vsplit (a, 3)


22. #Data copy 
a = np.arange(12)
b = a
print(b is a)#c is the same as a
b.shape = 3,4
print(a.shape)
print(id(a))#The name is different The area pointed to is the same, changing a to change b will change the value of the area

print(id(b))

#Copy operation
c = a.view()
print (c is a)#c is different from a
c.shape = 2,6
print (a.shape)
c[0,4] = 1234#c and a share a set of values
print(a)
print(id(a))

print (id(c))

#Commonly used copy function
d = a.copy()#Use the value of a to initialize the value of
d d is a
d[0,0] = 9999#d change
print (d)#A new value

print (a)#a does not change

23, #sort, index
import numpy as np
data = np.sin(np.arange(20)).reshape(5,4)#5*4 format
print (data)
ind = data.argmax(axis=0)# Find which column is the largest, index
print (ind)
data_max = data[ind,range(data.shape[1])]#Get the maximum value by finding the index

print (data_max)

#.tile operation
a = np.arange(0,40,10)#0-40,10 plus one
print(a)
b = np.tile(a,(2,2))#Line programming is twice as old, column Also becomes twice the original
print (b)

#sort
a = np.array([[4,3,5],[1,2,1]])
print(a)
print('------')
b = np.sort(a,axis =1)#sort the current array
print(b)
#b
a.sort(axis=1)
print('------')
print (a)
a = np.array([4,3,1 ,2])
j = np.argsort(a)#find the index of the maximum value
print ('------')
print (j)
print ('------')
print (a[j] )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568574&siteId=291194637