Some operations of about Numpy

Summary:

NumPy is an extensive library of the Python language. Advanced support a large number of dimensions of the array and matrix operations, in addition, provide a lot of math library for array operations. Internal Numpy lifted Python's PIL (Global Interpreter Lock), an excellent operation efficiency, is a lot of basic library of machine learning framework!

Create a simple array Numpy

import numpy as np
# Create a simple list
a = [1, 2, 3, 4]
# List into an array
b = np.array(b)

Numpy View array properties

The number of array elements

b.size

Array shape

b.shape

Array dimensions

b.ndim

Array element type

b.dtype

Quickly create N-dimensional array of function api

  • Create 10 rows and 10 columns of floating-point value of 1, Matrix

    array_one = np.ones([10, 10])

  • Create 10 rows and 10 columns of a matrix of values ​​floating point 0

    array_zero = np.zeros([10, 10])

  • Create an array from existing data

    • array (deep copy)

    • asArray (shallow copy)

Numpy create a random arraynp.random

  • Evenly distributed

    • np.random.rand(10, 10)Creating a predetermined shape (exemplified by 10 rows and 10 columns) array (range 0-1)

    • np.random.uniform(0, 100)Create a number within a specified range

    • np.random.randint(0, 100) Create an integer within the specified range

  • Normal distribution

    Given the mean / standard deviation / normal dimensionsnp.random.normal(1.75, 0.1, (2, 3))

  • Array index, sliced

    Generating a normal two-dimensional array of 4 rows and 5 columns

    ​ arr = np.random.normal(1.75, 0.1, (4, 5))

    ​ print(arr)

    1 to 3, taken through line 22 column (from line 0)

    ​ after_arr = arr[1:3, 2:4]

    ​ print(after_arr)

Array index

  • Changing the shape of the array (required number of elements before and after the match)

 

Change the shape of the array

print ( "Use reshape function!") 
one_20 np.ones = ([20 is])
print ( "-> Line 1 20 <-")
Print (one_20) one_4_5 one_20.reshape = ([. 4,. 5 ]) Print ( "-> 5 rows and 4 <-") Print (one_4_5) X 




 

Numpy computing (important)

Conditional Operations

Conditional


import numpy as np
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
stus_score > 80


import numpy as np
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
np.where(stus_score < 80, 0, 90)

Statistics operation

  • Maximum specified axis amax(Parameter 1: Array; Parameter 2: axis = 0/1; 0 represents row represents a column 1)

  • Seeking maximum


stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一列的最大值(0表示列)
print("每一列的最大值为:")
result = np.amax(stus_score, axis=0)
print(result)

print("每一行的最大值为:")
result = np.amax(stus_score, axis=1)
print(result)

 

  • 指定轴最小值amin

  • 求最小值


stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的最小值(0表示列)
print("每一列的最小值为:")
result = np.amin(stus_score, axis=0)
print(result)

# 求每一行的最小值(1表示行)
print("每一行的最小值为:")
result = np.amin(stus_score, axis=1)
print(result)
  • 指定轴平均值mean

求平均值


stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的平均值(0表示列)
print("每一列的平均值:")
result = np.mean(stus_score, axis=0)
print(result)

# 求每一行的平均值(1表示行)
print("每一行的平均值:")
result = np.mean(stus_score, axis=1)
print(result)
  • 方差std

求方差


stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的方差(0表示列)
print("每一列的方差:")
result = np.std(stus_score, axis=0)
print(result)

# 求每一行的方差(1表示行)
print("每一行的方差:")
result = np.std(stus_score, axis=1)
print(result)

数组运算

  • 数组与数的运算

加法


stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
print("加分前:")
print(stus_score)

# 为所有平时成绩都加5分
stus_score[:, 0] = stus_score[:, 0]+5
print("加分后:")
print(stus_score)

乘法


stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
print("减半前:")
print(stus_score)

# 平时成绩减半
stus_score[:, 0] = stus_score[:, 0]*0.5
print("减半后:")
print(stus_score)
  • 数组间也支持加减乘除运算,但基本用不到

image.png


a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
c = a + b
d = a - b
e = a * b
f = a / b
print("a+b为", c)
print("a-b为", d)
print("a*b为", e)
print("a/b为", f)

矩阵运算np.dot()(非常重要)

根据权重计算成绩

  • 计算规则

(M行, N列) * (N行, Z列) = (M行, Z列)

矩阵计算总成绩


stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 平时成绩占40% 期末成绩占60%, 计算结果
q = np.array([[0.4], [0.6]])
result = np.dot(stus_score, q)
print("最终结果为:")
print(result)
  • 矩阵拼接
    • 矩阵垂直拼接

垂直拼接


print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17],
    [18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.vstack((v1, v2))
print("v1和v2垂直拼接的结果为")
print(result)
  • 矩阵水平拼接

  • 水平拼接


print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17],
    [18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.hstack((v1, v2))
print("v1和v2水平拼接的结果为")
print(result)

Numpy读取数据np.genfromtxt

csv文件以逗号分隔数据

​ 读取csv格式的文件

 

Guess you like

Origin www.cnblogs.com/zuichuyouren/p/11487885.html