Menu

Showing posts with label Python Code Examples. Show all posts
Showing posts with label Python Code Examples. Show all posts

Tuesday, August 23, 2022

Classification : Iris Dataset : Predicting Class Labels

 


Classification: It is a process of categorizing a given set of data into classes, It can be performed on both structured or unstructured data. The process starts with predicting the class of given data points. The classes are often referred to as target, label or categories.

Random forest classifier: Random forest, like its name implies, consists of a large number of individual decision trees that operate as an ensemble. Each individual tree in the random forest spits out a class prediction and the class with the most votes becomes our model’s prediction.

Iris Dataset: The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. 

Attribute Information:

1. sepal length in cm
2. sepal width in cm
3. petal length in cm
4. petal width in cm
5. class:
-- Iris Setosa
-- Iris Versicolour
-- Iris Virginica

Below is the code to create Random Forest Classifier for classifying custom samples supplied from user. Output is class label (plan type : Setosa/Versicolour/Virginica)


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


data = pd.read_csv('Iris.csv')

data_points = data.iloc[:, 1:5]

labels = data.iloc[:, 5]


#split

from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test = train_test_split(data_points,labels,test_size=0.2)


# Classify using Random forest

from sklearn.ensemble import RandomForestClassifier

random_forest = RandomForestClassifier()

random_forest.fit(x_train, y_train)

print('Training data accuracy {:.2f}'.format(random_forest.score(x_train, y_train)*100))

print('Testing data accuracy {:.2f}'.format(random_forest.score(x_test, y_test)*100))


# predict for User Input

X_new = np.array([[3, 2, 1, 0.2], [  4.9, 2.2, 3.8, 1.1 ], [  5.3, 2.5, 4.6, 1.9 ]])

#classfication of the species from the input vector

classify = random_forest.predict(X_new)

print("classification of Species: {}".format(classify))


The output is predicted class labels.




Wednesday, March 23, 2022

A Simple Voice-Based Chatbot For Hindi Language

 A Simple Voice-Based Chatbot For Hindi Language

A Voice-Based Chatbot acts like a voice assistant to accept your vocal queries, process them and produce the vocal answer to satisfy the query. 

In this article, we will create a simple voice-based chatbot for Hindi language. The chatbot will accept the Hindi queries through vocal commands, will process them with text-matching principle and will revert with appropriate vocal answer in Hindi.

To do this following Python libraries will be helpful.         

  1. speech_recognition: Speech recognition is a machine's ability to listen to spoken words and identify them. You can then convert the spoken words into text, make a query or give a reply. The code uses this library to recognize the input Hindi query. (installation: pip install SpeechRecognition)
  2. translate: Translate is a simple but powerful translation tool written in python with with support for multiple translation providers. The code uses this library to translate the input Hindi query into English for text-matching. (Installation: pip install translate)
  3. gTTS:  A gTTS (Google Text-to-Speech), is a Python library to interface with Google Translate's text-to-speech API. It can process the .mp3 input file. (Installation: pip install gTTS). The code uses this library to translate the response in Hindi. You can reuse the above mentioned library 'translate' instead. However the code uses this specific library to explore to the geeks, the another available option. 
  4. pygame: It is a free and open-source cross-platform library for the development of multimedia applications including audio and video. (Installation: pip install pygame). The code uses this library to save and play the translated response in Hindi.

The Code is explained below.

Step1 : Import the libraries you need.

Python3

# import required libs

import random

import speech_recognition as sr

from translate import Translator

from gtts import gTTS

from datetime import datetime

from pygame import mixer

 

Step 2:  Receive A Vocal Query in Hindi: To do this we create and initialize object of speech_recognition. It helps to recognize the audio query  obtained from the device-microphone as the source. It recognizes the spoken Hindi words using the recognize_google().     

Python3

# the function is coded to take Hindi input queries and recognize them

def Receive_In_Hindi():

    r = sr.Recognizer()

    with sr.Microphone() as source:

        print('Listening')

        r.pause_threshold = 0.7

        audio = r.listen(source)

        try:

            print("Recognizing")

            Query = r.recognize_google(audio, language='hi-In')

            Answer_In_Hindi(Query)

            # handling the exception, so that assistant can

            # ask for telling again the command

        except Exception as e:

            print(e)

            print("Didn't Get That ! Please say again..")

            return "None"

        return Query

 

Step 3:  Translate the vocal query to English and process response : This is required as our code does 'text-matching' for processing the query. The input vocal Hindi query needs to be translated into English first. The Translator() helps in this task. Then a response is generated  in English text format. 

Python3

def Answer_In_Hindi(Question):

        print("Your Query:",Question)

        #translated_text = GoogleTranslator(source='hi', target='en').translate(Question)

        translator= Translator(from_lang="hindi",to_lang="english")

        translation = translator.translate(Question)

   

        if "today" in translation:  # check if the query is about today

            answer_text="today is "+ datetime.today().strftime('%A')

            translator= Translator(from_lang="english",to_lang="hindi")

            translation = translator.translate(answer_text)

            Process_For_Hindi(translation)

       

        if "age" in translation: # check if the query is about age

            answer_text="I am 22 years old"

            translator= Translator(from_lang="english",to_lang="hindi")

            translation = translator.translate(answer_text)

            Process_For_Hindi(translation)

 

