<jupyter_start><jupyter_text>Importing our modules<jupyter_code>import numpy as np
import pandas as pd
import sklearn as sk
from sklearn import datasets
from sklearn import svm
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
from sklearn.cross_validation import train_test_split
from sklearn import metrics
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Pandas: Import Data from a CSV File into a Pandas DataFrame<jupyter_code>filename_csv = '../datasets/IRIS.csv'

csv_data = pd.read_csv(filename_csv)
sk_data = datasets.load_iris()
#features = sk_data.data[:, :2]  # we only take the first two features.
#targets = sk_data.target


print("Pandas Dataframe Describe method: \n")
print(csv_data.describe())
print("\nPandas Dataframe Mean method: \n")
print(csv_data.mean())

print("\n\nVariable 'sk_data' has a type of: ")
print(type(sk_data))
print("\n sk_data data: ")

print("\nVariable 'csv_type' has a type of: ")
print(type(csv_data))
print("\nCSV Data:")
print(csv_data)<jupyter_output>Pandas Dataframe Describe method: 

         column1    column2    column3     column4     column5     column6  \
count  99.000000  99.000000  99.000000  100.000000  100.000000  100.000000   
mean    5.926263   0.451278   3.040404    0.433849    3.851000    0.483319   
std     0.851959   0.237209   0.441685    0.182881    1.785378    0.302445   
min     4.300000   0.010000   2.000000    0.010000    1.000000    0.010000   
25%     5.200000   0.250000   2.800000    0.333333    1.600000    0.101695   
50%     5.900000   0.444444   3.000000    0.416667    4.500000    0.593220   
75%     6.500000   0.611111   3.300000    0.541667    5.100000    0.694915   
max     7.900000   0.999900   4.400000    0.999900    6.900000    0.999900   

         column7     column8  
count  100.00000  100.000000  
mean     1.21300    0.464048  
std      0.74558    0.310207  
min      0.10000    0.010000  
25%      0.37500    0.114583  
50%      1.40000    0.541667  
75%      1.80000    0.708333  
max      2.50[...]<jupyter_text>Numpy: Simple Numpy NdArray vs Python List<jupyter_code>python_list = [1, 2, 3]
numpy_array = np.array([1, 2, 3])
pandas_dataframe = pd.DataFrame(data=[1,2,3])

print("Python List:")
print(python_list)
print(type(python_list))

print("\nNumpy Array:")
print(numpy_array)
print(type(numpy_array))

print("\nPandas DataFrame:")
print(pandas_dataframe)
print(type(pandas_dataframe))<jupyter_output>Python List:
[1, 2, 3]
<class 'list'>

Numpy Array:
[1 2 3]
<class 'numpy.ndarray'>

Pandas DataFrame:
   0
0  1
1  2
2  3
<class 'pandas.core.frame.DataFrame'><jupyter_text>Pandas: Subset of columns<jupyter_code>subset_columns = csv_data[['column1','column3','target']]

column1_cleaned = subset_columns[['column1']].fillna( subset_columns[['column1']].mean() )
column3_cleaned = subset_columns[['column3']].fillna( subset_columns[['column3']].mean() )

subset_columns.column1 = column1_cleaned
subset_columns.column3 = column3_cleaned

print(subset_columns)<jupyter_output>column1   column3     target
0   5.100000  3.500000     setosa
1   4.900000  3.000000     setosa
2   5.926263  3.200000     setosa
3   4.600000  3.040404     setosa
4   5.000000  3.600000     setosa
5   5.400000  3.900000     setosa
6   4.600000  3.400000     setosa
7   5.000000  3.400000     setosa
8   4.400000  2.900000     setosa
9   4.900000  3.100000     setosa
10  5.400000  3.700000     setosa
11  4.800000  3.400000     setosa
12  4.800000  3.000000     setosa
13  4.300000  3.000000     setosa
14  5.800000  4.000000     setosa
15  5.700000  4.400000     setosa
16  5.400000  3.900000     setosa
17  5.100000  3.500000     setosa
18  5.700000  3.800000     setosa
19  5.100000  3.800000     setosa
20  5.400000  3.400000     setosa
21  5.100000  3.700000     setosa
22  4.600000  3.600000     setosa
23  5.100000  3.300000     setosa
24  4.800000  3.400000     setosa
25  5.000000  3.000000     setosa
26  5.000000  3.400000     setosa
27  5.200000  3.500000     setosa
28  5.200000  [...]<jupyter_text>Pandas: Subset of rows<jupyter_code>setosa = subset_columns[csv_data.target == 'setosa']
versicolor = subset_columns[csv_data.target == 'versicolor']
virginica = subset_columns[csv_data.target == 'virginica']
print("Subset-rows created.")
setosa_x = setosa['column3'].values
setosa_y = setosa['column1'].values
versicolor_x = versicolor['column3'].values
versicolor_y = versicolor['column1'].values
virginica_x = virginica['column3'].values
virginica_y = virginica['column1'].values

f, axarr = plt.subplots(3, sharex=True, sharey=True)
axarr[0].plot(setosa_x, setosa_y, "bo")
axarr[0].set_title('Setosa')
axarr[1].set_title('Versicolor')
axarr[2].set_title('Virginica')
axarr[1].scatter(versicolor_x, versicolor_y)
axarr[2].scatter(virginica_x, virginica_y)

plt.show()<jupyter_output><empty_output><jupyter_text>Building a SVM (Support Vector Machine) Model/Classifier<jupyter_code>print("Preparing Data...")
# [5.1 , 3.5]
classifier_x = subset_columns[['column1','column3']].values

#[1,0,0] = setosa
#[0,1,0] = versicolor
#[0,0,1] = virginica7
labels = subset_columns['target'].values
le = preprocessing.LabelEncoder()
le.fit(labels)
classifier_y = le.transform(labels)

print("Data Splitting:")
print("Shape before Split: ",classifier_x.shape,"-",classifier_y.shape)
X_train, X_test, y_train, y_test = train_test_split(classifier_x,
                                                     classifier_y)
print("Shape after Split: ",X_train.shape,"-",X_test.shape)

clf = svm.LinearSVC(max_iter=10)
print("Fitting...")
clf.fit(X=X_train,
        y=y_train)
print(clf.coef_)
print("Predicting...")
y_pred = clf.predict(X_test)

print("#"*50)
print(metrics.confusion_matrix(y_test, y_pred))
print(metrics.classification_report(y_test, y_pred))

print(__doc__)


# Code source: <NAME>
# Modified for documentation by <NAME>
# License: BSD 3 clause

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2]  # we only take the first two features.
y = iris.target

x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5

plt.figure(2, figsize=(8, 6))
plt.clf()

# Plot the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Set1,
            edgecolor='k')
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())

# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
X_reduced = PCA(n_components=3).fit_transform(iris.data)
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=y,
           cmap=plt.cm.Set1, edgecolor='k', s=40)
ax.set_title("First three PCA directions")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])

plt.show()<jupyter_output>Automatically created module for IPython interactive environment
{'fraction_non_alphanumeric': 0.1266731061128743, 'fraction_numerical': 0.1404719194149303, 'mean_word_length': 3.806366047745358, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '450376', 'license': 'MIT', 'n_tokens_mistral': 3393, 'n_tokens_neox': 2798, 'n_words': 615, 'path': 'scripts/Basic Machine Learning Introduction.ipynb', 'repo_name': 'AndreyBulezyuk/LiveEduMachineLearning', 'size': '7263'}