08-04 細分構建機器學習應用程序的流程-數據收集

更新、更全的《機器學習》的更新網站,更有python、go、數據結構與算法、爬蟲、人工智能教學等着你:http://www.javashuo.com/article/p-vozphyqp-cm.htmlpython

細分構建機器學習應用程序的流程-數據收集

sklearn數據集官方文檔地址:https://scikit-learn.org/stable/modules/classes.html#module-sklearn.datasetsgit

sklearn數據集一覽算法

類型 獲取方式
sklearn生成的隨機數據集 sklearn.datasets.make_…
sklearn自帶數據集 sklearn.datasets.load_…
sklearn在線下載的數據集 sklearn.datasets.fetch_…
sklearn中加載的svmlight格式的數據集 sklearn.datasets.load_svmlight_file(…)
sklearn在mldata.org在線下載的數據集 sklearn.datasets.fetch_mldata(…)

1、1.1 經過sklearn生成隨機數據

經過sklearn改變生成隨機數據方法的參數,既能夠得到用不盡的數據,而且數據的樣本數、特徵數、標記類別數、噪聲數均可以自定義,很是靈活,簡單介紹幾個sklearn常常使用的生成隨機數據的方法。數據結構

方法 用途
make_classification() 用於分類
maek_multilabel_classfication() 用於多標籤分類
make_regression() 用於迴歸
make_blobs() 用於聚類和分類
make_circles() 用於分類
make_moons() 用於分類

1.1 1.1.1 make_classification()

參數 解釋
n_features 特徵個數= n_informative() + n_redundant + n_repeated
n_informative 多信息特徵的個數
n_redundant 冗餘信息,informative特徵的隨機線性組合
n_repeated 重複信息,隨機提取n_informative和n_redundant 特徵
n_classes 分類類別
n_clusters_per_class 某一個類別是由幾個cluster構成的
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from sklearn import datasets
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')
from sklearn import datasets
try:
    X1, y1 = datasets.make_classification(
        n_samples=50, n_classes=3, n_clusters_per_class=2, n_informative=2)
    print(X1.shape)
except Exception as e:
    print('error:{}'.format(e))

# 下面錯誤信息n_classes * n_clusters_per_class must be smaller or equal 2 ** n_informative,
# 當n_clusters_per_class=2時,意味着該生成隨機數的n_classes應該小於2,能夠理解成一分類或二分類
error:n_classes * n_clusters_per_class must be smaller or equal 2 ** n_informative
import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(10, 10))

plt.subplot(221)
plt.title("One informative feature, one cluster per class", fontsize=12)
X1, y1 = datasets.make_classification(n_samples=1000, random_state=1, n_features=2, n_redundant=0, n_informative=1,
                                      n_clusters_per_class=1)
plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)

plt.subplot(222)
plt.title("Two informative features, one cluster per class", fontsize=12)
X1, y1 = datasets.make_classification(n_samples=1000, random_state=1, n_features=2, n_redundant=0, n_informative=2,
                                      n_clusters_per_class=1)
plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)

plt.subplot(223)
plt.title("Two informative features, two clusters per class", fontsize=12)
X1, y1 = datasets.make_classification(
    n_samples=1000, random_state=1, n_features=2, n_redundant=0, n_informative=2)
plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)


plt.subplot(224)
plt.title("Multi-class, two informative features, one cluster",
          fontsize=12)
X1, y1 = datasets.make_classification(n_samples=1000, random_state=1, n_features=2, n_redundant=0, n_informative=2,
                                      n_clusters_per_class=1, n_classes=4)
plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)
plt.show()

png

1.2 1.1.2 make_multilabel_classification()

X1, y1 = datasets.make_multilabel_classification(
    n_samples=1000, n_classes=4, n_features=2, random_state=1)
datasets.make_multilabel_classification()

print('樣本維度:{}'.format(X1.shape))

# 一個樣本可能有多個標記
print(y1[0:5, :])
樣本維度:(1000, 2)
[[1 1 0 0]
 [0 0 0 0]
 [1 1 0 0]
 [0 0 0 1]
 [0 0 0 0]]
plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)
plt.show()

png

1.3 1.1.3 make_regression()

import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import datasets
X1, y1 = datasets.make_regression(n_samples=500, n_features=1, noise=20)
plt.scatter(X1, y1, color='r', s=10, marker='*')
plt.show()

png

1.4 1.1.4 make_blobs

# 生成3個簇的中心點
centers = [[1, 1], [-1, -2], [1, -2]]

X1, y1 = datasets.make_blobs(
    n_samples=1500, centers=centers, n_features=2, random_state=1, shuffle=False, cluster_std=0.5)

print('樣本維度:{}'.format(X1.shape))

# plt.scatter(X1[0:500, 0], X1[0:500, 1], s=10, label='cluster1')
# plt.scatter(X1[500:1000, 0], X1[500:1000, 1], s=10, label='cluster2')
# plt.scatter(X1[1000:1500, 0], X1[1000:1500, 1], s=10, label='cluster3')
plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)
plt.show()
樣本維度:(1500, 2)