Step 4: Process the English-textual response into Hindi Audio:  As we need the final response in Hindi-audio format, we need to convert the processed textual response into Hindi audio. The gTTS() does this task. Since all responses need to be saved, we generate a random text file name and save the audio responses.

Python3

def Process_For_Hindi(translation):

    print("Chatbot Response:"+translation)

    answer_text=str(translation) #translate the English response into Hindi and convert it into audio file

    myobj = gTTS(text=answer_text, lang='hi', slow=False)

    file_name="chat_aswer"+str(random.randint(0,100))+".mp3"

    myobj.save(file_name)

    mixer.init()

    mixer.music.load(file_name)

    mixer.music.play()   #play the audio response

 

Output: 

The Input Query and the Response

 

The input should be provided in Hindi to the microphone of the device. When the query is processed, the code plays the Hindi audio file generated as the response of the query.

 

 

Monday, March 21, 2022

Regression using Random Forest, SVM, and MLP

 

Regression is the process of process of  estimating the relationships between a dependent (or target) variable and one or more independent (or predictor) variables. It finds application in the area of Inference Analysis.  It is a handy technique for forecasting the future trends in data.  

Example: Consider that a HR head wants to fix salary of a new employee. For finalizing the salary the head, considers the various parameters like the level of education, no of years of experience, last position held, expertise level etc.  Now if the salary is predicted using only one parameter say 'no of years of experience' then this type of regression is called as Simple Linear Regression (one target and one predictor variable) . Also, if multiple parameters say 'level of education', 'no of years of experience', 'last position held' are used to fix the salary then it becomes Multivariate Regression (single target, multiple predictor variables).

Irrespective of the model you choose for the task of performing Simple Linear Regression, you need to complete the following steps.

  1. Prepare the training data: This step may involve operations such as data cleaning, transformation etc. 
  2. Create the model for prediction: During this step, the model of your choice needs to be initialized and configured.  
  3. Train the model: During this step, the model is trained on the data created in step 1 above,
  4. Deploy the model for prediction: This step accepts the test data and predicts the value of the target variable.

In this Article, let us explore three simple ways of performing Simple Linear Regression using the models such as:  

  1. Random Forest
  2. Support Vector Machine (SVM)
  3. Multi Layer Perceptron (MLP)

Let us consider the training data from the file 'Salaries.csv'.


Problem Statement: Using this data, we want to predict the salary of new person (target variable) using the parameter of 'no. of years of experience' (predictor variable).

In this Article, let us explore three simple ways of performing Simple Linear Regression using the models such as:  

  1. Random Forest
  2. Support Vector Machine (SVM)
  3. Multi Layer Perceptron (MLP)

4.      Let us explore these regression models. 
 

1.      1. Random Forest: A random forest is  an ensemble that consists of many decisions trees. It uses bagging and feature randomness when building each individual tree. While predicting, for the purpose of maximizing the prediction accuracy, it considers the prediction which has been generated by the maximum trees.  

       The 'sklearn' library in Python can be used to create the random forest as shown below.

 

Python3

# prediction using Random forest

# Importing the libraries

import pandas as pd

from sklearn.ensemble import RandomForestRegressor

 Now let us initialize the  training data set.

Python3

data = pd.read_csv('Salaries.csv')

x = data.iloc[:, 1:2].values  # so x=Yrs. of Experience

y = data.iloc[:, 2].values    # so y= Salary in Rs.

 Next step is to initialize the Random Forest model and feeding the training dataset to it.

Python3

# Create a Random Forest model.Default no of trees=100

model = RandomForestRegressor()

#Train the model using the training data

model.fit(x, y)

One the model is trained, you can use it for the task of prediction. Let us try to predict the salary of a person whi has experience of 7.4 years. 

Python3

 

#Predict the salary for test dataset

Y_pred = model.predict(np.array([7.4]).reshape(1, 1)) # test the output by changing values

print("Predicted Salary=", Y_pred)

 

Output: Predicted Salary= [82500.]

 

2. Support Vector Machine (SVM): A support vector machine (SVM) is a supervised machine learning model that can be used for both the tasks of classification and regression. After giving an SVM model sets of labeled training data they’re able to predict the target. The SVM models use kernel functions to avoid complex computations which make them suitable for handling the large data.

 The 'sklearn' library in Python can be used to create the SVM as shown below.

Python3

# prediction using SVM

from sklearn import svm

from sklearn import metrics

import pandas as pd

 

 

data = pd.read_csv('Salaries.csv')

x = data.iloc[:, 1:2].values  # so x=Yrs. of Experience

y = data.iloc[:, 2].values    # so y= Salary in Rs.

 

#Create a svm with Linear Kernel

