Изменения

Перейти к: навигация, поиск

Обзор библиотек для машинного обучения на Python

2527 байт добавлено, 14:56, 23 января 2019
Нет описания правки
'''weighted avg 0.98 0.97 0.97 38'''
====Байесовская классификация====
Основная статья: [[Байесовская классификация]].
 
====EM-алгоритм====
Основная статья: [[EM-алгоритм]].
 
'''import''' numpy '''as''' np
'''import''' matplotlib.pyplot '''as''' plt
'''from''' matplotlib.colors '''import''' LogNorm
'''from''' sklearn '''import''' mixture
n_samples = <font color="blue">300</font>
<font color="green"># generate random sample, two components</font>
np.random.seed(<font color="blue">0</font>)
<font color="green"># generate spherical data centered on (20, 20)</font>
shifted_gaussian = np.random.randn(n_samples, <font color="blue">2</font>) + np.array([<font color="blue">20</font>, <font color="blue">20</font>])
<font color="green"># generate zero centered stretched Gaussian data</font>
C = np.array([[<font color="blue">0.</font>, <font color="blue">-0.7</font>], [<font color="blue">3.5</font>, <font color="blue">.7</font>]])
stretched_gaussian = np.dot(np.random.randn(n_samples, <font color="blue">2</font>), C)
<font color="green"># concatenate the two datasets into the final training set</font>
X_train = np.vstack([shifted_gaussian, stretched_gaussian])
<font color="green"># fit a Gaussian Mixture Model with two components</font>
clf = mixture.GaussianMixture(n_components=<font color="blue">2</font>, covariance_type=<font color="red">'full'</font>)
clf.fit(X_train)
<font color="green"># display predicted scores by the model as a contour plot</font>
x = np.linspace(<font color="blue">-20.</font>, <font color="blue">30.</font>)
y = np.linspace(<font color="blue">-20.</font>, <font color="blue">40.</font>)
X, Y = np.meshgrid(x, y)
XX = np.array([X.ravel(), Y.ravel()]).T
Z = -clf.score_samples(XX)
Z = Z.reshape(X.shape)
CS = plt.contour(X, Y, Z, norm=LogNorm(vmin=<font color="blue">1.0</font>, vmax=<font color="blue">1000.0</font>),
levels=np.logspace(<font color="blue">0</font>, <font color="blue">3</font>, <font color="blue">10</font>))
CB = plt.colorbar(CS, shrink=<font color="blue">0.8</font>, extend=<font color="red">'both'</font>)
plt.scatter(X_train[:, <font color="blue">0</font>], X_train[:, <font color="blue">1</font>], <font color="blue">.8</font>)
plt.title(<font color="red">'Negative log-likelihood predicted by a GMM'</font>)
plt.axis(<font color="red">'tight'</font>)
plt.show()
 
[[File:EM-sklearn.png|400px|none|super]]
====Уменьшение размерности====
Основная статья: [[Уменьшение размерности#Пример кода scikit-learn | Уменьшение размерности: Пример использования через scikit-learn]].
333
правки

Навигация