text: This tutorial helps you to get started with Python. data science professionals would rise by more than 25% by 2020. In the PyPL Popularity of Programming language index, Python scored second rank with a 14 percent share. In advanced analytics and predictive analytics market, it is ranked among top 3 programming languages for advanced analytics.
  - Getting Started with Python
  - Python 2.7 vs. 3.6
  - Python for Data Science
  - How to install Python?
  - Spyder Shortcut keys
  - Basic programs in Python
  - Comparison, Logical and Assignment Operators
  - Data Structures and Conditional Statements
  - Python Data Structures
  - Python Conditional Statements
  - Python Libraries
  - List of popular packages (comparison with R)
  - Popular python commands
  - How to import a package
  - Data Manipulation using Pandas
  - Pandas Data Structures - Series and DataFrame
  - Important Pandas Functions (vs. R functions)
  - Examples - Data analysis with Pandas
  - Data Science with Python
  - Logistic Regression
  - Decision Tree
  - Random Forest
  - Grid Search - Hyper Parameter Tuning
  - Cross Validation
  - Preprocessing Steps
  Python 2.7 vs 3.6
  Google yields thousands of articles on this topic. Some bloggers opposed and some in favor of 2.7. If you filter your search criteria and look for only recent articles (late 2016 onwards), you would see majority of bloggers are in favor of Python 3.6. See the following reasons to support Python 3.6.
  1. The official end date for the Python 2.7 is year 2020. Afterward there would be no support from community. It does not make any sense to learn 2.7 if you learn it today.
  2. Python 3.6 supports 95% of top 360 python packages and almost 100% of top packages for data science.
  What's new in Python 3.6
  It is cleaner and faster. It is a language for the future. It fixed major issues with versions of Python 2 series. Python 3 was first released in year 2008. It has been 9 years releasing robust versions of Python 3 series.
  Key Takeaway
  You should go for Python 3.6. In terms of learning Python, there are no major differences in Python 2.7 and 3.6. It is not too difficult to move from Python 3 to Python 2 with a few adjustments. Your focus should go on learning Python as a language.
  Python for Data Science
  Python.Do you know these sites are developed in Python?
  There are several reasons to learn Python. Some of them are as follows -.
  - YouTube
  - Dropbox
  - Disqus
  There are two ways to download and install Python
  - Download Anaconda. It comes with Python software along with preinstalled popular libraries.
  - Download Python from its official website. You have to manually install libraries.
  Recommended : Go for first option and download anaconda. It saves a lot of time in learning and coding Python
  Coding Environments
  Anaconda comes with two popular IDE :
  - Jupyter (Ipython) Notebook
  - Spyder
  Spyder Shortcut Keys
  Example 1
  #Basics
  x = 10
  y = 3
  print("10 divided by 3 is", x/y)
  print("remainder after 10 divided by 3 is", x%y)
  Result :Example 2
  10 divided by 3 is 3.33
  remainder after 10 divided by 3 is 1
  10 divided by 3 is 3.33
  remainder after 10 divided by 3 is 1
  x = 100
  x > 80 and x <=95
  x > 35 or x < 60
  x > 80 and x <=95 Out[45]: False
  x > 35 or x < 60 Out[46]: True Structure
  In every programming language, it is important to understand the data structures. Following are some data structures used in Python.
  1. List’]
  We
  A tuple is similar to a list in the sense that it is a sequence of elements. The difference between list and tuple are as follows -
  - A tuple cannot be changed once created whereas list can be modified.
  - A tuple is created by placing comma-separated values inside parentheses ( ). Whereas, list is created inside square brackets [ ]
  K = (1,2,3)Perform for loop on Tuple
  City = ('Delhi','Mumbai','Bangalore')
  for i in City:
  print(i)
  Delhi Mumbai Bangalore
  Like give z = sum_fun(10, 15)Conditional Statements (if else)
  Conditional statements are commonly used in coding. It is IF ELSE statements. It can be read like : " if a condition holds true, then execute something. Else execute something else"
  Note : The if and else statements ends with a colon :
  Example
  k = 27Result : Not a Multiple of 5
  if k%5 == 0:
  print('Multiple of 5')
  else:
  print('Not a Multiple of 5')
  Popular python packages for Data Analysis & Visualization.
  - Scipy. For mathematical and scientific functions such as integration, interpolation, signal processing, linear algebra, statistics, etc. It is built on Numpy.
  - Scikit-learn. A collection of machine learning algorithms. It is built on Numpy and Scipy. It can perform all the techniques that can be done in R using gl.
  Popular Python Commands
  The commands below would help you to install and update new and existing packages. Let's say, you want to install / uninstall pandas package.
  Install Package
  !pip install pandas
  Uninstall Package
  !pip uninstall pandas
  !pip show pandas
  List of Installed Packages
  !pip list
  Upgrade a package
  !pip install --upgrade pandas
  How to import a package
  There are multiple ways to import a package in Python. It is important to understand the difference between these styles.
  1. import pandas as pd
  It imports the whole package and the function DataFrame is executed simply by typing DataFrame. It sometimes creates confusion when same function name exists in more than one package.
  Pandas Data Structures : Series and DataFrame
  In pandas package, there are two data structures - series and dataframe. These structures are explained below in detail -
  In pandas package, there are two data structures - series and dataframe. These structures are explained below in detail -
  - Series is a one-dimensional array. You can access individual elements of a series using position. It's similar to vector in R.
  In the example below, we are generating 5 random values.
  import pandas as pd
  s1 = pd.Series(np.random.randn(5))
  s1
  0 -2.412015 1 -0.451752 2 1.174207 3 0.766348 4 -0.361815 dtype: float64
  Extract first and second value
  You can get a particular element of a series using index value. See the examples below -
  s1[0]
  -2.412015
  s1[1]
  -0.451752
  sas
  The following table shows how Python and pandas package stores data.
  Important Pandas Functions
  The table below shows comparison of pandas functions with R functions for various data wrangling and manipulation tasks. It would help you to memorise pandas functions. It's a very handy information for programmers who are new to Python. It includes solutions for most of the frequently used data exploration tasks.
  Data Manipulation with pandas - Examples
  1. Import Required Packages
  You can import required packages using import statement. In the syntax below, we are asking Python to import numpy and pandas package. The 'as' is used to alias package name.
  import numpy as np
  import pandas as pd
  2. Build DataFrame
  We can build dataframe using DataFrame() function of pandas package.
  mydata = {'productcode': ['AA', 'AA', 'AA', 'BB', 'BB', 'BB'],In this dataframe, we have three variables - productcode, sales, cost.
  'sales': [1010, 1025.2, 1404.2, 1251.7, 1160, 1604.8],
  'cost' : [1020, 1625.2, 1204, 1003.7, 1020, 1124]}
  df = pd.DataFrame(mydata)
  To import data from CSV file
  You columns
  You can run the command below to find out number of rows and columns.
  df.shapeResult : (6, 3). It means 6 rows and 3 columns.
  4. To view first 3 rows.6. To summarize data frame
  Drop Variable
  We can remove variables by using df.drop() function. See the example below -
  df[["productcode", "cost"]]
  df.loc[ : , ["productcode", "cost"]]
  Drop Variable
  We can remove variables by using df.drop() function. See the example below -
  df2 = df.drop(['sales'], axis = 1)
  We can manually find out summary statistics such as count, mean, median by using commands below
  df.sales.mean()
  df.sales.median()
  df.sales.count()
  df.sales.min()
  df.sales.max()
  8. Filter Data
  Suppose you are asked to apply condition - productcode is equal to "AA" and sales greater than or equal to 1250.
  Suppose Data
  In the code below, we are arrange data in ascending order by sales.
  10. Group By : Summary by Grouping Variable
  Like SQL GROUP BY, you want to summarize continuous variable by classification variable. In this case, we are calculating average sale and cost by product code.
  In the code below, we are arrange data in ascending order by sales.
  df.sort_values(['sales'])
  10. Group By : Summary by Grouping Variable
  Like
  Let
  You can calculate frequency distribution of a categorical variable. It is one of the method to explore a categorical variable.
  df['productcode'].value_counts()
  BB 3 AA 3
  12. Generate Histogram
  Hist
  Boxplot is a method to visualize continuous or numeric variable. It shows minimum, Q1, Q2, Q3, IQR, maximum value in a single graph.
  df.boxplot(column='sales')
  Data Science using Python - Examples
  Import the following libraries before reading or exploring data
  #Import required libraries
  import pandas as pd
  import statsmodels.api as sm
  import numpy as np
  2. Download and import data into Python
  With Data
  Let'})
  # SummarizeCategorical variable Analysis
  df.describe()
  # plot all of the columns
  df.hist()
  It is important to check the frequency distribution of categorical variable. It helps to answer the question whether data is skewed.
  # Summarize
  df.position.value_counts(ascending=True)
  1 61 4 67 3 121 2 151
  Generating Crosstab
  By
  We can write a simple loop to figure out the number of blank values in all variables in a dataset.
  for i in list(df.columns) :
  k = sum(pd.isnull(df[i]))
  print(i, k)
  In this case, there are no missing values in the dataset.
  4. Logistic Regression Model
  Log
  80%
  By default, the regression without formula style does not include intercept. To include it, we already have added intercept in X_train which would be used as a predictor.
  #Fit Logit modellogit = sm.Logit(y_train, X_train)result = logit.fit()#Summary of Logistic regression modelresult Ratio
  Odd ratio is exponential value of parameter estimates.
  #Confusion Matrix
  result.pred_table()
  #Odd Ratio
  np.exp(result.params)
  Prediction on Test Data
  In this step, we take estimates of logit model which was built on training data and then later apply it into test data.
  #prediction on test data
  y_pred = result.predict(X_test)
  Calculate Area under Curve (ROC)
  # AUC on test data
  false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
  auc(false_positive_rate, true_positive_rate)
  Result : AUC = 0.6763
  Calculate Accuracy Score
  accuracy_score([ 1 if p > 0.5 else 0 for p in y_pred ], y_test)
  Decision Tree Model
  Decision Model
  Decision Parameters Tuning
  The')
  Data Mining : Variables
  Suppose
  Imputing
  There)
  Next Steps
  Blatant copy paste from
  Haha. Did you see the author of the post on datasciencecentral? Read the first line - Guest blog by Deepanshu Bhalla.
  Well, on the bright side, the poster was looking out for you even if s/he didn't realize it. :-)