Sunday, 12 April 2020

Create Environment and configure Tensorflow

Hi All,
We can configure Environment in Python and Configure Tensorflow using the below given statements.

1. install python
2. install virtual environment
pip install virtualenv
3. Create a floder c:\HUP
4. create virtual environment
python -m virtualenv c:\HUP
5. Activate virtual environment
c:\HUP\Scripts\activate

pip install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl

Sunday, 9 February 2020

ANN in Python

Hi all,
See the code.

//courtesy : Internet

# Perceptron Algorithm on the Sonar Dataset
from random import seed
from random import randrange
from csv import reader

# Load a CSV file
def load_csv(filename):
  dataset = list()
  with open(filename, 'r'as file:
    csv_reader = reader(file)
    for row in csv_reader:
      if not row:
        continue
      dataset.append(row)
  return dataset

# Convert string column to float
def str_column_to_float(datasetcolumn):
  for row in dataset:
    row[column] = float(row[column].strip())

# Convert string column to integer
def str_column_to_int(datasetcolumn):
  class_values = [row[column] for row in dataset]
  unique = set(class_values)
  lookup = dict()
  for i, value in enumerate(unique):
    lookup[value] = i
  for row in dataset:
    row[column] = lookup[row[column]]
  return lookup

# Split a dataset into k folds
def cross_validation_split(datasetn_folds):
  dataset_split = list()
  dataset_copy = list(dataset)
  fold_size = int(len(dataset) / n_folds)
  for i in range(n_folds):
    fold = list()
    while len(fold) < fold_size:
      index = randrange(len(dataset_copy))
      fold.append(dataset_copy.pop(index))
    dataset_split.append(fold)
  return dataset_split

# Calculate accuracy percentage
def accuracy_metric(actualpredicted):
  correct = 0
  for i in range(len(actual)):
    if actual[i] == predicted[i]:
      correct += 1
  return correct / float(len(actual)) * 100.0

# Evaluate an algorithm using a cross validation split
def evaluate_algorithm(datasetalgorithmn_folds, *args):
  folds = cross_validation_split(dataset, n_folds)
  scores = list()
  for fold in folds:
    train_set = list(folds)
    train_set.remove(fold)
    train_set = sum(train_set, [])
    test_set = list()
    for row in fold:
      row_copy = list(row)
      test_set.append(row_copy)
      row_copy[-1] = None
    predicted = algorithm(train_set, test_set, *args)
    actual = [row[-1for row in fold]
    accuracy = accuracy_metric(actual, predicted)
    scores.append(accuracy)
  return scores

# Make a prediction with weights
def predict(rowweights):
  activation = weights[0]
  for i in range(len(row)-1):
    activation += weights[i + 1] * row[i]
  return 1.0 if activation >= 0.0 else 0.0

# Estimate Perceptron weights using stochastic gradient descent
def train_weights(trainl_raten_epoch):
  weights = [0.0 for i in range(len(train[0]))]
  for epoch in range(n_epoch):
    for row in train:
      prediction = predict(row, weights)
      error = row[-1] - prediction
      weights[0] = weights[0] + l_rate * error
      for i in range(len(row)-1):
        weights[i + 1] = weights[i + 1] + l_rate * error * row[i]
  return weights

# Perceptron Algorithm With Stochastic Gradient Descent
def perceptron(traintestl_raten_epoch):
  predictions = list()
  weights = train_weights(train, l_rate, n_epoch)
  for row in test:
    prediction = predict(row, weights)
    predictions.append(prediction)
  return(predictions)

# Test the Perceptron algorithm on the sonar dataset
seed(1)
# load and prepare data
filename = 'tcsr.csv'
dataset = load_csv(filename)
for i in range(len(dataset[0])-1):
  str_column_to_float(dataset, i)
# convert string class to integers
str_column_to_int(dataset, len(dataset[0])-1)
# evaluate algorithm
n_folds = 3
l_rate = 0.01
n_epoch = 500
scores = evaluate_algorithm(dataset, perceptron, n_folds, l_rate, n_epoch)
print('Scores: %s' % scores)
print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores))))

Friday, 3 January 2020

Replace the tweet emoticon with sentiment text

Hi Dears,
See the code
This code helps to replace a text using another one in Python.
This is applied in sentiments analysis. So that the emoticons can be replaced by its sentiments.
This is used in the data mining part of the sentiments analysis using Python.


