網站首頁 編程語言 正文
簡介
本文主要簡述如何通過sklearn模塊來進行預測和學習,最后再以圖表這種更加直觀的方式展現出來
數據集
學習數據
預測數據
數據處理
數據分離
因為我們打開我們的的學習數據集,最后一項是我們的真實數值,看過小唐上一篇的人都知道,老規矩先進行拆分,前面的特征放一塊,后面的真實值放一塊,同時由于數據沒有列名,我們選擇使用iloc[]來實現分離
def shuju(tr_path,ts_path,sep='\t'): train=pd.read_csv(tr_path,sep=sep) test=pd.read_csv(ts_path,sep=sep) #特征和結果分離 train_features=train.iloc[:,:-1].values train_labels=train.iloc[:,-1].values test_features = test.iloc[:, :-1].values test_labels = test.iloc[:, -1].values return train_features,test_features,train_labels,test_labels
訓練數據
我們在這里直接使用sklearn函數,通過選擇模型,然后直接生成其識別規則
#訓練數據 def train_tree(*data): x_train, x_test, y_train, y_test=data clf=DecisionTreeClassifier() clf.fit(x_train,y_train) print("學習模型預測成績:{:.4f}".format(clf.score(x_train, y_train))) print("實際模型預測成績:{:.4f}".format(clf.score(x_test, y_test))) #返回學習模型 return clf
數據可視化
為了讓我們的觀察更加直觀,我們還可以使用matplotlib來進行觀測
def plot_imafe(test,test_labels,preds): plt.ion() plt.show() for i in range(50): label,pred=test_labels[i],preds[i] title='實際值:{},predict{}'.format(label,pred) img=test[i].reshape(28,28) plt.imshow(img,cmap="binary") plt.title(title) plt.show() print('done')
結果
完整代碼
import pandas as pd from sklearn.tree import DecisionTreeClassifier import matplotlib.pyplot as plt def shuju(tr_path,ts_path,sep='\t'): train=pd.read_csv(tr_path,sep=sep) test=pd.read_csv(ts_path,sep=sep) #特征和結果分離 train_features=train.iloc[:,:-1].values train_labels=train.iloc[:,-1].values test_features = test.iloc[:, :-1].values test_labels = test.iloc[:, -1].values return train_features,test_features,train_labels,test_labels #訓練數據 def train_tree(*data): x_train, x_test, y_train, y_test=data clf=DecisionTreeClassifier() clf.fit(x_train,y_train) print("學習模型預測成績:{:.4f}".format(clf.score(x_train, y_train))) print("實際模型預測成績:{:.4f}".format(clf.score(x_test, y_test))) #返回學習模型 return clf def plot_imafe(test,test_labels,preds): plt.ion() plt.show() for i in range(50): label,pred=test_labels[i],preds[i] title='實際值:{},predict{}'.format(label,pred) img=test[i].reshape(28,28) plt.imshow(img,cmap="binary") plt.title(title) plt.show() print('done') train_features,test_features,train_labels,test_labels=shuju(r"C:\Users\twy\PycharmProjects\1\train_images.csv",r"C:\Users\twy\PycharmProjects\1\test_images.csv") clf=train_tree(train_features,test_features,train_labels,test_labels) preds=clf.predict(test_features) plot_imafe(test_features,test_labels,preds)
原文鏈接:https://blog.csdn.net/weixin_52521533/article/details/123802259
相關推薦
- 2022-12-16 簡單聊聊Go語言里面的閉包_Golang
- 2022-06-16 Rancher+Docker+SpringBoot實現微服務部署、擴容、環境監控_docker
- 2022-03-21 golang?調用c語言動態庫方式實現_Golang
- 2023-02-06 Python實現號碼歸屬地查詢功能_python
- 2022-10-29 MultipartFile工具類的簡單介紹
- 2022-06-20 關于Golang獲取當前項目絕對路徑的問題_Golang
- 2022-04-15 詳解Python?prometheus_client使用方式_python
- 2022-07-26 更新kali源并,安裝dirseach
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支