Python讀取csv文件做K-means分析詳情
1.運行環(huán)境及數(shù)據(jù)
Python3.7、PyCharm Community Edition 2021.1.1,win10系統(tǒng)。
使用的庫:matplotlib、numpy、sklearn、pandas等
數(shù)據(jù):CSV文件,包含時間,經(jīng)緯度,高程等數(shù)據(jù)
2.基于時間序列的分析2D
讀取時間列和高程做一下分析:
代碼如下:
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è)置顏色聚類的類別個數(shù)(我們分別設(shè)置8,16,32,64,128進行對比) ? ? cluster = KMeans(n_clusters=k) ?# 構(gòu)造聚類器 ? ? C = cluster.fit_predict(data) ? ? # C_Image = cluster.fit_predict(data) ? ? print("訓(xùn)練總耗時為:%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,點太多還是不明顯。
3.經(jīng)緯度高程三維坐標(biāo)分類顯示3D-空間點聚類
修改代碼,讀取相應(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è)置顏色聚類的類別個數(shù)(我們分別設(shè)置8,16,32,64,128進行對比) ? ? cluster = KMeans(n_clusters=k) ?# 構(gòu)造聚類器 ? ? C = cluster.fit_predict(data) ? ? ? # C_Image = cluster.fit_predict(data) ? ? print("訓(xùn)練總耗時為:%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ù)測試。
3.2 300M的CSV數(shù)據(jù)計算顯示效果
105萬行數(shù)據(jù)顯示結(jié)果:
到此這篇關(guān)于Python讀取csv文件做K-means分析詳情的文章就介紹到這了,更多相關(guān)Python讀取csv文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3.9 beta2版本發(fā)布了,看看這7個新的PEP都是什么
這篇文章主要介紹了Python3.9 beta2版本發(fā)布了,看看這7個新的PEP都是什么,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2020-06-06Python3.5以上版本lxml導(dǎo)入etree報錯的解決方案
這篇文章主要介紹了Python3.5以上版本lxml導(dǎo)入etree報錯的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-06-06詳解Numpy擴充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方
這篇文章主要介紹了詳解Numpy擴充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Python網(wǎng)絡(luò)爬蟲之獲取網(wǎng)絡(luò)數(shù)據(jù)
本文介紹了Python中用于獲取網(wǎng)絡(luò)數(shù)據(jù)的重要工具之一——Requests庫,詳細(xì)講解了Requests庫的基本使用方法、請求方法、請求頭、請求參數(shù)、Cookies、Session等內(nèi)容,并結(jié)合實例代碼展示了Requests庫的應(yīng)用場景2023-04-04Python中for循環(huán)語句實戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于Python中for循環(huán)語句的相關(guān)資料,python中for循環(huán)一般用來迭代字符串,列表,元組等,當(dāng)for循環(huán)用于迭代時不需要考慮循環(huán)次數(shù),循環(huán)次數(shù)由后面的對象長度來決定,需要的朋友可以參考下2023-09-09