預測模型筆記

来源:https://www.cnblogs.com/NSGUF/archive/2018/01/12/8278119.html
-Advertisement-
Play Games

預測模型 1、簡介 預測建模(Predictive modeling)是一種用來預測系統未來行為的分析技術,它由一群能夠識別獨立輸入變數與反饋目標關聯關係的演算法構成。根據觀測值創建一個數學模型,然後用這個模型去預測未來發生的事情。 預測模型是用若幹個可能對系統行為產生影響的特征構建的,當處理系統問題 ...


預測模型

1、簡介

預測建模(Predictive modeling)是一種用來預測系統未來行為的分析技術,它由一群能夠識別獨立輸入變數與反饋目標關聯關係的演算法構成。根據觀測值創建一個數學模型,然後用這個模型去預測未來發生的事情。
預測模型是用若幹個可能對系統行為產生影響的特征構建的,當處理系統問題時,需要先判斷哪些因素可能會影響系統的行為,然後在訓練模型之前將這些因素添加進特征中。

2、用SVM建立線性分類器

SVM是用來構建分類器和回歸器的監督學習模型,SVM通過對數學方程組求解,可以找出兩組數據之間的最佳分割邊界。下麵先使用第2章的創建簡單分類器將數據分類並畫出。

# 1、載入數據
import numpy as np
import matplotlib.pyplot as plt
input_file = 'data_multivar.txt'

def load_data(input_file):
    X = []
    y = []
    with open(input_file, 'r') as f:
        for line in f.readlines():
            data = [float(x) for x in line.split(',')]
            X.append(data[:-1])
            y.append(data[-1])
    X=np.array(X)
    y = np.array(y)
    return X,y
X,y=load_data(input_file)

# 2、分類
class_0=np.array([X[i] for i in range(len(X)) if y[i]==0])
class_1=np.array([X[i] for i in range(len(X)) if y[i]==1])
print(class_0)
# 3、畫圖
plt.figure()
plt.scatter(class_0[:,0],class_0[:,1],facecolor='black',edgecolors='black',marker='s')
plt.scatter(class_1[:,0],class_1[:,1],facecolor='none',edgecolors='black',marker='s')
plt.show()

# 定義畫圖函數
def plot_classifier(classifier, X, y):
    # 獲取x,y的最大最小值,並設置餘值
    x_min, x_max = min(X[:, 0]) - 1.0, max(X[:, 0] + 1.0)
    y_min, y_max = min(X[:, 1]) - 1.0, max(X[:, 1] + 1.0)
    # 設置網格步長
    step_size = 0.01
    # 設置網格
    x_values, y_values = np.meshgrid(np.arange(x_min, x_max, step_size), np.arange(y_min, y_max, step_size))
    # 計算出分類器的分類結果
    mesh_output = classifier.predict(np.c_[x_values.ravel(), y_values.ravel()])
    mesh_output = mesh_output.reshape(x_values.shape)
    # 畫圖
    plt.figure()
    # 選擇配色方案
    plt.pcolormesh(x_values, y_values, mesh_output, cmap=plt.cm.gray)
    # 畫點
    plt.scatter(X[:, 0], X[:, 1], c=y, s=80, edgecolors='black', linewidths=1, cmap=plt.cm.Paired)
    # 設置圖片取值範圍
    plt.xlim(x_values.min(), x_values.max())
    plt.ylim(y_values.min(), y_values.max())
    # 設置x與y軸
    plt.xticks((np.arange(int(min(X[:, 0]) - 1), int(max(X[:, 0]) + 1), 1.0)))
    plt.yticks((np.arange(int(min(X[:, 1]) - 1), int(max(X[:, 1]) + 1), 1.0)))
    plt.show()

如圖:

從上面的結果可以看出,空心和實心分別為不同類。下麵使用SVM將不同類分開。結果如下圖:


代碼如下:

# 使用SVM
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=5)
params = {'kernel': 'linear'}
classifier = SVC(**params)
# 訓練線性SVM分類器,並查看結果邊界
classifier.fit(X_train, y_train)
plot_classifier(classifier, X_train, y_train)
# 測試數據集
y_test_pred = classifier.predict(X_test)
plot_classifier(classifier, X_test, y_test)
# 查看數據的精準性,訓練數據集的分類報告
from sklearn.metrics import classification_report
print(classification_report(y_train,classifier.predict(X_train),target_names=['Class-'+str(int(i)) for i in set(y)]))
# 測試數據集的分類報告
print(classification_report(y_test,classifier.predict(X_test),target_names=['Class-'+str(int(i)) for i in set(y)]))