png

1.5 1.1.5 make_circles()

X1, y1 = datasets.make_circles(
    n_samples=1000, random_state=1, factor=0.5, noise=0.1)

print('樣本維度:{}'.format(X1.shape))

plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)
plt.title('make_circles()', fontsize=20)
plt.show()
樣本維度:(1000, 2)

png

1.6 1.1.6 make_moons

X1, y1 = datasets.make_moons(n_samples=1000, noise=0.1, random_state=1)

print('樣本維度:{}'.format(X1.shape))

plt.scatter(X1[:, 0], X1[:, 1], marker='*', c=y1)
plt.title('make_moons()', fontsize=20)
plt.show()
樣本維度:(1000, 2)

png

2、1.2 skleran自帶數據集

方法 描述 用途
load_iris() 鳶尾花數據集 用於分類或聚類
load_digits() 手寫數字數據集 用於分類
load_breast_cancer() 乳腺癌數據集 用於分類
load_boston() 波士頓房價數據集 用於迴歸
load_linnerud() 體能訓練數據集 用於迴歸
load_sample_image(name) 圖像數據集
# 鳶尾花數據集
iris = datasets.load_iris()
iris['target_names']
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
# 手寫數字數據集
digits = datasets.load_digits()
digits['target_names']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 乳腺癌數據集
breast = datasets.load_breast_cancer()
breast['target_names']
array(['malignant', 'benign'], dtype='<U9')
# 波士頓房價數據集
boston = datasets.load_boston()
boston['feature_names']
array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD',
       'TAX', 'PTRATIO', 'B', 'LSTAT'], dtype='<U7')
# 體能訓練數據集
linnerud = datasets.load_linnerud()
linnerud['feature_names']
['Chins', 'Situps', 'Jumps']
# 圖像數據集
china = datasets.load_sample_image('china.jpg')
plt.axis('off')
plt.title('中國頤和園圖像', fontproperties=font, fontsize=20)
plt.imshow(china)
plt.show()

png

3、1.3 導入UCI官網數據

UCI官網:http://archive.ics.uci.edu/ml/datasets.htmldom

df = pd.read_csv(
    'http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)

# 取出前100行的第五列即生成標記向量
y = df.iloc[0:100, 4].values
# 若是標記爲'Iris-versicolor'則賦值1,不然賦值-1
y = np.where(y == 'Iris-versicolor', 1, -1)

# 取出前100行的第一列和第三列的特徵即生成特徵向量
X = df.iloc[:, [2, 3]].values

plt.scatter(X[:50, 0], X[:50, 1], color='b', s=50, marker='x', label='山鳶尾')
plt.scatter(X[50:100, 0], X[50:100, 1], color='r',
            s=50, marker='o', label='雜色鳶尾')
plt.scatter(X[100:150, 0], X[100:150, 1], color='g',
            s=50, marker='*', label='維吉尼亞鳶尾')
plt.xlabel('花瓣長度', fontproperties=font, fontsize=15)
plt.ylabel('花瓣寬度', fontproperties=font, fontsize=15)
plt.title('花瓣長度-花瓣寬度', fontproperties=font, fontsize=20)
plt.legend(prop=font)
plt.show()

png

4、1.4 導入天池比賽csv數據

本次以天池比賽中的葡萄酒質量研究的數據爲例,下載地址:https://tianchi.aliyun.com/dataset/dataDetail?dataId=44機器學習

第四部分-天池比賽白酒數據集

上圖能夠看出,葡萄酒質量的數據是存放在.csv文件當中,咱們首先把csv文件下載到本地,而後可使用pandas作處理。ide

# csv能夠當作普通文本文件,pandas可使用read_csv讀取
df = pd.read_csv('winequality-red.csv')
df[:2]
fixed acidity;"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality"
0 7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
1 7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5
# sep參數至關於規定csv文件數據的分隔符
df = pd.read_csv('winequality-red.csv', sep=';')
df[:2]
fixed acidity volatile acidity citric acid residual sugar chlorides free sulfur dioxide total sulfur dioxide density pH sulphates alcohol quality
0 7.4 0.70 0.0 1.9 0.076 11.0 34.0 0.9978 3.51 0.56 9.4 5
1 7.8 0.88 0.0 2.6 0.098 25.0 67.0 0.9968 3.20 0.68 9.8 5
# 獲取特徵值
X = df.iloc[:2, :-1].values
X
array([[ 7.4   ,  0.7   ,  0.    ,  1.9   ,  0.076 , 11.    , 34.    ,
         0.9978,  3.51  ,  0.56  ,  9.4   ],
       [ 7.8   ,  0.88  ,  0.    ,  2.6   ,  0.098 , 25.    , 67.    ,
         0.9968,  3.2   ,  0.68  ,  9.8   ]])
# 獲取標記值
y = df.iloc[:2, -1].values
y
array([5, 5])
相關文章
相關標籤/搜索