In this we will provide the many practice task which useful to increase your skills in machine learning and data science.

If you need any help then you can contact us by sending the mail at: __realcode4you@gmail.com__ , our expert provide better help with an affordable price, here you can also get help in any other machine learning and data science related help.

**Practice Set 1(Introduction to pandas)**

Consider the following Python dictionary data and Python list labels:

data = {'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'], 'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4], 'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2], 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}

labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

1. Create a DataFrame birds from this dictionary data which has the index labels.

```
#import libraries
import pandas as pd
import numpy as np
```

`data = {'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'], 'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4], 'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2], 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}`

```
birds = pd.DataFrame(data, index = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
birds
```

Output:

2. Display a summary of the basic information about birds DataFrame and its data.

3. Print the first 2 rows of the birds dataframe

4. Print all the rows with only 'birds' and 'age' columns from the dataframe

5. select [2, 3, 7] rows and in columns ['birds', 'age', 'visits']

6. select the rows where the number of visits is less than 4

7. select the rows with columns ['birds', 'visits'] where the age is missing i.e NaN

8. Select the rows where the birds is a Cranes and the age is less than 4

9. Select the rows the age is between 2 and 4(inclusive)

10. Find the total number of visits of the bird Cranes

11. Calculate the mean age for each different birds in dataframe.

12. Append a new row 'k' to dataframe with your choice of values for each column. Then delete that row to return the original DataFrame.

13. Find the number of each type of birds in dataframe (Counts)

14. Sort dataframe (birds) first by the values in the 'age' in decending order, then by the value in the 'visits' column in ascending order.

15. Replace the priority column values with'yes' should be 1 and 'no' should be 0

16. In the 'birds' column, change the 'Cranes' entries to 'trumpeters'.

**Practice Set 2(Python Practice Questions)**

1. Write a function that inputs a number and prints the multiplication table of that number

2. Write a program to print twin primes less than 1000. If two consecutive odd numbers are both prime then they are known as twin primes

3. Write a program to find out the prime factors of a number. Example: prime factors of 56 - 2, 2, 2, 7

4. Write a program to implement these formulae of permutations and combinations. Number of permutations of n objects taken r at a time: p(n, r) = n! / (n-r)!. Number of combinations of n objects taken r at a time is: c(n, r) = n! / (r!*(n-r)!) = p(n,r) / r!

5. Write a function that converts a decimal number to binary number

6. Write a function cubesum() that accepts an integer and returns the sum of the cubes of individual digits of that number. Use this function to make functions PrintArmstrong() and isArmstrong() to print Armstrong numbers and to find whether is an Armstrong number.

7. Write a function prodDigits() that inputs a number and returns the product of digits of that number.

8. If all digits of a number n are multiplied by each other repeating with the product, the one digit number obtained at last is called the multiplicative digital root of n. The number of times digits need to be multiplied to reach one digit is called the multiplicative persistance of n.

Example:

86 -> 48 -> 32 -> 6 (MDR 6, MPersistence 3)

341 -> 12->2 (MDR 2, MPersistence 2)

Using the function prodDigits() of previous exercise write functions MDR() and MPersistence() that input a number and return its multiplicative digital root and multiplicative persistence respectively.

9. Write a function sumPdivisors() that finds the sum of proper divisors of a number. Proper divisors of a number are those numbers by which the number is divisible, except the number itself.

**For example** proper divisors of 36 are 1, 2, 3, 4, 6, 9, 18

10. A number is called perfect if the sum of proper divisors of that number is equal to the number. For example 28 is perfect number, since 1+2+4+7+14=28. Write a program to print all the perfect numbers in a given range

11. Two different numbers are called amicable numbers if the sum of the proper divisors of each is equal to the other number. For example 220 and 284 are amicable numbers.

Sum of proper divisors of 220 = 1+2+4+5+10+11+20+22+44+55+110 = 284

Sum of proper divisors of 284 = 1+2+4+71+142 = 220

Write a function to print pairs of amicable numbers in a range

12. Write a program which can filter odd numbers in a list by using filter function

13. Write a program which can map() to make a list whose elements are cube of elements in a given list

14. Write a program which can map() and filter() to make a list whose elements are cube of even number in a given list.

**Practice Set 3(Python Practice example(without sklearn and numpy))**

Q1: Given two matrices please print the product of those two matrices

```
Ex 1: A = [[1 3 4]
[2 5 7]
[5 9 6]]
B = [[1 0 0]
[0 1 0]
[0 0 1]]
A*B = [[1 3 4]
[2 5 7]
[5 9 6]]
Ex 2: A = [[1 2]
[3 4]]
B = [[1 2 3 4 5]
[5 6 7 8 9]]
A*B = [[11 14 17 20 23]
[18 24 30 36 42]]
Ex 3: A = [[1 2]
[3 4]]
B = [[1 4]
[5 6]
[7 8]
[9 6]]
A*B =Not possible
```

