Kmeans演算法應用(加拿大森林火災為例)

2021-09-27 03:36:46 字數 2890 閱讀 4803

ffmc:細小可燃物含水量,(最大值為101,含水率為0,當數值為100時表示可燃物的含水率為0)

dmc:地表可燃物含水率(為0時含水率為100%)

isi:火災蔓延潛在等級

dc:乾旱碼,森林地被物中得含水率

ph:相對濕度

**如下:

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

from sklearn.cluster import kmeans #引入sklearn模組裡的機器學習演算法kmeans

class firedata():

def detectdate(self,filepath):

'''探索資料

:param filepath: 檔案路徑

:return:

'''df = pd.read_csv(filepath)

describe = df.describe(include='all')#同合計資料

print(describe.t)

df.to_excel('data/fire_data.xls')

pass

def choosedata(self,filepath):

df = pd.read_excel('data/fire_data.xls')

df = df[['ffmc','dmc','dc','isi','temp','rh','wind','area']]

df.to_excel('data/fire_coredata.xls')

pass

def standardata(self,filepath):

'''一般標準化得方式:(元資料-平均值)/標準差

:param filepath:

:return:

'''df = pd.read_excel('data/fire_coredata.xls')

df = (df - np.mean(df,axis=0))/np.std(df,axis=0)

df[['ffmc','dmc','dc','isi','temp','rh','wind']].to_excel('data/fire_stdcoredata.xls')

pass

def classifydata(self,filepath,k=7):

df = pd.read_excel(filepath)

kmeans = kmeans(k)

kmeans.fit(df[['ffmc','dmc','dc','isi','temp','rh','wind']])

df['lable'] = kmeans.labels_

coredata = pd.dataframe(kmeans.cluster_centers_)

coredata = np.array(kmeans.cluster_centers_)

#繪製雷達圖

xdata = np.linspace(0,2*np.pi,k,endpoint=false)

xdata = np.concatenate((xdata,[xdata[0]]))

ydata1 = np.concatenate((coredata[0],[coredata[0][0]]))

ydata2 = np.concatenate((coredata[1], [coredata[1][0]]))

ydata3 = np.concatenate((coredata[2], [coredata[2][0]]))

ydata4 = np.concatenate((coredata[3], [coredata[3][0]]))

print(xdata)

print(ydata1)

fig = plt.figure()

ax = fig.add_subplot(111, polar=true)

ax.plot(xdata, ydata1, 'b--', linewidth=1, label='customer1')

ax.plot(xdata, ydata2, 'r--', linewidth=1, label='customer2')

ax.plot(xdata, ydata3, 'g--', linewidth=1, label='customer3')

ax.plot(xdata, ydata4, 'o--', linewidth=1, label='customer4')

ax.set_thetagrids(xdata * 180 / np.pi, ['ffmc','dmc','dc','isi','temp','rh','wind'], )

ax.set_rlim(-3, 3)

plt.legend(loc='best')

plt.show()

print(xdata)

pass

pass

if __name__ == '__main__':

ad = firedata()

ad.detectdate('data/fire_data.csv')

ad.choosedata('data/fire_coredata.xls')

ad.standardata('data/fire_stdcoredata.xls')

ad.classifydata('data/fire_stdcoredata.xls', k=7)

pass

雷達圖:

中的customer 的四條線看作是火災發生的四個等級

根據我們能夠看出customer4相較於其他線較低,也就是說火災發生的可能性比較小

PC加拿大28經典演算法介紹

28原本就是乙個概率遊戲,而概率對於我們人而言可以理解為一種運氣成分的遊戲,但這個運氣其實是可以降低的,當運氣被降低後,遊戲技術和遊戲思維 遊戲方法 的命中率就會隨之而上公升了.當我們投注一次遊戲,你最大的命中率就是100 我們來試試看如何接近這100 注意,單期遊戲,我們永遠無法達到100 命中率...

機器學習 K Means演算法應用

import matplotlib.pyplot as plt from sklearn.datasets.samples generator import make blobs 聚類資料測試工具 在sklearn中,隨機生成1000個樣本,每個樣本2個特徵,共4個簇,簇中心在 1,1 0,0 1,...

Python機器學習應用 Kmeans演算法

import numpy as np from sklearn.cluster import kmeans def loaddata filepath fr open filepath,r 讀寫開啟乙個文字檔案 lines fr.readlines retdata 城市各個消費資料 retcityn...