繼續學習這本書,這一章主要是主要是如何加載scikit-learn庫中的自帶的一些資料集,以及如何建立仿真資料集,其他的幾個部分是加載CSV檔案,Excel檔案,加載json檔案,查詢SQL資料庫,這幾個部分的我也不會用到的,是以後面就暫時不寫了。
#這一章主要是學習如何加載資料的,盡管在Python生态體系中有很多加載資料的方法,但本書會着重使用pandas庫的
#一些方法來加載外部資料,并使用scikit-learn(Python中的一個開源的機器學習庫)來生成仿生資料
#2.1加載樣本資料集
#問題描述:加載已有的樣本資料集
#加載scikit-learn的shujuj
from sklearn import datasets
#加載手寫數字資料集
digits = datasets.load_digits()
#建立特征矩陣
features = digits.data
#建立目标向量
target = digits.target
#檢視第一個樣本資料
features[0] #不會顯示出來
print(features[0]) #顯示出來
#結果
# [ 0. 0. 5. 13. 9. 1. 0. 0. 0. 0. 13. 15. 10. 15. 5. 0. 0. 3.
# 15. 2. 0. 11. 8. 0. 0. 4. 12. 0. 0. 8. 8. 0. 0. 5. 8. 0.
# 0. 9. 8. 0. 0. 4. 11. 0. 1. 12. 7. 0. 0. 2. 14. 5. 10. 12.
# 0. 0. 0. 0. 6. 13. 10. 0. 0. 0.]
#讨論
# 在研究機器學習算法或方法時,大部分人不願意把時間花在加載、轉換、清洗從現實生活中得到的資料集上
# ,幸運的是,scikit-learn預置了一些很容易加載的常見資料集,這些資料集被稱為“玩具”(toy)資料集,因為它們
# 比現實中的資料集要小得多,幹淨得多,scikit-learn中流行的玩具資料集有:
#
# load_boston
# 503個波士頓房價的觀測值,這是一個用于研究回歸算法的優質資料集
#
# load_iris
# 包含150個鸢尾花尺寸的觀測值,這是一個用于研究分類算法的優質資料集
#
# load_digits
# 包含1797個手寫數字圖檔的觀測值,這是一個用于研究圖像分類算法的優質資料集
#################################################################################
#2.2建立仿真資料集
#問題描述:生成一個仿真資料集
#加載庫
from sklearn.datasets import make_regression
# 生成特征矩陣 目标向量以及模型系數
features,target,coefficients = make_regression(n_samples= 100,
n_features= 3,
n_informative=3,
n_targets=1,
noise= 0.0,
coef= True,
random_state=1)
#檢視特征矩陣和目标向量
print('Feature Matrix\n',features[:3])
print('Target Vector\n',target[:3])
#結果
# Feature Matrix
# [[ 1.29322588 -0.61736206 -0.11044703]
# [-2.793085 0.36633201 1.93752881]
# [ 0.80186103 -0.18656977 0.0465673 ]]
# Target Vector
# [-10.37865986 25.5124503 19.67705609]
#如果需要建立一個仿真資料集來做分類,可以使用make_classification
#加載庫
from sklearn.datasets import make_classification
#生成特征矩陣和目标向量
features,target =make_classification(n_samples= 100,
n_features= 3,
n_informative= 3,
n_redundant=0,
n_classes=2,
weights=[.25,.75],
random_state=1)
# 檢視特征矩陣和目标向量
print('Feature Matrix\n',features[:3])
print('Target Vector\n',target[:3])
#結果
# Feature Matrix
# [[ 1.06354768 -1.42632219 1.02163151]
# [ 0.23156977 1.49535261 0.33251578]
# [ 0.15972951 0.83533515 -0.40869554]]
# Target Vector
# [1 0 0]
#最後如果你想要一個适合做聚類處理的資料集,scikit-learn 提供了make_blobs
#加載庫
from sklearn.datasets import make_blobs
#生成特征矩陣和目标向量
features, target = make_blobs(n_samples= 100,
n_features=2,
centers= 3,cluster_std=0.5,
shuffle=True,
random_state=1)
# 檢視特征矩陣和目标向量
print('Feature Matrix\n',features[:3])
print('Target Vector\n',target[:3])
#結果
# Feature Matrix
# [[ -1.22685609 3.25572052]
# [ -9.57463218 -4.38310652]
# [-10.71976941 -4.20558148]]
# Target Vector
# [0 1 1]
#對于make_blobs來說,centers參數決定了要生成多少個聚類,使用matplotlib可視化庫,能将
#make_blobs生成的聚類可視化地顯示出來,如下圖所示。
#加載庫
import matplotlib .pyplot as plt
#檢視散點圖
plt.scatter(features[:,0],features[:,1],c=target)
plt.show()
########################################################################
#2.3加載CSV檔案
#問題描述:加載以逗号為分隔符(Comma-Separated Values,CSV)的檔案
#加載庫
import pandas as pd
#建立URL
# /url= 'https://tinyurl.com/simulated_data' #書裡這個網址打不開,網上找了一個能打開的
url= 'https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/07_Working_with_Data_Sources/birthweight_data/birthweight.dat'
#加載資料集
dataframe =pd.read_csv(url)
#檢視前兩行資料
dataframe.head(2)
print(dataframe.head(2))
#結果:
# LOW\tAGE\tLWT\tRACE\tSMOKE\tPTL\tHT\tUI\tBWT
# 0 1\t28\t113\t1\t1\t1\t0\t1\t709
# 1 1\t29\t130\t0\t0\t0\t0\t1\t1021
####################################################################
#2.4加載excel檔案 這個部分程式沒跑通,暫時記錄一下
#問題描述:加載一個excel資料表
#加載庫
import pandas as pd
#建立一個url
url='https://tinyurl.com/simulated_excel' #這個依然打不開
#加載資料
dataframe = pd.read_excel(url,sheetname= 0,header=1)
#檢視前兩行
dataframe.head(2)
####################################################################
#2.5加載 JSON檔案——暫時用不到就不記錄了
# 百度了一下 javascript中的對象和數組,是以這兩種結構就是對象和數組2種結構,
# 通過這兩種結構可以表示各種複雜的結構
#2.6加載SQL——暫時用不到就不記錄了