Search This Blog

Friday, March 22, 2019

Python datetime operations


Python has a module named datetime to work with date and time.

Below are few examples of various datetime operations in python.

import time
import datetime

print("Time in seconds: %s" %time.time())

print("Current date_time1: " , datetime.datetime.now()

print("Current date_time2: " ,datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))

print("Current year: ", datetime.date.today().strftime("%Y"))

print("Current month: ", datetime.date.today().strftime("%B"))

print("Current week number: ", datetime.date.today().strftime("%W"))

print("Weekday of the week: ", datetime.date.today().strftime("%w"))

print("Day of year: ", datetime.date.today().strftime("%j"))

print("Day of the month: ", datetime.date.today().strftime("%d"))

print("Day of week: ", datetime.date.today().strftime("%A"))

Output:

Time in seconds: 1553207598.3700132
Current date_time1:  2019-03-21 22:33:18.370013
Current date_time2:  19-03-21-22-33
Current year:  2019
Current Month:  March
Week number of the year:  11
Weekday of the week:  4
Day of year:  080
Day of the month:  21
Day of week:  Thursday

Thursday, March 21, 2019

File operations in Python


The different file operation in Python are
  • Open a file
  • Read or write
  • Close the file

Open()

Python has a built-in function open() to open a file. The open() function is used to open files in our system, the filename is the name of the file to be opened.

f = open("<Filename>")

We can specify the mode while opening a file. In mode, we specify whether we want to read 'r', write 'w' or append 'a' to the file.

The default is reading in text mode. In this mode, we get strings when reading from the file.

Example

filename = "hello.txt"
file = open(filename, "r")
for line in file:
   print line,

Read ()

The read functions contains following methods
  • read()           #return one big string
  • readline        #return one line at a time
  • readlines       #returns a list of lines

Write ()

This method writes a sequence of strings to the file.
  • write ()          #Used to write a fixed sequence of characters to a file
  • writelines()    #writelines can write a list of strings.

Append ()

The append function is used to append to the file instead of overwriting it.
To append to an existing file, simply open the file in append mode ("a")

Close()

When you’re done with a file, use close() to close it and free up any system
resources taken up by the open file

File Handling Examples

To open a text file, use:
f = open("hello.txt", "r")


To read a text file, use:
f = open("hello.txt","r")
print(f.read())


To read one line at a time, use:
f = open("hello".txt", "r")
print(f.readline())


To read a list of lines use:
f = open("hello.txt.", "r")
print(f.readlines())


To write to a file, use:
f = open("hello.txt","w")
write("Hello World")
f.close()


To write to a file, use:
f = open("hello.txt", "w")
text_lines = ["text line 1", "text line 2", "text line 3"]
f.writelines(text_lines)
f.close()


To append to file, use:
f = open("hello.txt", "a")
write("Hello World again")
f.close()


To close a file, use
f = open("hello.txt", "r")
print(f.read())
f.close()

Python Basics for beginners

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990.

There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 3, because it more semantically correct and supports newer features.

First Python Program

The simplest command in python is the "print" command. The print command prints the output.

In Python 2, the "print" statement is not a function, and therefore it is invoked without parentheses. 
However, in Python 3, it is a function, and must be invoked with parentheses. 

Below is a simple syntax to print "Hello World!"

print("Hello World!")

Lines and Indentation

Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example −

if True:
   print "True"
else:
   print "False"

However, the following block generates an error −

if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"

Thus, in Python all the continuous lines indented with same number of spaces would form a block.

 Standard Data Types

Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.
Python has five standard data types −
  • Numbers
  • String
  • List
  • Tuple
  • Dictionary


Basic Operators

Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.

Types of Operator
Python language supports the following types of operators.

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Functions

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

Defining a Function

  • You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
  • Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
  • The first statement of a function can be an optional statement - the documentation string of the function or docstring.
  • The code block within every function starts with a colon (:) and is indented.
  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Syntax
def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]

Machine Learning



