Python讀取csv文件做K-means分析詳情
1.運(yùn)行環(huán)境及數(shù)據(jù)
Python3.7、PyCharm Community Edition 2021.1.1,win10系統(tǒng)。
使用的庫(kù):matplotlib、numpy、sklearn、pandas等
數(shù)據(jù):CSV文件,包含時(shí)間,經(jīng)緯度,高程等數(shù)據(jù)
2.基于時(shí)間序列的分析2D
讀取時(shí)間列和高程做一下分析:
代碼如下:
from PIL import Image import matplotlib.pyplot as plt import numpy as np from sklearn.cluster import KMeans, MiniBatchKMeans import pandas as pd ? if __name__ == "__main__": ? ? data = pd.read_csv(r"H:\CSDN_Test_Data\UseYourTestData.csv") ? ? x, y = data['Time (sec)'], data['Height (m HAE)'] ? ? n = len(x) ? ? x = np.array(x) ? ? x = x.reshape(n, 1)#reshape 為一列 ? ? y = np.array(y) ? ? y = y.reshape(n, 1)#reshape 為一列 ? ? data = np.hstack((x, y)) #水平合并為兩列 ? ? k = 8 ?# 設(shè)置顏色聚類的類別個(gè)數(shù)(我們分別設(shè)置8,16,32,64,128進(jìn)行對(duì)比) ? ? cluster = KMeans(n_clusters=k) ?# 構(gòu)造聚類器 ? ? C = cluster.fit_predict(data) ? ? # C_Image = cluster.fit_predict(data) ? ? print("訓(xùn)練總耗時(shí)為:%s(s)" % (Trainingtime).seconds) ? ? plt.figure() ? ? plt.scatter(data[:, 0], data[:, 1], marker='o', s=2, c=C) ? ? plt.show()
結(jié)果展示:
2.1 2000行數(shù)據(jù)結(jié)果展示
2.2 6950行數(shù)據(jù)結(jié)果展示
2.3 300M,約105萬行數(shù)據(jù)結(jié)果展示
CPU立馬90%以上了。大約1-2分鐘,也比較快了。
markersize
有些大了, 將markersize
改小一些顯示,設(shè)置為0.1,點(diǎn)太多還是不明顯。
3.經(jīng)緯度高程三維坐標(biāo)分類顯示3D-空間點(diǎn)聚類
修改代碼,讀取相應(yīng)的列修改為X,Y,Z坐標(biāo):如下:
from PIL import Image import matplotlib.pyplot as plt import numpy as np from sklearn.cluster import KMeans, MiniBatchKMeans import pandas as pd from mpl_toolkits.mplot3d import Axes3D ? if __name__ == "__main__": ? ? data = pd.read_csv(r"H:\CSDN_Test_Data\UseYourTestData.csv") ? ? x, y,z = data['Longitude (deg)'],data['Latitude (deg)'], ?data['Height (m HAE)'] ? ? n = len(x) ? ? x = np.array(x) ? ? x = x.reshape(n, 1)#reshape 為一列 ? ? y = np.array(y) ? ? y = y.reshape(n, 1)#reshape 為一列 ? ? z = np.array(z) ? ? z = z.reshape(n, 1) ?# reshape 為一列 ? ? data = np.hstack((x, y, z)) #水平合并為兩列 ? ? k = 8 ?# 設(shè)置顏色聚類的類別個(gè)數(shù)(我們分別設(shè)置8,16,32,64,128進(jìn)行對(duì)比) ? ? cluster = KMeans(n_clusters=k) ?# 構(gòu)造聚類器 ? ? C = cluster.fit_predict(data) ? ? ? # C_Image = cluster.fit_predict(data) ? ? print("訓(xùn)練總耗時(shí)為:%s(s)" % (Trainingtime).seconds) ? ? fig = plt.figure() ? ? ax = Axes3D(fig) ? ? ? ax.scatter(data[:, 0], data[:, 1],data[:, 2], s=1, c=C) ? ? # 繪制圖例 ? ? ax.legend(loc='best') ? ? # 添加坐標(biāo)軸 ? ? ax.set_zlabel('Z Label', fontdict={'size': 15, 'color': 'red'}) ? ? ax.set_ylabel('Y Label', fontdict={'size': 15, 'color': 'red'}) ? ? ax.set_xlabel('X Label', fontdict={'size': 15, 'color': 'red'}) ? ? plt.show()
3.1 2000行數(shù)據(jù)結(jié)果顯示
由于經(jīng)度在緯度方向上在17m范圍類,所以立體效果較差,可以換其他數(shù)據(jù)測(cè)試。
3.2 300M的CSV數(shù)據(jù)計(jì)算顯示效果
105萬行數(shù)據(jù)顯示結(jié)果:
到此這篇關(guān)于Python讀取csv文件做K-means分析詳情的文章就介紹到這了,更多相關(guān)Python讀取csv文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用os模塊實(shí)現(xiàn)更高效地讀寫文件
os是python標(biāo)準(zhǔn)庫(kù),包含幾百個(gè)函數(shù)常用路徑操作、進(jìn)程管理、環(huán)境參數(shù)等好多類。本文將使用os模塊實(shí)現(xiàn)更高效地讀寫文件,感興趣的可以學(xué)習(xí)一下2022-07-07Python3.9 beta2版本發(fā)布了,看看這7個(gè)新的PEP都是什么
這篇文章主要介紹了Python3.9 beta2版本發(fā)布了,看看這7個(gè)新的PEP都是什么,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2020-06-06利用PyQt中的QThread類實(shí)現(xiàn)多線程
本文主要給大家分享的是python實(shí)現(xiàn)多線程及線程間通信的簡(jiǎn)單方法,非常的實(shí)用,有需要的小伙伴可以參考下2020-02-02Python3.5以上版本lxml導(dǎo)入etree報(bào)錯(cuò)的解決方案
這篇文章主要介紹了Python3.5以上版本lxml導(dǎo)入etree報(bào)錯(cuò)的解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方
這篇文章主要介紹了詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Python網(wǎng)絡(luò)爬蟲之獲取網(wǎng)絡(luò)數(shù)據(jù)
本文介紹了Python中用于獲取網(wǎng)絡(luò)數(shù)據(jù)的重要工具之一——Requests庫(kù),詳細(xì)講解了Requests庫(kù)的基本使用方法、請(qǐng)求方法、請(qǐng)求頭、請(qǐng)求參數(shù)、Cookies、Session等內(nèi)容,并結(jié)合實(shí)例代碼展示了Requests庫(kù)的應(yīng)用場(chǎng)景2023-04-04Python中for循環(huán)語(yǔ)句實(shí)戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于Python中for循環(huán)語(yǔ)句的相關(guān)資料,python中for循環(huán)一般用來迭代字符串,列表,元組等,當(dāng)for循環(huán)用于迭代時(shí)不需要考慮循環(huán)次數(shù),循環(huán)次數(shù)由后面的對(duì)象長(zhǎng)度來決定,需要的朋友可以參考下2023-09-09