```
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input examples
# you can free to change all these codes/structure
# here A and B are list of lists
def matrix_mul(A, B):
# write your code
return(#multiplication_of_A_and_B)
matrix_mul(A, B)
```

Q2: Select a number randomly with probability proportional to its magnitude from the given array of n elements

Consider an experiment, selecting an element from the list A randomly with probability proportional to its magnitude. assume we are doing the same experiment for 100 times with replacement, in each experiment you will print a number that is selected randomly from A.

```
Ex 1: A = [0 5 27 6 13 28 100 45 10 79]
let f(x) denote the number of times x getting selected in 100 experiments.
f(100) > f(79) > f(45) > f(28) > f(27) > f(13) > f(10) > f(6) > f(5) > f(0)
```

```
from random import uniform
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input examples
# you can free to change all these codes/structure
def pick_a_number_from_list(A):
# your code here for picking an element from with the probability propotional to its magnitude
#.
#.
#.
return #selected_random_number
def sampling_based_on_magnitued():
for i in range(1,100):
number = pick_a_number_from_list(A)
print(number)
sampling_based_on_magnitued()
```

Q3: Replace the digits in the string with #

Consider a string that will have digits in that, we need to remove all the characters which are not digits and replace the digits with #

```
Ex 1: A = 234 Output: ###
Ex 2: A = a2b3c4 Output: ###
Ex 3: A = abc Output: (empty string)
Ex 5: A = #2a$#b%c%561# Output: ####
```

```
import re
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input examples
# you can free to change all these codes/structure
# String: it will be the input to your program
def replace_digits(String):
# write your code
#
return() # modified string which is after replacing the # with digits
replace_digits(String)
```

Q4: Students marks dashboard

Consider the marks list of class students given in two lists
Students = ['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10']
Marks = [45, 78, 12, 14, 48, 43, 45, 98, 35, 80]
from the above two lists the Student[0] got Marks[0], Student[1] got Marks[1] and so on.

Your task is to print the name of students

Who got top 5 ranks, in the descending order of marks

Who got least 5 ranks, in the increasing order of marks

Who got marks between >25th percentile <75th percentile, in the increasing order of marks.

```
Ex 1:
Students=['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10']
Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80]
a.
student8 98
student10 80
student2 78
student5 48
student7 47
b.
student3 12
student4 14
student9 35
student6 43
student1 45
c.
student9 35
student6 43
student1 45
student7 47
student5 48
```

```
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input examples
# you can free to change all these codes/structure
def display_dash_board(students, marks):
# write code for computing top top 5 students
top_5_students = # compute this
# write code for computing top least 5 students
least_5_students = # compute this
# write code for computing top least 5 students
students_within_25_and_75 = # compute this
return top_5_students, least_5_students, students_within_25_and_75
top_5_students, least_5_students, students_within_25_and_75 = display_dash_board(students, marks)
print(# those values)
```

Q5: Find the closest points

Consider you are given n data points in the form of list of tuples like S=[(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5),..,(xn,yn)] and a point P=(p,q) your task is to find 5 closest points(based on cosine distance) in S from P

```
Ex:
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1)(6,0),(1,-1)]
P= (3,-4)
```

```
Output:
(6,-7)
(1,-1)
(6,0)
(-5,-8)
(-1,-1)
```

```
import math
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input examples
# you can free to change all these codes/structure
# here S is list of tuples and P is a tuple ot len=2
def closest_points_to_p(S, P):
# write your code here
return closest_points_to_p # its list of tuples
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1)(6,0),(1,-1)]
P= (3,-4)
points = closest_points_to_p(S, P)
print() #print the returned values
```

Q6: Find which line separates oranges and apples

Consider you are given two set of data points in the form of list of tuples like

```
Red =[(R11,R12),(R21,R22),(R31,R32),(R41,R42),(R51,R52),..,(Rn1,Rn2)]
Blue=[(B11,B12),(B21,B22),(B31,B32),(B41,B42),(B51,B52),..,(Bm1,Bm2)]
```

and set of line equations(in the string format, i.e list of strings)

```
Lines = [a1x+b1y+c1,a2x+b2y+c2,a3x+b3y+c3,a4x+b4y+c4,..,K lines]
Note: You need to do string parsing here and get the coefficients of x,y and intercept.
```

Your task here is to print "YES"/"NO" for each line given. You should print YES, if all the red points are one side of the line and blue points are on other side of the line, otherwise you should print NO.

```
Ex:
Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
```

Output: YES NO NO YES

```
import math
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings
# you can free to change all these codes/structure
def i_am_the_one(red,blue,line):
# your code
return #yes/no
Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
for i in Lines:
yes_or_no = i_am_the_one(Red, Blue, i)
print() # the returned value
```

Q7: Filling the missing values in the specified format

You will be given a string with digits and '_'(missing value) symbols you have to replace the '_' symbols as explained

