Quantcast
Channel: Data Science, Analytics and Big Data discussions - Latest topics
Viewing all articles
Browse latest Browse all 4448

Improving Supervised Algos Using Clustering

$
0
0

@rishii.ds wrote:

Hello Experts,

I have used RandomForestClassifier for solve the classification problem, then to improve the accuracy used K-Means clustering then re-applied the RandomForestClassifier. However, rather than improving, accuracy is slipping away.

Note: The aforementioned technique to improve the result is discussed in following section of Analytics Vidhya.

One can download the dataset from below link (Search Download)
https://trainings.analyticsvidhya.com/courses/course-v1:AnalyticsVidhya+Python-Final-Jan-Feb+Python-Session-1/courseware/73167b5cca8447dfa535a80d3961dc61/821ac77ab0104820bfa494cb55b237d9/?activate_block_id=block-v1%3AAnalyticsVidhya%2BPython-Final-Jan-Feb%2BPython-Session-1%2Btype%40sequential%2Bblock%40821ac77ab0104820bfa494cb55b237d9

In above link the code is written in R, I’ve re-written it in python.
Appreciate if anyone could let me know where I am committing mistake. Below is my code

#Importing Libraries
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix

#Importing Dataset
df =pd.read_csv(“stock_data.csv”)

#Splitting the Dataset into independent & dependent variables
X=df.iloc[:,:-1]
y=df.iloc[:,-1]

“”"
BEFORE CLUSTERING

“”"
#Importing Libraries
from sklearn.cross_validation import train_test_split

#Splitting dataset into training & test dataset
X_train,X_test,y_train,y_test= train_test_split(X,y,test_size=0.2)
#Creating the object of RandomForestClassifier
randomFClassifier=RandomForestClassifier()
#Training the model
randomFClassifier.fit(X_train,y_train)
#Checking accuracy on training data
accuracy_score(y_train,randomFClassifier.predict(X_train))
#Predicting values based on test data
y_predict=randomFClassifier.predict(X_test)
#Checking accuracy on test data
accuracy_score(y_test,y_predict)

“”"
Implementing clustering using KMeans

“”"
#Importing Libraries
from sklearn.cluster import KMeans
#Setting cluster size to 5
kmeans=KMeans(n_clusters=5)
#Fitting the model
kmeans.fit(X)
#Predicting the labels
labels=kmeans.predict(X)
#Adding Labels to independent variable dataset
X[“Labels”]=pd.Series(labels,index=df.index)

#from sklearn.cross_validation import train_test_split
#Splitting dataset into training & test dataset
X_train_1,X_test_1,y_train_1,y_test_1= train_test_split(X,y,test_size=0.2)
#Creating the object of RandomForestClassifier
randomFClassifier_1=RandomForestClassifier()
#Training the model
randomFClassifier_1.fit(X_train_1,y_train_1)
#Checking accuracy on training data
accuracy_score(y_train_1,randomFClassifier_1.predict(X_train_1))
#Predicting values based on test data
y_predict_1=randomFClassifier_1.predict(X_test_1)
#Checking accuracy on test data
accuracy_score(y_test_1,y_predict_1)

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4448

Trending Articles