What is Machine Learning (ML)?
Machine Learning is programming machines to make it capable of taking decision itself based on their experience.
Machine Learning is a system that can learn from example through self-improvement and without being explicitly coded by programmer. The breakthrough comes with the idea that a machine can singularly learn from the data (i.e., example) to produce accurate results.
Machine learning combines data with statistical tools to predict an output. This output is then used by corporate to make actionable insights. Machine learning is closely related to data mining and Bayesian predictive modelling. The machine receives data as input, use an algorithm to formulate answers.

Machine Learning v/s Traditional programming

Traditional programming differs significantly from machine learning.
In traditional programming, data and program are run on the computer to produce the output.


In Machine Learning, data and output are run on the computer to create a program. This program can be used in traditional programming.

Types of Learning
Learning is the process of converting experience into expertise or knowledge.
Learning can be broadly classified into categories, as mentioned below, based on the nature of the learning data and interaction between the learner and the environment.

  •  Supervised learning:
o   The program is “trained” on a pre-defined set of “training examples”, which then facilitate its ability to reach an accurate conclusion when given new data.
o   Supervised learning can be further classified into two types:
Ø  Regression
Ø  Classification
o   Supervised learning is commonly used in real world applications, such as face and speech recognition, products or movie recommendations, and sales forecasting.


  • Unsupervised learning:
o   The program is given a bunch of data and must find patterns and relationships therein.
o   It is the opposite of supervised learning.
o   Unsupervised learning is used to detect anomalies, outliers, such as fraud or defective equipment, or to group customers with similar behaviours for a sales campaign.

  •  Semi-supervised Learning:
o   If some learning samples are labelled, but some other are not labelled, then it is semi-supervised learning.
o   It makes use of a large amount of unlabelled data for training and a small amount of labelled data for testing.
o   Semi-supervised learning is applied in cases where it is expensive to acquire a fully labelled dataset while more practical to label a small subset.

  •  Reinforcement Learning:
o   Learning data gives feedback so that the system adjusts to dynamic conditions to achieve a certain objective.
o   The system evaluates its performance based on the feedback responses and reacts accordingly.
o   AI types like it, it is the most ambitious type of learning.
Similarly, there are four categories of machine learning algorithms as shown below −

  • Supervised learning algorithm
  •  Unsupervised learning algorithm
  •  Semi-supervised learning algorithm
  •  Reinforcement learning algorithm
However, the most commonly used ones are supervised and unsupervised learning.



Machine Learning Process





Purpose of Machine Learning
Machine learning can be seen as a branch of Artificial Intelligence (AI), since, the ability to change experience into expertise or to detect patterns in complex data is a mark of human or animal intelligence.
As a field of science, machine learning shares common concepts with other disciplines such as statistics, information theory, game theory, and optimization.
As a subfield of information technology, its objective is to program machines so that they will learn.
However, it is to be seen that, the purpose of machine learning is not building an automated duplication of intelligent behaviour but using the power of computers to complement and supplement human intelligence. For example, machine learning programs can scan and process huge databases detecting patterns that are beyond the scope of human perception.

Applications
Below are some of the applications of Machine Learning:

  • Web search: ranking page based on what you are most likely to click on.
  •  Finance: decide who to send what credit card offers to. Evaluation of risk on credit offers. How to decide where to invest money.
  •  E-commerce:  Predicting customer churn. Whether or not a transaction is fraudulent.
  •  Space exploration: space probes and radio astronomy.
  •  Robotics: how to handle uncertainty in new environments. Autonomous. Self-driving car.
  •  Social networks: Data on relationships and preferences. Machine learning to extract value from data.
  •  Debugging: Use in computer science problems like debugging. Labour intensive process. Could suggest where the bug could be.

Challenges
The primary challenge of machine learning is the lack of data or the diversity in the dataset. A machine cannot learn if there is no data available. Besides, a dataset with a lack of diversity gives the machine a hard time. A machine needs to have heterogeneity to learn meaningful insight. It is rare that an algorithm can extract information when there are no or few variations. It is recommended to have at least 20 observations per group to help the machine learn. This constraint leads to poor evaluation and prediction.

Conclusion
Machine learning is used by many known organizations in many different fields. However, this technology is quite new and not very mature, so it contains many security and ethical problems. However, this technology can help us in dealing with a lot of problems such as security. Machine learning has a lot of scope and with advancement in technology, it will get better n better.