```
Ex 1: _, _, _, 24 ==> 24/4, 24/4, 24/4, 24/4 i.e we. have distributed the 24 equally to all 4 places
Ex 2: 40, _, _, _, 60 ==> (60+40)/5,(60+40)/5,(60+40)/5,(60+40)/5,(60+40)/5 ==> 20, 20, 20, 20, 20 i.e. the sum of (60+40) is distributed qually to all 5 places
Ex 3: 80, _, _, _, _ ==> 80/5,80/5,80/5,80/5,80/5 ==> 16, 16, 16, 16, 16 i.e. the 80 is distributed qually to all 5 missing values that are right to it
Ex 4: _, _, 30, _, _, _, 50, _, _
==> we will fill the missing values from left to right
a. first we will distribute the 30 to left two missing values (10, 10, 10, _, _, _, 50, _, _)
b. now distribute the sum (10+50) missing values in between (10, 10, 12, 12, 12, 12, 12, _, _)
c. now we will distribute 12 to right side missing values (10, 10, 12, 12, 12, 12, 4, 4, 4)
```

for a given string with comma seprate values, which will have both missing values numbers like ex: "_, _, x, _, _, _" you need fill the missing values

Q: your program reads a string like ex: "_, _, x, _, _, _" and returns the filled sequence

Ex:

```
Input1: "_,_,_,24"
Output1: 6,6,6,6
Input2: "40,_,_,_,60"
Output2: 20,20,20,20,20
Input3: "80,_,_,_,_"
Output3: 16,16,16,16,16
Input4: "_,_,30,_,_,_,50,_,_"
Output4: 10,10,12,12,12,12,4,4,4
```

```
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings
# you can free to change all these codes/structure
def curve_smoothing(string):
# your code
return #list of values
S= "_,_,30,_,_,_,50,_,_"
smoothed_values= curve_smoothing(S)
print(# print above values)
```

Q8: Find the probabilities

You will be given a list of lists, each sublist will be of length 2 i.e. [[x,y],[p,q],[l,m]..[r,s]] consider its like a martrix of n rows and two columns

The first column F will contain only 5 uniques values (F1, F2, F3, F4, F5)

The second column S will contain only 3 uniques values (S1, S2, S3)your task is to find a. Probability of P(F=F1|S==S1), P(F=F1|S==S2), P(F=F1|S==S3) b. Probability of P(F=F2|S==S1), P(F=F2|S==S2), P(F=F2|S==S3) c. Probability of P(F=F3|S==S1), P(F=F3|S==S2), P(F=F3|S==S3) d. Probability of P(F=F4|S==S1), P(F=F4|S==S2), P(F=F4|S==S3) e. Probability of P(F=F5|S==S1), P(F=F5|S==S2), P(F=F5|S==S3) Ex:

```
[[F1,S1],[F2,S2],[F3,S3],[F1,S2],[F2,S3],[F3,S2],[F2,S1],[F4,S1],[F4,S3],[F5,S1]]
a. P(F=F1|S==S1)=1/4, P(F=F1|S==S2)=1/3, P(F=F1|S==S3)=0/3
b. P(F=F2|S==S1)=1/4, P(F=F2|S==S2)=1/3, P(F=F2|S==S3)=1/3
c. P(F=F3|S==S1)=0/4, P(F=F3|S==S2)=1/3, P(F=F3|S==S3)=1/3
d. P(F=F4|S==S1)=1/4, P(F=F4|S==S2)=0/3, P(F=F4|S==S3)=1/3
e. P(F=F5|S==S1)=1/4, P(F=F5|S==S2)=0/3, P(F=F5|S==S3)=0/3
```

```
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings
# you can free to change all these codes/structure
def compute_conditional_probabilites(A):
# your code
# print the output as per the instructions
A = [[F1,S1],[F2,S2],[F3,S3],[F1,S2],[F2,S3],[F3,S2],[F2,S1],[F4,S1],[F4,S3],[F5,S1]]
compute_conditional_probabilites(A)
```

Q9: Operations on sentences

You will be given two sentances S1, S2 your task is to find

```
a. Number of common words between S1, S2
b. Words in S1 but not in S2
c. Words in S2 but not in S1
```

Ex:

```
S1= "the first column F will contain only 5 unique values"
S2= "the second column S will contain only 3 unique values"
Output:
a. 7
b. ['first','F','5']
c. ['second','S','3']
```

```
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings
# you can free to change all these codes/structure
def string_features(S1, S2):
# your code
return a, b, c
S1= "the first column F will contain only 5 uniques values"
S2= "the second column S will contain only 3 uniques values"
a,b,c = string_features(S1, S2)
print(#the above values)
```

Q10: Error Function

You will be given a list of lists, each sublist will be of length 2 i.e. [[x,y],[p,q],[l,m]..[r,s]] consider its like a martrix of n rows and two columns

a. the first column Y will contain interger values b. the second column 𝑌𝑠𝑐𝑜𝑟𝑒 will be having float values

```
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input string
# you can free to change all these codes/structure
def compute_log_loss(A):
# your code
return loss
A = [[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
loss = compute_log_loss(A)
print(# the above loss)
```

## תגובות