Wednesday, 11 January 2023

Read from one csv file and write to new csv file

 Hi,

We can use pandas for reading csv file.

Using csv package we can write to another csv file.

import pandas as pd
hupactivity = pd.read_csv('huptest.csv')

import csv
csvoutput=open('data.csv''w')
writer = csv.writer(csvoutput, lineterminator='\n')

all = []

for i in hupactivity:
  print(i)
  all.append(row)
writer.writerows(all)

Use Google Drive in Google colab

 Hi All,

While analising data using Google colab, it is better to use google colab as the data will be permanently saved in drive.

from google.colab import drive
drive.mount('/content/drive')
cd /content/drive/MyDrive/HUPHealth

Here HUPHealth is your folder in Drive. You may change according to your choice.


Wednesday, 11 August 2021

Pandas and CSV

 Hi all,

Create a csv file like given below

No,Name,Place

1,HUP,Kollam

2,Abc,Test

3,Raj,Klm


#Program 1

import pandas as pd
df=pd.read_csv('hup1.csv')
print(df.to_string())


#Program 2

import pandas as pd
df=pd.read_csv('hup1.csv')
for ind in df.index:
  print(df['1'][ind], df['HUP'][ind])

Monday, 10 May 2021

Remove stop words and predict using Naive Bayes Classifier

 Hi all,

Use this code for NBC which removes stop words

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

from nltk import NaiveBayesClassifier as nbc


from nltk.tokenize import word_tokenize


from itertools import chain


import csv
from gensim.parsing.preprocessing import remove_stopwords

from nltk.tokenize import word_tokenize



with open('trainingdata.csv','r'as csvinput:


    reader=csv.reader(csvinput,delimiter=",")


    rownum = 0 


    training_data = []



    for row in reader:
      old=row[0]
      sent=remove_stopwords(row[0])
      row[0]=sent
     
      training_data.append (row)
      rownum += 1
      print('hup original ',old)
      print('hup new ',sent)
      print('----------------')



vocabulary = set(chain(*[word_tokenize(i[0].lower()) for i in training_data]))



feature_set = [({i:(i in word_tokenize(sentence.lower())) for i in vocabulary},tag) for sentence, tag in training_data]



classifier = nbc.train(feature_set)



with open('testdata.csv','r'as csvinput:


    with open('data.csv''w'as csvoutput:


        writer = csv.writer(csvoutput, lineterminator='\n')


        reader1 = csv.reader(csvinput)



        all = []


        row = next(reader1)


        



        for row in reader1:


            test_sentence = row[1]


            featurized_test_sentence =  {i:(i in word_tokenize(test_sentence.lower())) for i in vocabulary}


            print ("test_sent:",test_sentence)


            print ("tag:",classifier.classify(featurized_test_sentence))


            row.append(classifier.classify(featurized_test_sentence))


            all.append(row)


        writer.writerows(all)

Wednesday, 28 October 2020

Sort in reverse order in Python

 

Hi,

Here one list is loaded with cubes. One element is edited intentionally. 


a=[]

for i in range(10):

a.append(i**3)

a[4]=10

print(a)

a.sort(reverse=True)

print(a)

Array using Numpy and List in PythonTuple

 Hi all

Array can be implemented in Python using two methods

####Numpy

import numpy as np

hup=np.arange(10)

hup=np.zeros(10)

for i in range(10):

hup[i]=i**2


print(hup)

############

#Using List

a=[]

for i in range(10):

a.append(i**3)

print(a)


a = np.array([[10],
              [01]])
b = np.array([[41],
              [22]])
c=np.matmul(a, b)
print(c)

Monday, 7 September 2020

DB Connection from Python and Serial Port connection in Python

 Hi 

Use this code for Python MySql connection. You need mysql.connector for this purpose. you can install this using pip command in Python.

Also install serial using pip for serial connection.

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


import mysql.connector

import time;

import serial;

from mysql.connector import Error

from mysql.connector import errorcode

ser = serial.Serial('COM3', 9600, timeout=0,parity=serial.PARITY_EVEN, rtscts=1)


try:

   connection = mysql.connector.connect(host='localhost',

                             database='iot',

                             user='root',

                             password='')

   cur = connection.cursor()

   while True:

      s=ser.read();

      print(s);

      time.sleep(1);

      sql=("INSERT INTO timerecord (userid, recordtime, userrole ) VALUES (%s,%s,%s)")

      val=("1",s,"1")

      if s=="9":

         cur.execute(sql,val)

   connection.close()

except mysql.connector.Error as error :

    connection.rollback() #rollback if any exception occured

    print("Failed inserting record into python_users table {}".format(error))