Posts

Groupby with multiple functions and handling multiple index

Image
creating a data-set- import pandas as pd a='a' b='b' df= pd.DataFrame({'survived':[0,1,1,1,0,1],'class': [a,b,a,b,a,b]}) have a look of data-set- group by based on class - grouped_data= df.groupby(['class']).agg({'survived':['max', 'sum']}) have alook at grouped data , it has got multiple index now- we can delete on of the column index- grouped_data.columns=grouped_data.columns.get_level_values(1)

Words count in a sentence in Python

### counting number of words in Python- This requires use of counter from collection package, Also as split can be used only on strings, one needs to convert list into str. from collections import Counter sentence= ['Data science is growing field but buzz is also around and This is hyped'] splitted_sentence= sentence[0].split(" ")      # we will get str from list and then split would work, split doesn't work on list count_word= Counter() for i in splitted_sentence:     count_word[i]+= 1   # these are like dictionary, can be used to count words etc print(count_word)     output is- Counter({'is': 3, 'and': 1, 'hyped': 1, 'growing': 1, 'around': 1, 'field': 1, 'science': 1, 'buzz': 1, 'Data': 1, 'also': 1, 'but': 1, 'This': 1}) Thus we get the count of all the unique words. can you try list comprehension for the same?

List Comprehension in Python

list can be created in this was- list_variable= [ x for x in iterable] v=(1,4,9,16,25,36) now list comprehension can be use as- v=[x**2 for x in range(7)] Using for loop for getting even numbers in 10 natural numbers- numbers= range(10) newlist=[] for i in numbers:     if i%2 == 0:         newlist= new_list.append(i) print(newlist) Using list comprehension to do the same: newlist= [x for x in numbers if x%2 == 0] print(newlist) output in both the case- [0,2,4,6,8]

How to save a model variable in Python using pickle library

import pickle # take user input to take the amount of data number_of_data = int(input('Enter the number of data : ')) data = [] # take input of the data for i in range(number_of_data):     raw = input('Enter data '+str(i)+' : ')     data.append(raw) # open a file, where you ant to store the data file = open('important', 'wb') # dump information to that file pickle.dump(data, file) # close the file file.close()

Vectorization and looping in python

Vectorised and non vectorised code  comparison vectorization is required in python when we are dealing with matrics. With the evolution of deep learning it has gained more lime-light. Here is the exection time comparison of vectorised and non vectorised  code. # initialization of array  import numpy as np a=np.array([1,2,3,4]) print(a) [1 2 3 4] #initialize numpy array import time A = np.random.rand(1000000) B = np.random.rand(1000000) # calculating execution time using vectorization tic = time.time() C =np.dot(A, B) toc =time.time() print('total time taken in vectorised multiplication' + str(toc-tic) + 'mili-seconds') total time taken in vectorised multiplication0.002000093460083008mili-seconds # calculating execution time using non vectorization code tic = time.time() for i in range(1000000): C = C +A[i]*B[i] print(C) toc= time.time() print('total time taken in non-vectorised code'+ str(toc-tic) +' mili seconds') 749296...