ANU COMP6670 Python学习笔记

ANU COMP6670 Python学习笔记


Numpy

首先import相关库

# numpy
import numpy as np

!pip install sympy
import sympy as sp

# matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

Numpy矩阵运算总结

1. 创建矩阵对象

import numpy as np #引入numpy库

#创建一维的narray对象(1行5列)
a = np.array([1,2,3,4,5])

#创建二维的narray对象(2行5列)
a2 = np.array([[1,2,3,4,5],[6,7,8,9,10]])

#创建多维对象以其类推

2. 元素获取

#获取单个元素:
print('\nSingle Element Extraction')
b = A[0, 0]
print(b)

#获取第一列:
print('\nColumn Extraction')
b = A[:, 0]
print(b)

#获取第一行:
print('\nRow Extraction')
b = A[0, :]
print(b)

#获取某几行某几列元素(第2、3行第3、4列):
#冒号左闭右开
print('\nRow Extraction')
b = A[1:3, 2:5]
print(b)

3. 矩阵运算

运算符作用
+矩阵对应元素相加
-矩阵对应元素相减
*矩阵对应元素相乘(注意区别于理论上的矩阵乘法)
/矩阵对应元素相除
%矩阵对应元素相除后取余
**矩阵每个元素都取n次方,如**2:每个元素都取平方
@理论上的矩阵乘法
A = np.array([[2, 3], [0, 1]])
x = np.array([[1], [3]])

#Matrix Multiplication Example
b = A @ x
print('\nMatrix Multiplication')
print(b)
#结果:[[11]
#      [ 3]]

#Matrix Addition Example
b = A + x
print('\nMatrix Addition')
print(b)
#结果:[[3 4]
#       [3 4]]
       
#Elementwise Multiplication Example
b = A * x
print('\nElementwise Matrix Multiplication')
print(b)
#结果:[[2 3]
#       [0 3]]

4. 高级运算

运算函数结果
np.dot(a,b)计算a和b的点积
A.T得到矩阵A的转置矩阵
np.linalg.inv(A)得到矩阵A的逆矩阵
np.linalg.pinv(A)得到矩阵A的伪逆矩阵
np.linalg.solve(A, B)求解线性方程组AX=B
# define A,B
A = np.array([[2, 3, 4], [0, 1, 3], [-1, 0, 2]])
B = np.array([[3], [5],[7]])

# show solution
X = np.linalg.solve(A, B)
print (X)
# 结果: [[-11.]
#        [ 11.]
#        [ -2.]]



A_inv = np.linalg.inv(A)#YOUR CODE HERE
print(A_inv)
# 结果:
# [[-2.  6. -5.]
#  [ 3. -8.  6.]
#  [-1.  3. -2.]]



A = np.array([[1, 3], [2, 7], [5, 1]])
B = np.array([[13], [30], [9]])
A_pseudo_inverse =  np.linalg.pinv(A) 
print(A_pseudo_inverse)
# 结果:
# [[-0.00544323 -0.02799378  0.21228616]
#  [ 0.05287714  0.12908243 -0.0622084 ]]

Matplotlib相关

1.quiver画箭头

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.quiver.html
https://blog.csdn.net/liuchengzimozigreat/article/details/84566650

2.scatter画散点图

https://www.cnblogs.com/shuaishuaidefeizhu/p/11359826.html
https://blog.csdn.net/m0_37393514/article/details/81298503

3.plot

https://www.jianshu.com/p/ed3f31fc6a41

Sklearn

1. sklearn中的make_blobs的用法

sklearn中的make_blobs函数主要是为了生成数据集的,具体如下:

调用make_blobs

from sklearn.datasets import make_blobs

make_blobs的用法

data, label = make_blobs(n_features=2, n_samples=100, centers=3, random_state=3, cluster_std=[0.8, 2, 5])
  • n_features表示每一个样本有多少特征值
  • n_samples表示样本的个数
  • centers是聚类中心点的个数,可以理解为label的种类数
  • random_state是随机种子,可以固定生成的数据
  • cluster_std设置每个类别的方差

例子:

'''创建训练的数据集'''
from sklearn.datasets import make_blobs
data, label = make_blobs(n_features=2, n_samples=100, centers=2, random_state=2019, cluster_std=[0.6,0.7] )

data有2个特征(n_features=2),样本个数是100(n_samples=100)
在这里插入图片描述
在这里插入图片描述

再看看生成的label:
label只有0或者1(centers=2),维度是100
在这里插入图片描述
random_state给定数值后,每次生成的数据集就是固定的,方便后期复现,默认的是每次随机生成

原文链接:https://blog.csdn.net/weixin_44177568/article/details/102213508

Python语法

1.python里面交换两个变量的值:

a,b = b,a 

不需要先建一个temp先接下来,再赋值。直接交换就行。

Assignment1 相关代码

高斯消去法获得REFRREF矩阵


schmidt正交化获得标准正交基


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值