This model show a quick way to apply different classification machine learning algorithm on data strored in CSV formate.
Python provide very powerful tools like pandas, numpy, and sklearn. we exploit them a lot.
Source Identification using 2 Class of image¶
In [72]:
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import normalize
import numpy as np
import pandas as pd
# matplotlib.pyplot as plt
Understanding Data¶
In [2]:
sony=pd.read_csv("Sony_NONIQM.csv")
sony.head()
73
In [3]:
nikon=pd.read_csv("Nikons_NONIQM.csv")
nikon.head()
Out[3]:
In [5]:
sony.describe()
Out[5]:
Create trainig and testing data Frame¶
Add Label Coloumn¶
In [6]:
sony.loc[:,'label'] = pd.Series(0, index=sony.index)
In [7]:
sony.head()
Out[7]:
In [8]:
nikon.loc[:,'label'] = pd.Series(1, index=nikon.index)
nikon.head()
Out[8]:
Join Both data set & Normalise¶
In [9]:
data=pd.concat([sony,nikon])
data[140:148]
Out[9]:
In [10]:
#select all coulomn except label
data[1:3].loc[:,data.columns != 'label']
Out[10]:
In [11]:
Norm_feature=normalize(data.loc[:,data.columns != 'label'], norm='l2', axis=1, copy=True)
In [12]:
Norm_feature
Out[12]:
Create train and test¶
In [13]:
#concatenate numpy array
#training Set
train_x=np.concatenate((Norm_feature[15:144], Norm_feature[160:]), axis=0)
train_y=np.concatenate((np.array(data[15:144].loc[:,"label"]),np.array(data[160:].loc[:,"label"])),axis=0)
#Testing Set
test_x=np.concatenate((Norm_feature[0:15], Norm_feature[144:160]), axis=0)
test_y=np.concatenate((np.array(data[0:15].loc[:,"label"]),np.array(data[144:160].loc[:,"label"])),axis=0)
SVM Classifier¶
In [14]:
# clsf = classifier
clsf=SVC(kernel='rbf',gamma=10,C=1) #SVM Classier
svm={} #store accuracy data for plotting
In [15]:
#training
clsf.fit(train_x, train_y)
Out[15]:
Testing accuracy on training data¶
In [16]:
#Now testing for trainig data
#Now predict values for given classifier
prediction = clsf.predict(train_x)
svm["trainig_set"]=accuracy_score(prediction,train_y)*100
print 'Accuracy Check ',svm["trainig_set"],'%'
Testing accuracy on Testing data¶
In [17]:
prediction = clsf.predict(test_x)
svm["test_set"]=accuracy_score(prediction,test_y)*100
print 'Accuracy Check ',svm["test_set"],'%'
In [18]:
svm
Out[18]:
Decision Tree Classifier¶
In [19]:
decision={}
clf=DecisionTreeClassifier(max_depth=10,min_samples_split=4)
clf
Out[19]:
In [20]:
#train
clf = clf.fit(train_x, train_y)
Testing accuracy on training data¶
In [21]:
#Now testing for trainig data
#Now predict values for given classifier
prediction = clf.predict(train_x)
decision["trainig_set"]=accuracy_score(prediction,train_y)*100
print 'Accuracy Check ',decision["trainig_set"],'%'
Testing accuracy on Testing data¶
In [22]:
prediction = clf.predict(test_x)
decision["test_set"]=accuracy_score(prediction,test_y)*100
print 'Accuracy Check ',decision["test_set"],'%'
In [23]:
decision
Out[23]:
K-mean Clustering¶
In [24]:
from sklearn.cluster import KMeans
In [25]:
kmean={}
clust=KMeans(n_clusters=2,n_init=1)
clust
Out[25]:
In [26]:
clust.fit(train_x)
Out[26]:
In [27]:
def accuraccy(pred,expected):
#calculate positive and negative prediction
pos=0
for i in pred:
if i==expected:
pos+=1
return pos
Testing accuracy on training data¶
In [28]:
#defining class then cheking class
expected=clust.predict(train_x[0].reshape(1,12))[0]
pos=accuraccy(clust.predict(train_x[:128]),expected)
expected2=clust.predict(train_x[-1].reshape(1,12))[0]
if expected2==expected:
expected2=clust.predict(train_x[-5].reshape(1,12))[0]
if expected2==expected:
print "Error in class , fix it mannually"
else:
pos+=accuraccy(clust.predict(train_x[128:]),expected2)
kmean["trainig_set"]=(pos/float(len(train_y)))*100
print 'Accuracy Check ',kmean["trainig_set"],'%'
Testing accuracy on testing data¶
In [29]:
#defining class then cheking class
expected=clust.predict(test_x[0].reshape(1,12))[0]
pos=accuraccy(clust.predict(test_x[:15]),expected)
expected2=clust.predict(test_x[-1].reshape(1,12))[0]
if expected2==expected:
expected2=clust.predict(train_x[-5].reshape(1,12))[0]
if expected2==expected:
print "Error in class , fix it mannually"
else:
pos+=accuraccy(clust.predict(train_x[144:160]),expected2)
kmean["test_set"]=(pos/float(len(test_y)))*100
print 'Accuracy Check ',kmean["test_set"],'%'
In [30]:
kmean
Out[30]:
Neural Net Regressor¶
- Uses Backpropagation for trainig
In [38]:
from sklearn.neural_network import MLPClassifier
In [63]:
neural=dict()
clf = MLPClassifier(algorithm='l-bfgs',hidden_layer_sizes=(50,), alpha=1e-5, random_state=1)
clf
Out[63]:
In [64]:
#trainig Network
clf.fit(train_x, train_y)
Out[64]:
Testing accuracy on training data¶
In [65]:
#Now testing for trainig data
#Now predict values for given classifier
prediction = clf.predict(train_x)
neural["trainig_set"]=accuracy_score(prediction,train_y)*100
print 'Accuracy Check ',neural["trainig_set"],'%'
Testing accuracy on testing data¶
In [66]:
prediction = clf.predict(test_x)
neural["test_set"]=accuracy_score(prediction,test_y)*100
print 'Accuracy Check ',neural["test_set"],'%'
In [67]:
neural
Out[67]:
Ploting¶
In [76]:
import pylab as plt
plt.rcParams['figure.figsize'] = 16, 12
In [77]:
#%pylab inline
classifier = [1,2,3,4]
accuracy_test_data=[svm["test_set"],decision["test_set"],kmean["test_set"],neural["test_set"]]
LABELS=["SVM","Decision-Tree","K-Mean","Neural-net"]
plt.bar(classifier,accuracy_test_data,align='center')
plt.xticks(classifier, LABELS) #binding label with x axis data
plt.xlabel('Classifier')
plt.ylabel('Accuracy')
plt.title('Analysis with 2 Class')
plt.show()
Hello,
ReplyDeleteThe Article on Quick Dive to data handeling and machine learning in Python is nice.It give detail information about it .Thanks for Sharing the information about Machine Learning. hire data scientists