Machine Learning——Homework1

今天终于开启了机器学习的实践之路,还是作业香啊,让我终于在纸上摸到了算法~

一.单变量线性回归

首先是数据准备阶段,先来一张准备好数据的截图(第一次用代码搞出图像+表格,很有成就感)
在这里插入图片描述

算法实现

传送门:其中的computeCost 函数可以参看第一回的视频总结,嘿嘿

# 机器学习练习一
# 准备数据阶段
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
path = 'E:\PyCharm\数据\ex1data1.txt'
data = pd.read_csv(path, header=None, names=['Population','Profit'])# 读取文件数据并制表
print(data.head())
print(data.describe())
data.plot(kind='scatter',x='Population',y='Profit',figsize=(12,8))
plt.show()
# 创建一个以θ为特征函数的代价函数 (神奇啊,我竟然看懂了!!)
def computeCost(X,y,theta):
    inner = np.power(((X*theta.T)-y),2)
    return np.sum(inner)/(2*len(X))
# 在训练集中添加一列,以便于用向量方法计算代价和梯度
data.insert(0,'Ones',1)
# 变量初始化
# set X (training data) and y (target variable)
cols = data.shape[1]
X = data.iloc[:,0:cols-1]#x是所有行去掉最后一列
y = data.iloc[:,cols-1:cols]# y是所有行,最后一列
#将x,y转化为矩阵,并初始化θ
X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))
# 计算代价函数的值
print(computeCost(X,y,theta))

二.batch gradient decent (批量梯度下降)

def gradientDescent(X,y,theta,alpha,iters):
    temp = np.matrix(np.zeros(theta.shape))
    parameters = int(theta.ravel().shape[1])
    cost = np.zeros(iters)

    for i in range(iters):
        error = (X * theta.T)-y

        for j in range (parameters):
            term = np.multiply(error,X[:,j])
            temp[0,j] = theta[0,j] -((alpha/len(X))*np.sum(term))
        theta = temp
        cost[i] = computeCost(X,y,theta)
    return theta, cost
alpha = 0.01
iters = 1000
g,cost = gradientDescent(X,y,theta,alpha,iters)
print(computeCost(X,y,g))
# 画图来拟合
x = np.linspace(data.Population.min(),data.Population.max(),100)
f = g[0,0]+(g[0,1]*x)
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(x,f,'r',label='Prediction')
ax.scatter(data.Population,data.Profit, label='Training Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')
plt.show()
# 画图代码需要多加练习,嘿嘿
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(np.arange(iters),cost,'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('hE vs.training')
plt.show()

三.多变量线性回归

path = 'E:\PyCharm\数据\ex1data2.txt'
data2 = pd.read_csv(path,header=None,names=['Size','Bedroom','Price'])
data2 = (data2 - data2.mean())/data2.std()
data2.insert(0,'Ones',1)
cols = data2.shape[1]
X2 = data2.iloc[:,0:cols-1]
y2 = data2.iloc[:,cols-1:cols]

X2 = np.matrix(X2.values)
y2 = np.matrix(y2.values)
theta2 = np.matrix(np.array([0,0,0]))
g2,cost2 = gradientDescent(X2,y2,theta2,alpha,iters)

print(computeCost(X2,y2,g2))

scikit-learn 中的线性回归函数

sklearn 真是个好东西啊,直接封装线性回归模型,省去了从头开始写的烦恼,开心~

# 使用sklearn的线性回归函数
from sklearn import linear_model
model = linear_model.LinearRegression()
model.fit(X,y)
x = np.array(X[:,1].A1)
f = model.predict(X).flatten()
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(x,f,'r',label='Prediction')
ax.scatter(data.Population,data.Profit, label='Training Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')
plt.show()

两种方式对比图

干写
在这里插入图片描述
sklearn
在这里插入图片描述
肉眼完全看不出差别啊,只有两张图快速切换时才会发现斜率稍有不同,但是我也很满意,嘿嘿

四.正规方程

# 正规方程
def normalEqn(X,y):
    theta = np.linalg.inv(X.T@X)@X.T@y #@是数量积的意思~
    return theta
final_theta2=normalEqn(X,y)
print(final_theta2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值