Docsity

Introduction to NumPy, Pandas, and Scikit-learn for Data Science, Summaries of Machine Learning

Partial preview of the text

Download Introduction to NumPy, Pandas, and Scikit-learn for Data Science and more Summaries Machine Learning in PDF only on Docsity!

INDEX

INTRODUCTION TO NUMPY:

NumPy stands for ‘Numerical Python’. It is a package for data analysis and scientific computing with Python. NumPy uses a multidimensional array object, and has functions and tools for working with these arrays. The powerful n-dimensional array in NumPy speeds-up data processing. NumPy can be easily interfaced with other Python packages and provides tools for integrating with other programming languages like C, C++ etc. NumPy is a Python package. It is a library consisting of multidimensional array objects and a collection of routines for processing of array. Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. Operations using NumPy Using NumPy, a developer can perform the following operations:--

  • Mathematical and logical operations on arrays
  • Fourier transforms and routines for shape manipulation
  • Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation. NumPy – A Replacement for MatLab NumPy is often used along with packages like SciPy (Scientific Python) and Mat−plotlib (plotting library). This combination is widely used as a replacement for MatLab, a popular platform for technical computing. However, Python alternative to MatLab is now seen as a more modern and complete programming language. It is open source, which is an added advantage of NumPy.

INTRODUCTION TO PANDAS:

Pandas is an open source, BSD-licensed library. It has high-performance, it is easy-to-use data structures and data analysis tools, built for the Python

programming language. The pandas package is the most important tool at the

disposal of Data Scientists and Analysts working in Python today. The powerful machine learning and glamorous visualization tools may get all the attention, but pandas is the backbone of most data projects. Pandas has so many uses that it might make sense to list the things it can't do instead of what it can do. This tool is essentially your data’s home. Through pandas, you get acquainted with your data by cleaning, transforming, and analyzing it.

UNIVARIATE VARIBLES:

1. HISTOGRAM

from matplotlib import pyplot from pandas import read_csv path = r"C:\pima-indians-diabetes.csv" names = ['preg', 'plas', 'pres', 'skin', 'test','mass','pedi', 'age', 'class'] data = read_csv(path, names=names) data.hist() pyplot.show()

2. DENSITY PLOTS:

from matplotlib import pyplot from pandas import read_csv path = r"C:\pima-indians-diabetes.csv" names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass','pedi', 'age', 'class'] data = read_csv(path, names=names) data.plot(kind='density', subplots=True, layout=(3,3), sharex=False) pyplot.show()

MULTIVARIATE VARIABLES

1. CORRELATION MATRIX PLOTS:

from matplotlib import pyplot from pandas import read_csv import numpy Path = r"C:\pima-indians-diabetes.csv" names = ['preg', 'plas', 'pres', 'skin', 'test','mass','pedi', 'age', 'class'] data = read_csv(Path, names=names) correlations = data.corr() fig = pyplot.figure() ax = fig.add_subplot(111) cax = ax.matshow(correlations, vmin=-1, vmax=1) fig.colorbar(cax) ticks = numpy.arange(0,9,1) ax.set_xticks(ticks) ax.set_yticks(ticks) ax.set_xticklabels(names) ax.set_yticklabels(names) pyplot.show()

2. SCATTER MATRIX PLOT:

from matplotlib import pyplot from pandas import read_csv from pandas.tools.plotting import scatter_matrix path = r"C:\pima-indians-diabetes.csv" names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass','pedi', 'age', 'class'] data = read_csv(path, names=names) scatter_matrix(data) pyplot.show()

2. L1 NORMALIZATION:

from pandas import read_csv from numpy import set_printoptions from sklearn.preprocessing import Normalizer path = r'C:\pima-indians-diabetes.csv' names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass','pedi', 'age', 'class'] dataframe = read_csv (path, names=names) array = dataframe.values Data_normalizer = Normalizer(norm='l1').fit(array) Data_normalized = Data_normalizer.transform(array)

3. L2 NORMALIZATION:

from pandas import read_csv from numpy import set_printoptions from sklearn. import Normalizer path = r'C:\pima-indians-diabetes.csv' names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] dataframe = read_csv (path, names=names) array = dataframe.values Data_normalizer = Normalizer(norm='l2').fit(array) Data_normalized = Data_normalizer.transform(array) set_printoptions(precision=2) print ("\nNormalized data:\n", Data_normalized [0:3])

5. STANDARDIZATION:

from sklearn.preprocessing import StandardScaler from pandas import read_csv from numpy import set_printoptions path = r'C:\pima-indians-diabetes.csv’ names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] dataframe = read_csv(path, names=names) array = dataframe.values data_scaler = StandardScaler().fit(array) data_rescaled = data_scaler.transform(array) set_printoptions(precision=2) print ("\nRescaled data:\n", data_rescaled [0:5])

DATA LABELING:

import numpy as np from sklearn import preprocessing input_labels = ['red','black','red','green','black','yellow','white'] encoder = preprocessing.LabelEncoder() encoder.fit(input_labels) test_labels = ['green','red','black'] encoded_values = encoder.transform(test_labels) print("\nLabels =", test_labels) print("Encoded values =", list(encoded_values)) encoded_values = [3,0,4,1] decoded_list = encoder.inverse_transform(encoded_values) print("\nEncoded values =", encoded_values) print("\nDecoded labels =", list(decoded_list))