--------------------------
from nltk import NaiveBayesClassifier as nbc
from nltk.tokenize import word_tokenize
from itertools import chain
import csv
sentence="it is #abcd1212 and #12341212"
with open('emoticon.csv','r'as csvinput:
    reader=csv.reader(csvinput,delimiter=",")
    rownum = 0 
    training_data = []

    for code,tag in reader:
        sentence=sentence.replace(code,tag)
        
print(sentence)

------------------------------------
Output : it is positive and negativve
------------------------------------

emoticon.csv

#abcd1212,positive #12341212,negative #121212aa,positive

Thursday, 19 December 2019

Parse a textfile and generate tokens to a file in Python

Hi All

find the program.

------------------------------------
import re
def ngram_gen(sn):
    s = s.lower()
    s = re.sub(r'[^a-zA-Z0-9\s]'' ', s)
    tokens = [token for token in s.split(" "if token != ""]
    ngrams = zip(*[tokens[i:] for i in range(n)])
    return [" ".join(ngram) for ngram in ngrams]


fil=open("aaa.php""r")
cou=fil.read()
am= ngram_gen(cou, n=1)
am

str1=''
for i in am:
  print (i)
  i=i.replace("\n","")
  i=i.replace("\t","")
  str1=str1+" "+i
print(str1)
f = open("hup.csv""w")
f.write(str1)
f.close()

Tuesday, 22 October 2019

Canny Edge Detection in Python

Hi All,

See the code.
------------------------------------
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('tkm1.jpg',0)
edges = cv2.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

Tuesday, 15 October 2019

n-gram implementation in Python using PDF file Operations

Hi All,
See the codes...
---------------------------------------------------------------------------------------------------------------------
# code to generte ngrams

import re
def ngram_gen(s, n):
    s = s.lower()
    s = re.sub(r'[^a-zA-Z0-9\s]', ' ', s)
    tokens = [token for token in s.split(" ") if token != ""]
    ngrams = zip(*[tokens[i:] for i in range(n)])
    return [" ".join(ngram) for ngram in ngrams]


------------------------------------------------------------------------------------------

# code to read from text file and generating ngrams

fil=open("hup.txt", "r")
cou=fil.read()
am= generate_ngrams(cou, n=3)
am


-------------------------------------------------------------------------------------------

# code to read from pdf file and generating ngrams

pip install PyPDF2
pip install textract
pip install nltk
import PyPDF2
pdfFileObj = open('new.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
print(pdfReader.numPages)
pageObj = pdfReader.getPage(0)
z=pageObj.extractText()
bm= generate_ngrams(z, n=5)
bm


--------------------------------------------------------------------------------------------

# code to count plagiarism and store in array

j=0
cnt=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in am:
  c=bm.count(i)
  c=c+bm1.count(i)
  print(i,c)
  cnt[j]=c
  j=j+1
print(cnt)
print(cnt)
a=cnt[0:34].count(0)
p=((j-a)/j*100)
if p>25:
  print("\nplagiarism detected!!\n plagiarism level:",round(p),"%")


---------------------------------------------------------------------------------------------

# code to combine all files and count plagiarism

import os, fnmatch
c1=""
listOfFiles = os.listdir('.')
pattern = "*.pdf"
for entry in listOfFiles:
    if fnmatch.fnmatch(entry, pattern):
            pdfFileObj = open(entry, 'rb')
            pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
            print(pdfReader.numPages) 
            pageObj = pdfReader.getPage(0) 
            z1=pageObj.extractText()
            c1=c1+z1;
bm1= generate_ngrams(c1, n=5)
j=0
cnt=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in am:
  c=bm1.count(i)
  print(i,c)
  cnt[j]=c
  j=j+1
print(cnt)
a=cnt[0:34].count(0)
p=((j-a)/j*100)
if p>25:
  print("\nplagiarism detected!!\n plagiarism level:",round(p),"%")
           

--------------------------------------------------------------------------------------

Tuesday, 1 October 2019

Steps in Machine Learning

Hi All
Find the different steps in Machine Learning

---------------------------------
Train data

you are great,positive
its bad,negative
ok fine,neutral
-----------------------------------------------
Step1: Create Vocabulary

create a vocabulary=you are great its bad ok fine
--------------------------------------------------
Step 2; Training

1. create feature set

you:true are:true great:true its:false bad:false ok:false fine:false,positive

you:false are:false great:false its:true bad:true ok:false fine:false,negative

2. using feature set train the classifier(Naive bayes)

------------------------------------------------
Step3 :Testing
1. read the test data from console/csv/cloud/google drive
  it was fine
2. create featureset of test data

 you:false are:false great:false its:false bad:false ok:false fine:true



Testing