第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何繪制 KMeans?

如何繪制 KMeans?

犯罪嫌疑人X 2022-10-06 20:03:10
我正在嘗試將 MiniBatchKMeans 與更大的數(shù)據(jù)集一起使用并繪制 2 個不同的屬性。我收到一個Keyerror: 2我相信我在for循環(huán)中出錯但我不確定在哪里。有人可以幫我看看我的錯誤是什么?我正在運行以下代碼:import numpy as np ##Import necessary packagesimport pandas as pdimport matplotlib.pyplot as pltfrom matplotlib import stylestyle.use("ggplot")from pandas.plotting import scatter_matrixfrom sklearn.preprocessing import *from sklearn.cluster import MiniBatchKMeans url2="http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data" #Reading in Data from a freely and easily available source on the internetAdult = pd.read_csv(url2, header=None, skipinitialspace=True) #Decoding data by removing extra spaces in cplumns with skipinitialspace=True##Assigning reasonable column names to the dataframeAdult.columns = ["age","workclass","fnlwgt","education","educationnum","maritalstatus","occupation",                   "relationship","race","sex","capitalgain","capitalloss","hoursperweek","nativecountry",                 "less50kmoreeq50kn"]print("reviewing dataframe:")print(Adult.head()) #Getting an overview of the dataprint(Adult.shape)print(Adult.dtypes)np.median(Adult['fnlwgt']) #Calculating median for final weight columnTooLarge = Adult.loc[:,'fnlwgt'] > 748495 #Setting a value to replace outliers from final weight column with medianAdult.loc[TooLarge,'fnlwgt']=np.median(Adult['fnlwgt']) #replacing values from final weight Column with the median of the final weight columnAdult.loc[:,'fnlwgt']X = pd.DataFrame()X.loc[:,0] = Adult.loc[:,'age']X.loc[:,1] = Adult.loc[:,'hoursperweek']kmeans = MiniBatchKMeans(n_clusters = 2)kmeans.fit(X)centroids = kmeans.cluster_centers_labels = kmeans.labels_print(centroids)print(labels)colors = ["g.","r."]for i in range(len(X)):    print("coordinate:",X[i], "label:", labels[i])    plt.plot(X.loc[:,0][i],X.loc[:,1][i], colors[labels[i]], markersize = 10)plt.scatter(centroids[:, 0], centroids[:, 1], marker = "x", s=150, linewidths = 5, zorder = 10)plt.show()當(dāng)我運行for循環(huán)時,我只看到散點矩陣中繪制了 2 個數(shù)據(jù)點。我是否需要以與創(chuàng)建的數(shù)據(jù)框不同的方式調(diào)用這些點?
查看完整描述

1 回答

?
繁花不似錦

TA貢獻(xiàn)1851條經(jīng)驗 獲得超4個贊

您可以通過不運行循環(huán)來單獨繪制 32,000 個點中的每一個來避免此問題,這是不好的做法,也是不必要的。您可以簡單地傳遞兩個數(shù)組來plt.scatter()制作這個散點圖,不需要循環(huán)。使用這些行:


colors = ["green","red"]


plt.scatter(X.iloc[:,0], X.iloc[:,1], c=np.array(colors)[labels], 

    s = 10, alpha=.1)


plt.scatter(centroids[:, 0], centroids[:, 1], marker = "x", s=150, 

    linewidths = 5, zorder = 10, c=['green', 'red'])

plt.show()

http://img1.sycdn.imooc.com//633ec42800016f9a05610434.jpg

您最初的錯誤是由于對熊貓索引的不當(dāng)使用造成的。您可以通過這樣做來復(fù)制您的錯誤:


df = pd.DataFrame(list('dasdasas'))

df[1]


查看完整回答
反對 回復(fù) 2022-10-06
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號