赞
踩
m1=np.matrix('1 2 3;4 5 6')
"""
matrix([[1, 2, 3],
[4, 5, 6]])
"""
m1.A
"""
array([[1, 2, 3],
[4, 5, 6]])
"""
m1.A1 #array([1, 2, 3, 4, 5, 6])
"""
"""
m1.I
"""
matrix([[-0.94444444, 0.44444444],
[-0.11111111, 0.11111111],
[ 0.72222222, -0.22222222]])
"""
m1.T """ matrix([[1, 4], [2, 5], [3, 6]]) """ m1.transpose() """ matrix([[1, 4], [2, 5], [3, 6]]) """ m1.swapaxes(1,0) matrix([[1, 4], [2, 5], [3, 6]])
m1
"""
matrix([[1, 2, 3],
[4, 5, 6]])
"""
m2=np.array([[1,2],[3,4],[5,6]])
"""
matrix([[1, 2],
[3, 4],
[5, 6]])
"""
m1*m2 """ matrix([[22, 28], [49, 64]]) """ m1 @ m2 """ matrix([[22, 28], [49, 64]]) """ m1*3 """ array([[ 3, 6, 9], [12, 15, 18]]) """ np.matmul(m1,m2) """ matrix([[22, 28], [49, 64]]) """
array3 = np.array([[1, 2], [3, 4]])
"""
array([[1, 2],
[3, 4]])
"""
np.linalg.det(array3) #-2.0000000000000004
矩阵对应的行列式的值是0,矩阵是一个奇异矩阵,无法求逆
array5 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
"""
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
"""
np.linalg.det(array5) #0.0
array6 = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
np.linalg.matrix_rank(array6) #1
{
x
+
2
y
+
z
=
8
3
x
+
7
y
+
2
z
=
23
2
x
+
2
y
+
z
=
9
A x = b Ax = b Ax=b
r a n k ( A ) = r a n k ( A b ) rank(A) = rank(Ab) rank(A)=rank(Ab)
A = np.array([[1,2,1],[3,7,2],[2,2,1]])
"""
A
array([[1, 2, 1],
[3, 7, 2],
[2, 2, 1]])
"""
np.linalg.matrix_rank(A)
# 3
b = np.array([8,23,9]).reshape(-1,1)
"""
b
array([[ 8],
[23],
[ 9]])
"""
合成增广矩阵
np.hstack((A,b))
"""
array([[ 1, 2, 1, 8],
[ 3, 7, 2, 23],
[ 2, 2, 1, 9]])
"""
计算增广矩阵的秩
np.linalg.matrix_rank(np.hstack((A,b)))
# 3
求解
np.linalg.solve(A,b)
"""
array([[1.],
[2.],
[3.]])
"""
np.linalg.inv(A) @ b
"""
array([[1.],
[2.],
[3.]])
"""
# 垂直堆叠 x = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) y = np.array([[4, 4, 4], [5, 5, 5], [6, 6, 6]]) np.vstack((x, y)) """ array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5], [6, 6, 6]]) """ # 水平堆叠 np.hstack((x, y)) """ array([[1, 1, 1, 4, 4, 4], [2, 2, 2, 5, 5, 5], [3, 3, 3, 6, 6, 6]]) """
“”"
“”"
from sklearn.datasets import load_boston
dataset = load_boston()
# 通过Python内置函数dir了解对象的属性和方法
dir(dataset) #['DESCR', 'data', 'feature_names', 'filename', 'target']
df = pd.DataFrame(data=dataset.data, columns=dataset.feature_names)
df
df['PRICE'] = dataset.target
df
绘制房间数和房价的对应的散点图
x, y = df['RM'], df['PRICE']
plt.scatter(x, y)
将房间数和对应的房价保存到字典中
my_dict = {key: value for key, value in zip(x, y)}
my_dict
直接对字典排序,排序对象是键,不是值
prices = {
'AAPL': 191.88,
'GOOG': 1186.96,
'IBM': 149.24,
'ORCL': 48.44,
'ACN': 166.89,
'FB': 208.09,
'SYMC': 21.29
}
sorted(prices) #['AAPL', 'ACN', 'FB', 'GOOG', 'IBM', 'ORCL', 'SYMC']
要想对字典的值进行排序,可以使用匿名函数
下面是以值进行排序后的,生成的对应的键的数组
sorted(prices, key=lambda x: prices[x]) #['SYMC', 'ORCL', 'IBM', 'ACN', 'AAPL', 'FB', 'GOOG']
找到跟当前数据最接近的k条数据,用它们的情况来预估当前的情况。
# 输入小区平均房间数量,返回预测的房价
def knn(history_data, rm, top_k=5):
most_near_key = sorted(history_data, key=lambda x: (x - rm) ** 2)[:top_k]
return np.mean([my_dict[x] for x in most_near_key])
knn(my_dict, 6.5, top_k=3) #24.2
knn(my_dict, 7.2, top_k=10) #35.26
为了判定预测效果(拟合效果)是否理想,引入MSE(均方误差, Mean Squared Error)损失函数。
M S E = 1 N ∑ i = 1 N ( y i − y i ^ ) 2 MSE = \frac {1} {N} \sum_{i=1}^{N}(y_i - \hat{y_i})^2 MSE=N1i=1∑N(yi−yi^)2
# 找到一条线对图上的点做最佳拟合 ---> 最佳的标准:误差的平方和最小 ---> 损失函数
def get_loss(y, y_hat):
return np.mean((y - y_hat) ** 2)
y = a x + b y = ax + b y=ax+b
# 用随机的方式产生a和b的值,试图让MSE变得更小
min_loss = np.inf
best_a, best_b = None, None
for i in range(10000):
a, b = np.random.random(2) * 200 - 100
y_hat = a * x + b
curr_loss = get_loss(y, y_hat)
# 找到更好的拟合
if curr_loss < min_loss:
min_loss = curr_loss
best_a, best_b = a, b
print(f'第{i}次', best_a, best_b, min_loss)
得到a,b的值后,就可计算预测值了,a,b的值每次进行实验都可能不同
y_hat1 = 9.067 * x - 34.233
y_hat1
y_hat2 = 9.3163 * x - 35.531
y_hat2
绘制图像查看拟合情况
plt.scatter(x, y)
plt.plot(x, y_hat1, linewidth=4, color='red')
plt.plot(x, y_hat2, linewidth=4, color='green')
plt.show()
#python 梯度下降算法实现
def partial_a(a,b,x,y):
# return 2*np.mean((y-a*x-b)*(-x))
return 2*np.mean((a*x+b-y)*x)
def partial_b(a,b,x,y):
# return 2*np.mean(-y+a*x+b)
return 2*np.mean((a*x+b-y))
# a,b=np.random.random(2)*200-100
a,b=20,-35
# min_loss=get_loss()
delta=0.01
for _ in range(1000):
a=a-partial_a(a,b,x,y)*delta
b=b-partial_b(a,b,x,y)*delta
curr_loss=get_loss(y,a*x+b)
print(a,b,curr_loss) #9.184417171048281 -35.19418978913763 43.603929193890764
# 最小二乘法 A=np.vstack([x,np.ones(len(x))]).T # print(x,x.shape,type(x)) print(A) B=y results=np.linalg.lstsq(A,B.values,rcond=None) a,b=results[0] loss=results[1][0]/506 print(a,b) print(loss) """ [[6.575 1. ] [6.421 1. ] [7.185 1. ] ... [6.976 1. ] [6.794 1. ] [6.03 1. ]] 9.102108981180313 -34.67062077643857 43.600551771169584 """
#正规方程法求系数
X=np.vstack([np.ones(len(x)),x]).T
# print(X)
def normalEqn(X,y):
theta=np.linalg.inv(X.T@X)@X.T@y
return theta
print(normalEqn(X,y)) #[-34.67062078 9.10210898]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。