机器学习系列1:单变量线性回归
生活随笔
收集整理的這篇文章主要介紹了
机器学习系列1:单变量线性回归
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 手寫代碼的單變量線性回歸
用簡單代碼說清楚最小二乘法原理,代價函數,梯度下降等基本概念。
import numpy as np import matplotlib.pyplot as plt# Prepare train data train_X = np.linspace(-1, 1, 100) train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.33 + 10 w = 0 b = 0 for i in range(60000):det = 2*(train_Y - train_X*w -b)pasw = np.dot(train_X.T,det)pasb = sum(det)w = w + 0.001*paswb = b + 0.001*pasb print(w,b) plt.plot(train_X,train_Y,"+") plt.plot(train_X,train_X.dot( w ) + b ) plt.show()??
2 基于sklearn的單變量線性回歸
from sklearn import linear_model import matplotlib.pyplot as plt import numpy as np lr = linear_model.LinearRegression() boston = datasets.load_boston() y = boston.target# cross_val_predict returns an array of the same size as `y` where each entry # is a prediction obtained by cross validation: predicted = cross_val_predict(lr, boston.data, y, cv=20) W = np.column_stack( (y, predicted) ) #print( W )fig, ax = plt.subplots() ax.scatter(y, predicted) ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4) ax.set_xlabel('Measured') ax.set_ylabel('Predicted') plt.show()(略去運算結果)?
3 基于tensorflow的單變量線性回歸
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt# Prepare train data train_X = np.linspace(-1, 1, 100) train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.33 + 10# Define the model X = tf.placeholder("float") Y = tf.placeholder("float") w = tf.Variable(0.0, name="weight") b = tf.Variable(0.0, name="bias") loss = tf.square( Y - X*w - b ) train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)# Create session to run with tf.Session() as sess:sess.run(tf.initialize_all_variables())epoch = 1for i in range(100):for (x, y) in zip(train_X, train_Y):_, w_value, b_value = sess.run([train_op, w, b],feed_dict={X: x,Y: y})print("Epoch: {}, w: {}, b: {}".format(epoch, w_value, b_value))epoch += 1#draw plt.plot(train_X,train_Y,"+") plt.plot(train_X,train_X.dot(w_value)+b_value) plt.show()運算結果:
基于pandas的代碼?
# 一、導入所需庫 import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽 plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號# 二、構造矩陣data = pd.read_csv("linear_regression.csv") data.insert(0,'ones',1) n = data.shape[1] x = data.iloc[:,:n-1] y = data.iloc[:,n-1:]x = np.matrix(x) y = np.matrix(y) w = np.array([0,0]).reshape(1,2)# 三、構造目標函數、損失函數、代價函數#其實不需要寫 在代價函數中有體現 我只是拿來復習一下理論知識 def obiect_function(x,w):''':param x: 特征矩陣:param w: 權重矩陣:return: 返回目標函數的值'''return x*w.T#其實不需要寫 在代價函數中有體現 我只是拿來復習一下理論知識 def loss_function(x,y,w):''':param x:特征矩陣一個樣本值:param w: 權重矩陣:param y: 對應x的實際值:return: 返回一個樣本的損失函數值'''return x*w.T-y#代價函數 也是梯度下降要針對的函數 def cost_function(x,y,w):''':param x: 特征矩陣:param w: 權重矩陣:param y: 實際值矩陣:return: 返回代價函數的值'''#一共有m個樣本m = x.shape[0]return np.sum(np.power(x*w.T-y,2))/(2*m) # 四、梯度下降 def gradient_descent(x,y,w,alpha,iters):''':param x: 特征矩陣:param y: 實際值:param w: 權重矩陣:param alpha: 步長:param iters: 迭代次數:return: 返回迭代之后的權重矩陣w和每次迭代之后的代價函數的值組成的數組cost'''temp = np.zeros(w.shape)x_len = x.shape[0]w_len = w.shape[1]cost = np.zeros(iters)for i in range(iters):error = x*w.T-yfor j in range(w_len):temp[0,j] =w[:,j] - sum(np.multiply(error,x[:,j]))*(alpha/x_len)w = tempcost[i] = cost_function(x,y,w)return w,costalpha = 0.01 iters = 1000 w,cost = gradient_descent(x,y,w,alpha,iters)# 五、繪制預測收益和實際收益圖 # plt.figure(figsize=(12,8)) # plt.scatter(data["人口"],data['收益'],label = '實際值') # plt.xlabel("人口") # plt.ylabel("收益") # plt.title("人口收益預測模型圖") # # c = np.linspace(min(data["人口"]),max(data['人口']),100) # f = [w[0,0]+w[0,1]* i for i in c] # # plt.plot(c,f,label = "預測模型",color = "r") # plt.legend() # plt.show()# 六、繪制迭代次數和代價函數關系圖 plt.plot(range(1,iters+1),cost,label = '迭代次數和代價函數關系') plt.xlabel("迭代次數") plt.ylabel('代價函數') plt.title('迭代次數和代價函數關系圖') plt.legend() plt.show()總結
以上是生活随笔為你收集整理的机器学习系列1:单变量线性回归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker知识6:实战!将一个tens
- 下一篇: GraphViz:2 DOT语法和相关应