<jupyter_start><jupyter_text>Data as table<jupyter_code>import seaborn as sns
iris = sns.load_dataset('iris')
iris.head()<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>%matplotlib inline
import seaborn as sns; sns.set()
# sns.pairplot(iris, hue='species', size=1.5);  'size' parameter has been renamed to 'height'
sns.pairplot(iris, hue='species', height=1.5);<jupyter_output><empty_output><jupyter_text>Extracting features matrix and target array<jupyter_code>X_iris = iris.drop('species', axis=1)
X_iris.shape
y_iris = iris['species']
y_iris.shape<jupyter_output><empty_output><jupyter_text>Scikit-Learn's Estimator API  Supervised learning example: Simple linear regression<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
rng = np.random.RandomState(42)
x = 10 * rng.rand(50)
y = 2 * x - 1 + rng.randn(50)
plt.scatter(x, y)<jupyter_output><empty_output><jupyter_text>1. Choose a class of model<jupyter_code>from sklearn.linear_model import LinearRegression<jupyter_output><empty_output><jupyter_text>2. Choose model hyperparameters<jupyter_code>model = LinearRegression(fit_intercept=True)
model
# LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)<jupyter_output><empty_output><jupyter_text>3. Arrange data into a features matrix and target vector<jupyter_code>X = x[:, np.newaxis]
X.shape<jupyter_output><empty_output><jupyter_text>4. Fit the model to our data<jupyter_code>model.fit(X, y)
# LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
model.coef_
model.intercept_<jupyter_output><empty_output><jupyter_text>5. Predict labels for unknown data<jupyter_code>xfit = np.linspace(-1, 11)
Xfit = xfit[:, np.newaxis]
yfit = model.predict(Xfit)
plt.scatter(x, y)
plt.plot(xfit, yfit)<jupyter_output><empty_output><jupyter_text>Supervised learning example: Iris classification<jupyter_code># from sklearn.cross_validation import train_test_split -- No longer works

from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris, random_state=1)
# With data arranged, we can now predict the labels


from sklearn.naive_bayes import GaussianNB  # 1. choose model class
model = GaussianNB()                        # 2. instantiate model
model.fit(Xtrain, ytrain)                   # 3. fit model to data
y_model = model.predict(Xtest)              # 4. predict on new data
# Finally, we can use accuracy_score utility to see the fraction of predicted lables that match their true value

from sklearn.metrics import accuracy_score
accuracy_score(ytest, y_model)<jupyter_output><empty_output><jupyter_text>Unsupervised learning example: Iris classification<jupyter_code>from sklearn.decomposition import PCA  # 1. choose the model class
model = PCA(n_components=2)            # 2. instantiate the model with hyperparameters
model.fit(X_iris)                      # 3. Fit to data. Notice y is not specified!
X_2D = model.transform(X_iris)         # 4. Transform the data to two dimensions
iris['PCA1'] = X_2D[:, 0]
iris['PCA2'] = X_2D[:, 1]
sns.lmplot("PCA1", "PCA2", hue='species', data=iris, fit_reg=False);<jupyter_output>e:\virtual_env\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(<jupyter_text>Unsupervised learning: Iris clustering<jupyter_code>from sklearn import mixture
model = mixture.GaussianMixture(n_components=3, covariance_type='full')

# from sklearn.mixture import GMM                     # 1. Choose the model class
# model = GMM(n_components=3, covariance_type='full') # 2. instantiate the model w/ hyperparameters
model.fit(X_iris)                                     # 3. Fit to data, Notice y is not specified!
y_gmm = model.predict(X_iris)                         # 4. Determine cluster labels
iris['cluster'] = y_gmm
sns.lmplot("PCA1", "PCA2", data=iris, hue='species', col='cluster', fit_reg=False)<jupyter_output>e:\virtual_env\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(<jupyter_text>Exploring Handwritten Digits  Loading and visualizing the digits data<jupyter_code>from sklearn.datasets import load_digits
digits = load_digits()
digits.images.shape
# Visualizing first 100 of these

import matplotlib.pyplot as plt

fig, axes = plt.subplots(10, 10, figsize=(8, 8),
                        subplot_kw={'xticks':[], 'yticks':[]},
                        gridspec_kw=dict(hspace=0.1, wspace=0.1))

for i, ax in enumerate(axes.flat):
    ax.imshow(digits.images[i], cmap='binary', interpolation='nearest')
    ax.text(0.05, 0.05, str(digits.target[i]),
           transform=ax.transAxes, color='green')
X = digits.data
X.shape
y = digits.target
y.shape<jupyter_output><empty_output><jupyter_text>Unsupervised learning: Dimensionality reduction<jupyter_code>from sklearn.manifold import Isomap
iso = Isomap(n_components=2)
iso.fit(digits.data)
data_projected = iso.transform(digits.data)
data_projected.shape
plt.scatter(data_projected[:, 0], data_projected[:, 1], c=digits.target,
            edgecolor='none', alpha=0.5,
            cmap=plt.cm.get_cmap('Spectral', 10))
plt.colorbar(label='digit label', ticks=range(10))
plt.clim(-0.5, 9.5)<jupyter_output><empty_output><jupyter_text>Classification on digits<jupyter_code>Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, random_state=0)
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
model.fit(Xtrain, ytrain)
y_model = model.predict(Xtest)
from sklearn.metrics import accuracy_score
accuracy_score(ytest, y_model)
from sklearn.metrics import confusion_matrix

mat = confusion_matrix(ytest, y_model)

sns.heatmap(mat, square=True, annot=True, cbar=False)
plt.xlabel('predicted value')
plt.ylabel('true value')
fig, axes = plt.subplots(10, 10, figsize=(8, 8),
                        subplot_kw={'xticks':[], 'yticks':[]},
                        gridspec_kw=dict(hspace=0.1, wspace=0.1))

for i, ax in enumerate(axes.flat):
    ax.imshow(digits.images[i], cmap='binary', interpolation='nearest')
    ax.text(0.05, 0.05, str(y_model[i]),
           transform=ax.transAxes,
           color='green' if (ytest[i] == y_model[i]) else 'red')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11433318229874641, 'fraction_numerical': 0.01676483914816493, 'mean_word_length': 5.08080808080808, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '427066', 'license': 'MIT', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2254, 'n_words': 526, 'path': 'Scikit-Learn/Scikit-Learn_Intro.ipynb', 'repo_name': 'JasmineChhotaray/Miscelleneous', 'size': '6621'}