In [34]:
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
In [2]:
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)
In [3]:
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))
In [4]:
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)
In [5]:
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.")
In [6]:
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()
In [42]:
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))
In [8]:
print(__doc__)
# Code source: Gaël Varoquaux
# Modified for documentation by Jaques Grobler
# 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()
In [ ]: