Skip to main content

Posts

Showing posts from June, 2018

Machine Learning — Supervised, Unsupervised, and Reinforcement — Explanation with Example

🤖 Let's take an example of machine learning and see how it can be performed in three different ways — Supervised, Unsupervised, and Reinforcement. We want a program to be able to identify apple in pictures Supervised Learning You will create or use a model that takes a set of pictures of apple and it analyses the commonality in those pictures. Now when you show a new picture to the program, it will identify whether it has an apple or not. It can also provide details on how confident is the program about it. Unsupervised Learning In this method, you create or use a model that goes through some images and tries to group them as per the commonalities it observes such as color, shape, size, partern, etc. And now you can go through the groups and inform the program what to call them. So, you can inform the program about the group that is apple mostly. Next time you show a picture, it can tell if an apple is there or not. Reinforcement Learning Here the model you create or...

How to go from Andheri in Mumbai to Pune by Road?

🚌 Here is how you go from Andheri in Mumbai to Pune by Road. Take an auto or walk to Bisleri Bus Stop Take a Bus for Pune. It cost me ₹400 This is the best way to travel to Pune from Andheri in Mumbai by Road on bus. My friends use car pooling on bla bla car. That is cheaper but will require coordination + could be a risk for women.

How to go Andheri from Pune by road?

🛣️  I am putting out how I managed to go from Pune to Andheri in Mumbai. I had to reach Zipgrid office in Andheri. It is near MagicBricks Weh Metro Station. Took Shivneri Bus from Indira College, Wakad that was going to Borivali via Powai. I get down at Jogeshwari. The bus cost me 560. I would suggest to take non-Shivneri, it is cheaper. From Jogeshwari I take an auto to Magicbricks Weh Metro Station. That cost me 33 rupees. That is where is Zipgrid Office. The firm is also known as MyAashiana. It is in the building opposite to PVR. The building's name is The Summit Business Bay by Omkar. Now you know how to go Andheri in Mumbai from Pune by road. You could also opt for bla bla car or anything where you can share transport, it will cost you similar.

What is Model View Controller (MVC)?

💻 Model View Controller (MVC) is an architectural paradigm. It is popular because a lot of frameworks use it, such as Ruby on Rails, cakephp, and Django. Clients are generally browsers. It is basically an envirnment users use to access the application. Servers are where programs are run to fulfill the needs of a client. Database is where data is stored. Servers can pull data from database, processes it as per the needs of the client and sends to the client where is it is displayed to the user. In MVC architecture, separate files take care of these three parts. Model takes care of the Database related activities View takes care of the Client Controller takes care of the Server View listens to the Controller and Controller talks to the Model and vice versa. At all times Controller is there in between the two. There is no direct connection between View and Model.

Python — With Statement — A Context Manager

🐍 When you open a file, you are required to close it too. If between your open statement and close statement there are n other statements, and in any of the statement an error occurs, your program won't reach the point to close the file. It will remian opened. Try this to understand the issue. Input f = open('some_file.txt', 'a') f.readlines() f.close() Output UnsupportedOperation Error Input f.write('add more text to test if file is open') Output 37 The above example shows that at line 2 an error occurred and the program didn't reach line 3 to close the file. Later we wrote something to the file and it worked, which means the file is still open. Run the close statement again to close it. Input f.close() Protect the file with try/except/finally One solution to this is use of try/except/finally statements as follows. Input f.open('some_file.txt', 'a') try: f.readlines() except: print('An exception was raised.') finally: ...

Python - List - Append, Count, Extend, Index, Insert, Pop, Remove, Reverse, Sort

🐍 Advance List List is widely used and it's functionalities are heavily useful. Append Adds one element at the end of the list. Syntax list1.append(value) Input l1 = [1, 2, 3] l1.append(4) l1 Output [1, 2, 3, 4] append can be used to add any datatype in a list. It can even add list inside list. Caution: Append does not return anything. It just appends the list. Count .count(value) counts the number of occurrences of an element in the list. Syntax list1.count(value) Input l1 = [1, 2, 3, 4, 3] l1.count(3) Output 2 It returns 0 if the value is not found in the list. Extend .count(value) counts the number of occurrences of an element in the list. Syntax list1.extend(list) Input l1 = [1, 2, 3] l1.extend([4, 5]) Output [1, 2, 3, 4, 5] If we use append, entire list will be added to the first list like one element. Extend, i nstead of considering a list as one element, it joins the two lists one after other. Append works in the following way. Input l1 = [1, 2, 3] l1.append([4, 5]) Output...

Python - Dictionary - Comprehension, Iteration, Items, Keys, Values

🐍 Advance Dictionaries There is very little one can do with dictionaries. Dictionaries are very useful, but because of their relatively complex state, very few modifications can be done in simple ways. Here are a couple of things you do with dictionaries. Dictionary Comprehension Just like List Comprehensions, Dictionary Data Types also support their own version of comprehension for quick creation. It is not as commonly used as List Comprehensions. Here is an example of Dictionary Comprehension. Input d = {x:x**2 for x in range(10)} d Output {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81} Another example where we pick keys from one place and values from another. Input d = {k:v**2 for k,v in zip(['a','b','c','d','e','f','g','h','i','j'],range(10))} d Output {'a': 0, 'b': 1, 'c': 4, 'd': 9, 'e': 16, 'f': 25, 'g': 36, 'h': 49, ...

Python - Set Functions - add, clear, difference, discard, intersection, checks, union, update

🐍 Advanced Sets Let's create a blank set and learn by example. Input s = Set() Add Elements to Set Input s.add(1) s.add(2) s Output {1, 2} This adds new elements to our existing set. Clear Input s.clear() s Output set() This empties the set. It deletes all elements. Copy Input s = {1,2,3} sc = s.copy() print(s,sc) Output {1, 3, 4} {1, 2, 3} This copies elements from one set to another. Difference Syntax set1.difference(set2) Input s.add(4) s.difference(sc) Output {4} This returns elements that are not common in the two sets. Difference Update Syntax set1.difference_update(set2) Input s1 = {1,2,3} s2 = {1,4,5} s1.difference_update(s2) s1 Output {2, 3} This updates set1 to the difference between set1 and set2. Discard Syntax set.discard(element) Input s = {1,2,3,4} s.discard(2) s Output {1, 3, 4} This deletes an element from the set, if that element exists in the set. Intersection Syntax set1.intersection(set2) Input s1 = {1,2,3} s2 = {2,3,4} s1.intersection(s2) Output {2, 3} It ret...

Python - String Functions - Changing Case, Location, Counting, Formatting, Checking, Regular Expressions

🐍 Some of the String Functions include the following: .capitalize()  .upper()  .lower()  .title()  .count()  .find()  .center()  .expandtabs()  .isalnum()  .isalpha()  .islower()  .isspace()  .istitle()  .isupper()  .endswith() .split() .partition()  Let's take an example string and apply all methods on it. s = 'hello 1st World, 2nd World, and 3rd World.' Changing case Capitalize s.capitalize() Output: 'Hello 1st world, 2nd world, and 3rd world' It capitalizes the first letter of the string   for that output alone . Uppercase s.upper() Output: 'HELLO 1ST WORLD, 2ND WORLD, AND 3RD WORLD' It converts entire string into upper-case for that output alone. Lowercase s.lower() Output: 'hello 1st world, 2nd world, and 3rd world' It converts entire string into lower-case   for that output alone . Location and Counting Count s.count('o') Output: 4 It counts number of occurrences of the string passed in the strin...

Python - Mathematical Functions - Hexadecimal, Binary, Exponential, Absolute, Round

🐍 Some mathematical functions are in-built in python. Here are some of them. Convert Decimal to Hexadecimal hex(246) Output: '0xf6' hex(512) Output: '0x200' 0x is prefix to all hexadecimal numbers in output. The number after 0x is the hexadecimal value. Convert Decimal to Binary bin(1234) Output: '0b10011010010' bin(246) Output: '0b11110110' bin(512) Output: '0b1000000000' Exponential Function pow() function is similar to num1**num2, but it comes useful when you want to execute (num1**num2)%num3. Passing three arguments to pow(), does this. pow(3,4) Output: 81 This works just like 3**4 pow(3,4,5) Output: 1 But this, works equivalent to (3**4)%5. Either of the two ways can be used. At this point I feel (3**4)%5 is better, as anybody reading the code will understand what is being done immediately. While with pow(2,3,4), the person may have to refer to documentation to understand how this function works. Absolute Value The function abs() is used ...

Python - StringIO - Treat Text Like File

🐍 At times we need a file to run a code. Python allows you to treat a text/string like a file for that purpose. It is not really a file, but it lets you treat it that way. You need a module called StringIO for it. Use Jupyter Notebooks to run the following code. StringIO Let's begin by importing StringIO import io Next, we create a string and store it in a variable. message = 'This is just a normal string.' Let's apply StringIO magic to it and make it look like a file. f = io.StringIO(message) Now, we can do all file operations on it. Like read f.read() Output: 'This is just a normal string.' Like Write f.write(' Second line written to file like object') Output: 40 Like moving cursor to a location in the file f.seek(0) Output: 0 Like read again f.read() Output: 'This is just a normal string. Second line written to file like object' Like closing the file f.close() After closing 'f' cannot carry out the file functions.

Python - Parsing or Regular Expression or Regex or RE (search, split, findall)

🐍  Parsing is way too simple in python. Basic parsing is done using the library re (Regular Expression). TRY ON JUPYTER NOTEBOOK Following should be the first line to start using functionalities of Regular Expressions: import re re.search(pattern, text) import re text = 'This is a string with term1, but it does not have the other term.' pattern = 'term1' print(re.search(pattern, text)) This will search 'term1' in the text and return Details, since it found it in the text. import re text = 'This is a string with term1, but it does not have the other term.' pattern = 'term2' print(re.search(pattern, text)) This will search 'term2' in the text and return 'None', since it didn't find it. re.search(pattern, text).start() It return the position of the first character of the pattern you are searching in the text. import re text = 'This is a string with term1, but it does not have the other term...