一.數(shù)據(jù)處理
數(shù)據(jù)集包含了美國(guó)400個(gè)城市的經(jīng)度和緯度數(shù)據(jù)贤壁,每個(gè)城市都被貼上紅色或藍(lán)色的標(biāo)簽,這取決于他們?cè)?012年選舉中的投票方式狂男。數(shù)據(jù)來(lái)源https://simplemaps.com/static/demos/resources/us-cities/cities.csv
image.png
導(dǎo)入python常用模塊综看,讀取數(shù)據(jù)
import pandas as pd
from sklearn.dummy import DummyClassifier
df = pd.read_csv(r'C:\Users\PC\Desktop\python\cpsc330\lectures\data/cities_USA.csv', index_col=0) ##自己下載好的文件路徑
df.plot.scatter(x="lon", y="lat", c="vote", alpha=0.3); ##畫散點(diǎn)圖
#獲取前經(jīng)度和緯度數(shù)據(jù)
X = df[["lon", "lat"]] ##X.shape X.ndim 查看大小維度
#獲取標(biāo)簽值
y = df["vote"] ##y.shape y.ndim 查看大小維度
二.預(yù)測(cè)和回歸
分類:根據(jù)精度和緯度這兩個(gè)特征判斷標(biāo)簽是紅還是綠
回顧:定量目標(biāo)的預(yù)測(cè)
三.建模訓(xùn)練預(yù)測(cè)
model = DummyClassifier(strategy="prior") ##建模
model.fit(X, y) ##訓(xùn)練
prediction = model.predict([[0, 0]]) ##預(yù)測(cè)
score = model.score(X, y) ##評(píng)估