3、用SVM建立非線性分類器

從上面的分類報告可知,我們的分類情況並不理想,而且,最開始的數據可視化也可看出,實心完全被空心包圍著,所以,我們需要嘗試非線性分類器。
SVM為建立非線性分類器提供了許多選項,需要用不同的核函數建立費線性分類器。為了簡單起見,考慮一下兩種情況。

3.1、多項式函數

直接將線性中的params={'kernel':'linear'}替換成params={'kernel':'poly','degree':3},其中degree表示三次多項式,隨著次數增加,可讓曲線變得更彎,但是訓練時間也會越長,計算強度越大。結果如下圖:

3.2、徑向基函數(Radial Basis Function,RBF)

直接將線性中的params={'kernel':'linear'}替換成params={'kernel':'rbf'},結果如下圖:

4、解決類型數量不平衡問題

在現實生活中,我們得到的數據可能會出現某類數據比其他類型數據多很多的情況,在這種情況下分類器會有偏差,邊界線也不會反應數據的真實性,所以需要對數據進行調和。
換一個數據,以上麵線性SVM為例,將數據換成data_multivar_imbalance.txt之後,顯示為

由圖可知,沒有邊界線,這是因為分類器不能區分這兩種類型,所以,將參數改為params={'kernel':'linear','class_weight':'balanced'}即可。結果如下:

5、提取置信度

當一個信的數據點被分類為某一個已知類別時,我們可訓練SVM來計算輸出類型的置信度。

input_datapoints = np.array([[2, 1.5], [8, 9], [4.8, 5.2], [4, 4], [2.5, 7], [7.6, 2], [5.4, 5.9]])

for i in input_datapoints:
    print(i, '-->', classifier.decision_function(i)[0])# 測量點到邊界的距離

params = {'kernel': 'rbf', 'probability': True}
classifier = SVC(**params)
classifier.fit(X_train, y_train)

for i in input_datapoints:
    print(i, '-->', classifier.predict_proba(i)[0])# 這裡要求params中probability必須為True,計算輸入數據點的置信度

plot_classifier(classifier, input_datapoints, [0]*len(input_datapoints))

6、尋找最優超參數

超參數對分類器的性能至關重要。尋找步驟如下:

# 1、載入數據,通過交叉驗證
parameter_grid = [{'kernel': ['linear'], 'C': [1, 10, 50, 600]},
                  {'kernel': ['poly'], 'degree': [2, 3]},
                  {'kernel': ['rbf'], 'gamma': [0.01, 0.001], 'C': [1, 10, 50, 600]},
                  ]

metrics = ['precision', 'recall_weighted']

from sklearn import svm, grid_search, cross_validation
from sklearn.metrics import classification_report
# 2、為每個指標搜索最優超參數
for metric in metrics:
    classifier = grid_search.GridSearchCV(svm.SVC(C=1), parameter_grid, cv=5, scoring=metric)# 獲取對象
    classifier.fit(X_train, y_train)# 訓練
    for params, avg_score, _ in classifier.grid_scores_:# 看指標得分
        print(params, '-->', round(avg_score, 3))
    print('最好參數集:',classifier.best_params_)# 最優參數集
    y_true, y_pred = y_test, classifier.predict(X_test)
    print(classification_report(y_true, y_pred))# 列印一下性能報告

7、建立時間預測器

這個例子和之前的差不多,主要是記住步驟。

# 1、讀取數據
input_file='building_event_multiclass.txt'
# input_file='building_event_binary.txt'

X=[]
y=[]
with open(input_file,'r') as f:
    for line in f.readlines():
        data=line[:-1].split(',')
        X.append([data[0]]+data[2:])
X=np.array(X)
# 2、編碼器編碼
from sklearn import preprocessing
label_encoder=[]
X_encoder=np.empty(X.shape)
for i,item in enumerate(X[0]):
    if item.isdigit():
        X_encoder[:,i]=X[:,i]
    else:
        label_encoder.append(preprocessing.LabelEncoder())
        X_encoder[:,i]=label_encoder[-1].fit_transform(X[:,i])
X=np.array(X_encoder[:,:-1]).astype(int)
y=np.array(X_encoder[:,-1]).astype(int)
# 3、進行分類
from sklearn.svm import SVC
params={'kernel':'rbf','probability':True,'class_weight':'balanced'}
classifier=SVC(**params)
classifier.fit(X,y)
# 4、交叉驗證
from sklearn.model_selection import cross_val_score
accuracy=cross_val_score(classifier,X,y,scoring='accuracy',cv=3)
print('accuracy:',accuracy.mean())
# 5、對新數據進行驗證
input_data = ['Tuesday', '12:30:00','21','23']
input_data_encoder=[-1]*len(input_data)
count=0

