Thursday 15 November 2018

Shiny Code

library(shiny)
ui<-fluidPage(
  sidebarPanel(
    sliderInput("num","integer",1,200,5,step=1,animate = animationOptions(interval = 1000,loop = TRUE))),
  radioButtons("radio",label = h1("Choose one"),choices = list("Table"=1,"Even/Odd"=2,"Fact"=3),selected = 1),
  hr(),
  mainPanel(
    tableOutput("abc")
  )
)


server<-function(input,output)
{
 
    output$abc<-renderPrint({
      r<-input$radio
      x<-input$num
      if(r==1){
        for(i in 1:10)
        {
          cat(x,"X",i,"=",x*i,"<br>")
        }
      }else if(r==2)
      {
        if(x%%2==0)
        {
          print("Even")
        }else{
          print("Odd")
        }
      }
      else{
        print(factorial(x))
      }
    })
   
}   
   
shinyApp(ui=ui,server=server)

Wednesday 14 November 2018

R Softwares

R      - https://cran.r-project.org/bin/windows/base/
RStudio   - https://www.rstudio.com/products/rstudio/download/




Sunday 4 November 2018

OCR -AI -ML

https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.02-20180621.exe
________________________________________________________________________
create path variable:
TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR


_______________________________________________________________________
import os
import tempfile
import subprocess
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
def ocr(path):
    temp = tempfile.NamedTemporaryFile(delete=False)

    process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    process.communicate()

    with open(temp.name + '.txt', 'r') as handle:
        contents = handle.read()

    os.remove(temp.name + '.txt')

    #os.remove(temp.name)
    

    return contents


str = ocr(r'C:\Users\Freeware Sys\Downloads\alpha.jpg')
print(str)

MR - Classification

word_list = ['Emma','Woodhouse','father','Taylor','Miss','been','she','her']
    #i'm using this example text in place of the file you are using
text = 'This is an example text. It will contain words you are looking for, like Emma, Emma, Emma, Woodhouse, Woodhouse, Father, Father, Taylor,Miss,been,she,her,her,her. I made them repeat to show that the code works.'
text = text.replace(',',' ') #these statements remove irrelevant punctuation
text = text.replace('.','')
text = text.lower() #this makes all the words lowercase, so that capitalization wont affect the frequency measurement
for repeatedword in word_list:
        counter = 0 #counter starts at 0
        for word in text.split():
            if repeatedword.lower() == word:
                counter = counter + 1 #add 1 every time there is a match in the list
        print(repeatedword,':', counter) #prints the word from 'word_list' and its frequency

ML - ANN

from numpy import exp, array, random, dot
training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]).T
random.seed(1)
synaptic_weights = 2 * random.random((3, 1)) - 1
for iteration in range(10000):
    output = 1 / (1 + exp(-(dot(training_set_inputs, synaptic_weights))))
    synaptic_weights += dot(training_set_inputs.T, (training_set_outputs - output) * output * (1 - output))

print 1 / (1 + exp(-(dot(array([1, 0, 0]), synaptic_weights))))

ML - Decision Tree - Classification

install pydotplus and graphviz

Graphviz is a tool for drawing graphics using dot files. Pydotplus is a module to Graphviz’s Dot language.

import pydotplus
from sklearn.datasets import load_iris
from sklearn import tree
import collections

# Data Collection
X = [ [180, 15,0],    
      [177, 42,0],
      [136, 35,1],
      [174, 65,0],
      [141, 28,1]]

Y = ['man', 'woman', 'woman', 'man', 'woman']    

data_feature_names = [ 'height', 'hair length', 'voice pitch' ]

# Training
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)

# Visualize data
dot_data = tree.export_graphviz(clf,
                                feature_names=data_feature_names,
                                out_file=None,
                                filled=True,
                                rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)

colors = ('turquoise', 'orange')
edges = collections.defaultdict(list)

for edge in graph.get_edge_list():
    edges[edge.get_source()].append(int(edge.get_destination()))

for edge in edges:
    edges[edge].sort()    
    for i in range(2):
        dest = graph.get_node(str(edges[edge][i]))[0]
        dest.set_fillcolor(colors[i])


graph.write_png('tree.png')

Friday 2 November 2018

Private Cloud

https://drive.google.com/open?id=1yvF6w_SOHYKXyxrHQ1Fgd1tQY5HxCQyg

Blockchain with R

block_example <- list(index = 1,
                     timestamp = "2018-11-02 08.32 IST",
                     data = "Infoway",
                     previous_hash = 0,
                     proof = 9,
                     new_hash = NULL)
_________________________________________________________________
library("digest")
digest("Manish" ,"sha256")
________________________________________________________________

    hash_block <- function(block){
      block$new_hash <- digest(c(block$index,
                                 block$timestamp,
                                 block$data,
                                 block$previous_hash), "sha256")
      return(block)
    }
____________________________________________________________
      # Increment the proof number until a number is found that is divisable by 99 and by the proof of the previous block
   
proof_of_work <- function(last_proof){
      proof <- last_proof + 1
      while (!(proof %% 99 == 0 & proof %% last_proof == 0 )){
        proof <- proof + 1}
 return(proof) }
_________________________________________________________________
    gen_new_block <- function(previous_block){
      new_proof <- proof_of_work(previous_block$proof)
      new_block <- list(index = previous_block$index + 1,
                        timestamp = Sys.time(),
                        data = paste0("this is block ", previous_block$index +1),
                        previous_hash = previous_block$new_hash,
                        proof = new_proof)
      new_block_hashed <- hash_block(new_block)
      return(new_block_hashed) }
_____________________________________________________________________

    block_genesis <-  list(index = 1,
                           timestamp = Sys.time(),
                           data = "Genesis Block",
                           previous_hash = "0",
                           proof = 1)
_____________________________________________________________________
blockchain <- list(block_genesis)
    previous_block <- blockchain[[1]]

      # How many blocks should we add to the chain after the genesis block
      num_of_blocks_to_add <- 5

      # Add blocks to the chain
      for (i in 1: num_of_blocks_to_add){
        block_to_add <- gen_new_block(previous_block)
        blockchain[i+1] <- list(block_to_add)
        previous_block <- block_to_add

        print(cat(paste0("Block ", block_to_add$index, " has been added", "\n",
                   "\t", "Proof: ", block_to_add$proof, "\n",
                   "\t", "Hash: ", block_to_add$new_hash))) }