model = svm.SVC() # model = svm.SVC(kernel='linear')

#Train the model using the training data

model.fit(x,y)

 

 

#Predict the salary for test dataset

y_pred = model.predict(np.array([7.4]).reshape(1, 1))

print("Predicted Salary=", y_pred)

 

Output: Predicted Salary= [80000]

3. Multi Layer Perceptron (MLP): It  is one of the most common neural network models used in machine learning. A multi-layered perceptron consists of interconnected neurons transferring information to each other. The MLP is a feedforward neural network, which means that the data is transmitted from the input layer to the output layer in the forward direction. The connections between the layers are assigned weights. The weight of a connection specifies its importance.  The technique of 'Backpropagation' is used to optimize the weights of an MLP till the weights converge to predict the correct values.  

The 'sklearn' library in Python can be used to create the MLP regressor as shown below.

Python3

# prediction using NN: MLP

 

from sklearn.neural_network import MLPRegressor

import pandas as pd

import numpy as np

 

data = pd.read_csv('Salaries.csv')

x = data.iloc[:, 1:2].values  # so x=Yrs. of Experience

y = data.iloc[:, 2].values    # so y= Salary in Rs.

 

# create the MLPRegressor model

nn = MLPRegressor(solver='lbfgs', alpha=1e-1, hidden_layer_sizes=(5, 2), random_state=0)

#Train the model using the training sets

nn.fit(x,y)

 

#predict the salary of a person who has experience of 7.4 years.

y_pred = nn.predict(np.array([7.4]).reshape(1, 1))

print("Predicted Salary=", y_pred)

 

Output: Predicted Salary= [88285.71344169]

Conclusion: The three models discussed have different levels of accuracy as depicted from the output obtained. So the 'prediction accuracy' parameter affects the decision of selecting the proper model for the task of prediction.  

 

 



Monday, December 20, 2021

Sentiment Analysis for Indic Language : Hindi

 This article exhibits how to use the library VADER  for doing the sentiment analysis.

Sentiment analysis is a metric to that conveys how positive or negative or neutral the text or data is. It is performed on textual data to help businesses monitor brand and product sentiment in customer feedback, and understand customer needs. It is time-efficient, cost-friendly solution to analyse huge data.

Python avails great support for doing sentiment analysis of data. Few of the libraries available for this purpose are: NLTK, TextBlob and VADER.

For doing sentiment analysis of Indic languages such as Hindi we need to do following tasks.

1.   Read the text file which is in Hindi.

2.   Translate the sentences in Hindi to the sentences in English as the python libraries do support text-analysis in the English language. (Even if you give the Hindi sentences to such functions the ‘compound score’ which is metric of the sentiment if the sentence is calculated in a wrong manner. So before computing this metric conversion to the equivalent sentence in the English language is appropriate.)  The Google Translator helps in this task.

3.   Do sentiment analysis of the translated text using any of the libraries mentioned above.

 The following steps need to be done.

Step 1: Import the necessary libraries / packages.


# codecs provides access to the internal Python codec registry

import codecs

 

# This is to translate the text from Hindi to English

from deep_translator import GoogleTranslator

 

# This is to analyse the sentiment of text

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

Step 2: Read the file data.  The ‘codecs’ library provides access to the internal Python codec registry.  Most standard codecs are text encodings, which encode text to bytes. Custom codecs may encode and decode between arbitrary types.

# Read the hindi text into 'sentences'

with codecs.open('SampleHindiText.txt', encoding='utf-8') as f:

    sentences = f.readlines()

§  Step 3: Translate the sentences read into the English so that VADER library can process the translated text for sentiment analysis. The polarity_scores() returns the sentiment dictionary of the text which includes the ‘'compound'’ score that tells about the sentiment of the sentence as given below.

* positive sentiment: compound score >= 0.05 

* Neutral sentiment : compound score > -0.05 and compound score < 0.05

* Negative sentiment : compound score <= -0.05

for sentence in sentences:

    translated_text = GoogleTranslator(source='auto', target='en').translate(sentence)

    #print(translated_text)

    analyzer = SentimentIntensityAnalyzer()

    sentiment_dict = analyzer.polarity_scores(translated_text)

   

    print("\nTranslated Sentence=",translated_text, "\nDictinary=",sentiment_dict)

    if sentiment_dict['compound'] >= 0.05 :

            print("It is a Poistive Sentence")

            

    elif sentiment_dict['compound'] <= - 0.05 :

            print("It is a Negative Sentence")     

    else :   

           print("It is a Neutral Sentence"))

·        The source file 'SampleHindiText.txt' is as given below.

गोवा की यात्रा बहुत अच्छी रही।

समुद्र तट बहुत गर्म थे।

मुझे समुद्र तट पर खेलने में बहुत मजा आया।

मेरी बेटी बहुत गुस्से में थी।

 

·        The output of the code is shown as below.



The article has been contributed by Rupali Kulkarni.

        rdkulkarni21@kkwagh.edu.in

    90118966811