for i,item in enumerate(input_data):
    if item.isdigit():
        input_data_encoder[i]=int(input_data[i])
    else:
        label=[]
        label.append(input_data[i])
        input_data_encoder[i]=label_encoder[count].transform(label)
        count=count+1

result=int(classifier.predict(np.array(input_data_encoder)))
print('result:',label_encoder[-1].inverse_transform(result))

8、估算交通流量

在之前的SVM都是用作分類器,現在展示一個回歸器的例子:

# 1、獲取數據
X=[]
input_file='traffic_data.txt'
with open(input_file,'r') as f:
    for line in f.readlines():
        data=line[:-1].split(',')
        X.append(data)

X=np.array(X)

# 2、編碼
from sklearn import preprocessing
label_encoder=[]
X_encoder=np.empty(X.shape)
for i,item in enumerate(X[0]):
    if item.isdigit():
        X_encoder[:,i]=X[:,i]
    else:
        label_encoder.append(preprocessing.LabelEncoder())
        X_encoder[:,i]=label_encoder[-1].fit_transform(X[:,i])

X=X_encoder[:,:-1].astype(int)
y=X_encoder[:,-1].astype(int)

# 3、線性回歸
from sklearn.svm import SVR
# params = {'kernel': 'rbf', 'C': 10.0, 'epsilon': 0.2}
params={'kernel':'rbf','C':10.0,'epsilon':0.2}# C表示對分類的懲罰,參數epsilon表示不使用懲罰的限制
regressor=SVR(**params)
regressor.fit(X,y)
# 4、驗證
from sklearn.metrics import mean_absolute_error
y_pred=regressor.predict(X)
print('mean_absolute_error:',mean_absolute_error(y,y_pred))
# 5、預測新值
input_data = ['Tuesday', '13:35', 'San Francisco', 'yes']
input_data_encoder=[-1]*len(input_data)
count=0
for i,item in enumerate(input_data):
    if item.isdigit():
        input_data_encoder[i]=int(input_data[i])
    else:
        label=[]
        label.append(input_data[i])
        input_data_encoder[i]=int(label_encoder[count].transform(label))
        count=count+1
        
result=regressor.predict(input_data_encoder)
print(result)

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 原因 今天在使用element ui時,el input組件監聽不了回車事件,如下代碼沒有想要的效果: 原因應該是element ui自身封裝了一層input標簽之後影響了事件的監聽,在element ui里有很多因為自身封裝了幾層標簽導致事件和樣式無法按想要的效果呈現,遇到後可以在網頁中查看dom ...
  • 12、浮動 特點:將當前元素脫離文檔流 float: left 即左浮動 float: right 即右浮動 註:*父與子元素,設置子元素浮動不能超出父元素的範圍 *多個元素均設置為浮動時,將自動排列(水平方向) *兄弟元素,後一個元素設置為浮動,前一個元素不浮動,後一個元素的位置不能超過前一個元素 ...
  • function getobjArr (data) { var result = []; data.HELMET.system = '系統分類' // console.log(data) $.each(data.HELMET, function (index_h, e... ...
  • 這兩天學習類、屬性、方法、對象等概念,由於原來基礎比較薄弱,沒接觸過面向對象的語言,一遍看下來,徹底懵逼了。 啃了兩天,才算是不那麼懵了。簡單記錄下。 Class是一種抽象概念,比如我們定義的Class——Student,是指學生這個概念,而實例(Instance)則是一個個具體的Student,比 ...
  • 之前我剛學的python知識點,沒有題目進行熟悉,後面的知識點會有練習題,並且慢慢補充。看到很多都是很簡單的練習,碰到複雜、需要運用的再補充吧#字元串中使用到正則表達式 #字元串裡面使用的是完全匹配 ##引入正則:模糊匹配#引入例子 ##元字元(11個) . 通配符 ^尖角符(頭) $(尾) * + ...
  • //1 1 public class Area_interface 2 { 3 public static void main(String[] args) 4 { 5 Rect r = new Rect(15, 12); 6 Circle c = new Circle(8); 7 System.o ...
  • While迴圈 基本迴圈 ...
  • 前一篇我們探討了關於springboot的配置文件和Controller的使用,本篇我們來一起探討一下關於springboot如何傳遞參數的知識。 參數傳遞我們最常見的就是在url後通過?/&兩個符號來將參數傳遞到後臺,當然springboot也是也一樣,我們可以通過這種方式將參數傳遞到後臺,那麼後 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...