Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,813 members, 7,817,359 topics. Date: Saturday, 04 May 2024 at 10:52 AM

Data Science With Python Tutorial - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Data Science With Python Tutorial (2530 Views)

Python Tutorial Group / Learn Data Science With Ease / Learn Data Science With Python (2) (3) (4)

(1) (2) (Reply) (Go Down)

Data Science With Python Tutorial by lahp(m): 11:24pm On Feb 15, 2019
I will be conducting a tutorial on data science using python
we shall do an extensive tutorial on python packages like: numpy, scipy, pandas etc.
also carry out computing on the above mentioned packages.

This tutorial is not for those who are not familiar with python syntax.


after getting drowned in those packages
we shall finally swim our way to the shores of machine learning.
so be atikulated enough to draw from my pool of knowledge and we shall all move to the next level
Re: Data Science With Python Tutorial by lambvard: 11:36pm On Feb 15, 2019
Ok
Re: Data Science With Python Tutorial by lahp(m): 11:45pm On Feb 15, 2019
DAY 1 : DATA SCIENCE??


What is data science?

as we know data is raw information. data varies from all works of life, the traders in the market deal on the demand of customers over a particular item x.
the data when collected is manipulated by the trader to know the exact amount of supply to be given to the user...So data science by my definition is the manipulation of data to derive certain results from the data....

there are 4 steps in data science

1: get data
2: clean data
3: build a model to test the data
4: u output the result of your test

1 Like

Re: Data Science With Python Tutorial by Nbote(m): 11:46pm On Feb 15, 2019
I must follow U wherever U go

2 Likes

Re: Data Science With Python Tutorial by lahp(m): 11:47pm On Feb 15, 2019
feedbacks will be appreciated
Re: Data Science With Python Tutorial by lahp(m): 12:14am On Feb 16, 2019
WHAT LANGUAGE IS SUITABLE FOR DATA SCIENCE


There are variety of languages used in data science
but the most used is R and python

but python is widely acceptable because Python is emerging as the popular
language used more in data science applications. ...
Python has other advantages that speed up it's
upward swing to the top of data science tools. It
integrates well with the most cloud as well as
platform-as-a-service providers.
it's has packages which perform mathematical operations quickly than other languages
it's syntax is also easy to understand
Python can also be extended with modules in c/c++


HOW TO DOWNLOAD PYTHON:

www.python.org/downloads

you can download the jupyter notebook which has a python interface (ipython)
which quickly compiles and runs the python script quickly.
jupyter can be downloaded with anaconda

https://www.anaconda.com


follow the instructions to download and install

NOTE: JUPYTER NOTEBOOK COMES WITH PYTHON PACKAGES ALREADY INSTALLED...
DOWNLOAD PYTHON 3.7 WHICH IS THE LATEST VERSION
PYTHON 2.7 IS THE LAST AND FINAL VERSION OF PYTHON 2
Re: Data Science With Python Tutorial by lahp(m): 12:21am On Feb 16, 2019
if any installation error occurs feel free to call my attention and is will help u out
Re: Data Science With Python Tutorial by lahp(m): 12:31am On Feb 16, 2019
Nbote:
I must follow U wherever U go


pls do. I promise to hold u by the hand and take u on a tour of this beautiful jungle called data science...
there shall be pools of knowledge so clear like crystal
feel free and secured to take a drink and feel refreshed


There are a whole lot of things data can be done with

u can mention friends too

1 Like

Re: Data Science With Python Tutorial by Nbote(m): 6:22am On Feb 16, 2019
lahp:



pls do. I promise to hold u by the hand and take u on a tour of this beautiful jungle called data science...
there shall be pools of knowledge so clear like crystal
feel free and secured to take a drink and feel refreshed


There are a whole lot of things data can be done with

u can mention friends too

But I don't have any programming knowledge.. Will it b a problem
Re: Data Science With Python Tutorial by EngrBouss(m): 5:43pm On Feb 16, 2019
Will this be a WhatsApp group because I am interested
Re: Data Science With Python Tutorial by EngrBouss(m): 5:44pm On Feb 16, 2019
I would like to join in
Re: Data Science With Python Tutorial by lahp(m): 7:10pm On Feb 16, 2019
DAY 2: python packages: Numpy


Data science is the new oil because with data science
companies have gained insight into the market and how best to improve it's services.
As a data scientist there are times where we have to change data to numerical values that the machine/computer understands.
Data is so large to be quantified, making manipulation stressful and time consuming u might end up sleeping while on ur machine..
Python came to the rescue with a package/ library called NumPy

WHAT IS NumPy??
NumPy is an acronym for numerical python
is a library for the Python
programming language , adding support for large, multi-
dimensional arrays and matrices , along with a large
collection of high-level mathematical functions to
operate on these arrays. The ancestor of NumPy,
Numeric, was originally created by Jim Hugunin with
contributions from several other developers. In 2005,
Travis Oliphant created NumPy by incorporating features
of the competing Numarray into Numeric, with extensive
modifications. NumPy is open-source software and has
many contributors.
You can get NumPy package via
http:// www.numpy. org
Re: Data Science With Python Tutorial by lahp(m): 7:14pm On Feb 16, 2019
Nbote:

But I don't have any programming knowledge.. Will it b a problem
through this tutorial u shall learn syntaxes of the python language
but I might recommend you taking a tutorial on the python language
I can Lead u through the fundamentals of python or better yet u can get a tutorial in their website
Re: Data Science With Python Tutorial by lahp(m): 7:15pm On Feb 16, 2019
EngrBouss:
Will this be a WhatsApp group because I am interested



for now no whatsapp group lets see later on
Re: Data Science With Python Tutorial by Nbote(m): 7:32pm On Feb 16, 2019
lahp:


through this tutorial u shall learn syntaxes of the python language

but I might recommend you taking a tutorial on the python language

I can Lead u through the fundamentals of python
or better yet u can get a tutorial in their website

Can U recommend any gud site I can get python tutorials while also following urs up
Re: Data Science With Python Tutorial by lahp(m): 7:37pm On Feb 16, 2019
Nbote:


Can U recommend any gud site I can get python tutorials while also following urs up




https://www.tutorialspoint.com/python
Re: Data Science With Python Tutorial by Nbote(m): 7:44pm On Feb 16, 2019
lahp:




https://www.tutorialspoint.com/python
God bless U and increase ur knowledge baba..
Re: Data Science With Python Tutorial by amadiwati(m): 7:47pm On Feb 16, 2019
God bless you sir, carry on with your lectures.
Re: Data Science With Python Tutorial by greatface(m): 8:28pm On Feb 16, 2019
Nbote:


God bless U and increase ur knowledge baba..
The official python tutorial is awesome and it is also a good idea to match the version of your python installation with that of the tutorial as a beginner.

Python is a good language and takes you through nearly all terrain.
Re: Data Science With Python Tutorial by greatface(m): 8:32pm On Feb 16, 2019
Am with you on this lahp, just that the nairaland bread of tutorials always keep me doubting.
Re: Data Science With Python Tutorial by lahp(m): 8:47pm On Feb 16, 2019
greatface:
Am with you on this lahp, just that the nairaland bread of tutorials always keep me doubting.

doubting on what?
Re: Data Science With Python Tutorial by greatface(m): 8:53pm On Feb 16, 2019
lahp:

doubting on what?
Most of them were half completed.
Re: Data Science With Python Tutorial by lahp(m): 8:58pm On Feb 16, 2019
hope you guys are ready to get your hands soiled

cos i am going to start vomiting topics on numpy in a minute

so setup your environments i will be expecting u have the jupyter notebook

if u dont have the jupyter i also expect whatever way u run your python script

you got numpy package installed

for those who installed python directly without the use of anaconda
pip install "numpy‑1.14.2+mkl‑cp36‑cp36m‑win32.whl"
2

go to ur cmd and type pip
Re: Data Science With Python Tutorial by lahp(m): 9:00pm On Feb 16, 2019
greatface:
Most of them were half completed.

I hope i don't die yet but if i get to live for 112 years i plan to be rest assured this tutorial will be completed
Re: Data Science With Python Tutorial by lahp(m): 9:27pm On Feb 16, 2019
NumPy Arrays:

NOTE: [IN]<- whenever u see this it shows the line of codes u will type into your notebook
[OUT] <- the expected result after u print out your code

Unlike python lists NumPy arrays is an array which must be of the same type(int64, float64, int32, float32, strings, booleans).
if the array is made of diffrent types by default it will upcast if possible

you can create a numpy array like this

[IN] x = np.array([1,2,3,4])

[IN] print (x)

[OUT] array([1, 2, 3, 4])

#this is an array of type int that will upcast to a floating point
[IN] x = np.array([3.147,2,3,4])

[IN] print(x)

[OUT] array([3.147, 2., 3., 4.])


we can explicitly set the datatype of an array using the dtype keyword



[IN] x = np.array([1,2,3,4], dtype = int64)



[OUT] array([1, 2, 3, 4, dtype = int64])



There are other ways to create NumPy arrays from scratch we shall see such ways in the next class

1 Like

Re: Data Science With Python Tutorial by greatface(m): 9:35pm On Feb 16, 2019
lahp:


I hope i don't die yet but if i get to live for 112 years i plan to be rest assured this tutorial will be completed
Thanks for your word of assurance. I hope to be there with you at the end.
Re: Data Science With Python Tutorial by lahp(m): 4:55pm On Feb 19, 2019
There are several ways to create arrays. For example, you can create an array from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences. A frequent error consists in calling array with multiple numeric arguments, rather than providing a single list of numbers as an argument. array transforms sequences of sequences into two- dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on. The type of the array can also be explicitly specified at creation time: Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64 . To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists. When arange is used with floating point arguments, it is generally not possible to predict the number of elements obtained, due to the finite floating point precision. For this reason, it is usually better to use the function linspace that receives as an argument the number of elements that we want, instead of the step: See also: array , zeros, zeros_like , ones , ones_like , empty , empty_like , arange, linspace, numpy.random.rand , numpy.random.randn , fromfunction, fromfile Printing Arrays When you print an array, NumPy displays it in a similar way to nested lists, but with the following layout: the last axis is printed from left to right, the second-to-last is printed from top to bottom, the rest are also printed from top to bottom, with each slice separated from the next by an empty line. One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices. See below to get more details on reshape . If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners: To disable this behaviour and force NumPy to print the entire array, you can change the printing options using set_printoptions . Basic Operations Arithmetic operators on arrays apply elementwise . A new array is created and filled with the result. Unlike in many matrix languages, the product operator * operates elementwise in NumPy arrays. The matrix product can be performed using the @ operator (in python >=3.5) or the dot function or method: Some operations, such as += and *=, act in place to modify an existing array rather than create a new one. When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting). Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the ndarray class. By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the axis parameter you can apply an operation along the specified axis of an array: Universal Functions NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called “universal functions”( ufunc ). Within NumPy, these functions operate elementwise on an array, producing an array as output. See also: all , any , apply_along_axis , argmax, argmin , argsort, average , bincount , ceil, clip , conj , corrcoef , cov , cross, cumprod , cumsum , diff, dot, floor, inner , inv, lexsort, max , maximum , mean , median , min, minimum, nonzero , outer , prod, re , round, sort , std, sum , trace, transpose , var, vdot , vectorize , where Indexing, Slicing and Iterating One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences. Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas: When fewer indices are provided than the number of axes, the missing indices are considered complete slices : The expression within brackets in b[i] is treated as an i followed by as many instances of : as needed to represent the remaining axes. NumPy also allows you to write this using dots as b[i,...] . The dots ( ... ) represent as many colons as needed to produce a complete indexing tuple. For example, if x is an array with 5 axes, then x[1,2,...] is equivalent to x[1,2,:,:,:] , x[...,3] to x[:,:,:,:,3] and x[4,...,5,:] to x[4,:,:,5,:] . Iterating over multidimensional arrays is done with respect to the first axis: However, if one wants to perform an operation on each element in the array, one can use the flat attribute which is an iterator over all the elements of the array: See also: Indexing , Indexing (reference), newaxis, ndenumerate , indices Shape Manipulation Changing the shape of an array An array has a shape given by the number of elements along each axis: The shape of an array can be changed with various commands. Note that the following three commands all return a modified array, but do not change the original array: The order of the elements in the array resulting from ravel () is normally “C-style”, that is, the rightmost index “changes the fastest”, so the element after a[0,0] is a [0,1]. If the array is reshaped to some other shape, again the array is treated as “C-style”. NumPy normally creates arrays stored in this order, so ravel() will usually not need to copy its argument, but if the array was made by taking slices of another array or created with unusual options, it may need to be copied. The functions ravel() and reshape () can also be instructed, using an optional argument, to use FORTRAN-style arrays, in which the leftmost index changes the fastest. The reshape function returns its argument with a modified shape, whereas the ndarray.resize method modifies the array itself: If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated: See also: ndarray.shape , reshape , resize , ravel Stacking together different arrays Several arrays can be stacked together along different axes: The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to hstack only for 2D arrays: On the other hand, the function row_stack is equivalent to vstack for any input arrays. In general, for arrays of with more than two dimensions, hstack stacks along their second axes, vstack stacks along their first axes, and concatenate allows for an optional arguments giving the number of the axis along which the concatenation should happen. Note In complex cases, r_ and c_ are useful for creating arrays by stacking numbers along one axis. They allow the use of range literals (“:”) When used with arrays as arguments, r_ and c_ are similar to vstack and hstack in their default behavior, but allow for an optional argument giving the number of the axis along which to concatenate. See also: hstack , vstack, column_stack, concatenate, c_ , r_ Splitting one array into several smaller ones Using hsplit, you can split an array along its horizontal axis, either by specifying the number of equally shaped arrays to return, or by specifying the columns after which the division should occur: vsplit splits along the vertical axis, and array_split allows one to specify along which axis to split. Copies and Views When operating and manipulating arrays, their data is sometimes copied into a new array and sometimes not. This is often a source of confusion for beginners. There are three cases: No Copy at All Simple assignments make no copy of array objects or of their data. Python passes mutable objects as references, so function calls make no copy. View or Shallow Copy Different array objects can share the same data. The view method creates a new array object that looks at the same data. Slicing an array returns a view of it: Deep Copy The copy method makes a complete copy of the array and its data. Functions and Methods Overview Here is a list of some useful NumPy functions and methods names ordered in categories. See Routines for the full list. Array Creation arange, array, copy , empty , empty_like , eye, fromfile , fromfunction , identity, linspace, logspace , mgrid , ogrid , ones , ones_like , r , zeros, zeros_like Conversions ndarray.astype , atleast_1d , atleast_2d , atleast_3d , mat Manipulations array_split , column_stack, concatenate , diagonal , dsplit, dstack , hsplit, hstack , ndarray.item , newaxis , ravel , repeat , reshape , resize , squeeze, swapaxes , take , transpose, vsplit , vstack Questions all , any , nonzero , where Ordering argmax, argmin , argsort , max , min, ptp , searchsorted , sort Operations choose , compress , cumprod , cumsum , inner , ndarray.fill , imag , prod, put , putmask , real, sum Basic Statistics cov , mean , std, var Basic Linear Algebra cross, dot , outer , linalg.svd , vdot Less Basic Broadcasting rules Broadcasting allows universal functions to deal in a meaningful way with inputs that do not have exactly the same shape. The first rule of broadcasting is that if all input arrays do not have the same number of dimensions, a “1” will be repeatedly prepended to the shapes of the smaller arrays until all the arrays have the same number of dimensions. The second rule of broadcasting ensures that arrays with a size of 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is assumed to be the same along that dimension for the “broadcast” array. After application of the broadcasting rules, the sizes of all arrays must match. More details can be found in Broadcasting. Fancy indexing and index tricks NumPy offers more indexing facilities than regular Python sequences. In addition to indexing by integers and slices, as we saw before, arrays can be indexed by arrays of integers and arrays of booleans. Indexing with Arrays of Indices When the indexed array a is multidimensional, a single array of indices refers to the first dimension of a. The following example shows this behavior by converting an image of labels into a color image using a palette. We can also give indexes for more than one dimension. The arrays of indices for each dimension must have the same shape. Naturally, we can put i and j in a sequence (say a list) and then do the indexing with the list. However, we can not do this by putting i and j into an array, because this array will be interpreted as indexing the first dimension of a. Another common use of indexing with arrays is the search of the maximum value of time-dependent series: You can also use indexing with arrays as a target to assign to: However, when the list of indices contains repetitions, the assignment is done several times, leaving behind the last value: This is reasonable enough, but watch out if you want to use Python’s += construct, as it may not do what you expect: Even though 0 occurs twice in the list of indices, the 0th element is only incremented once. This is because Python requires “a+=1” to be equivalent to “a = a + 1”. Indexing with Boolean Arrays When we index arrays with arrays of (integer) indices we are providing the list of indices to pick. With boolean indices the approach is different; we explicitly choose which items in the array we want and which ones we don’t. The most natural way one can think of for boolean indexing is to use boolean arrays that have the same shape as the original array: This property can be very useful in assignments: You can look at the following example to see how to use boolean indexing to generate an image of the Mandelbrot set : The second way of indexing with booleans is more similar to integer indexing; for each dimension of the array we give a 1D boolean array selecting the slices we want: Note that the length of the 1D boolean array must coincide with the length of the dimension (or axis) you want to slice. In the previous example, b1 has length 3 (the number of rows in a), and b2 (of length 4) is suitable to index the 2nd axis (columns) of a. The ix_() function The ix_ function can be used to combine different vectors so as to obtain the result for each n-uplet. For example, if you want to compute all the a+b*c for all the triplets taken from each of the vectors a, b and c: You could also implement the reduce as follows: and then use it as: The advantage of this version of reduce compared to the normal ufunc.reduce is that it makes use of the Broadcasting Rules in order to avoid creating an argument array the size of the output times the number of vectors. Indexing with strings See Structured arrays . Linear Algebra Work in progress. Basic linear algebra to be included here. Simple Array Operations See linalg.py in numpy folder for more. Tricks and Tips Here we give a list of short and useful tips. “Automatic” Reshaping To change the dimensions of an array, you can omit one of the sizes which will then be deduced automatically: Vector Stacking How do we construct a 2D array from a list of equally- sized row vectors? In MATLAB this is quite easy: if x and y are two vectors of the same length you only need do m=[x;y] . In NumPy this works via the functions column_stack , dstack , hstack and vstack , depending on the dimension in which the stacking is to be done. For example: The logic behind those functions in more than two dimensions can be strange. See also: NumPy for Matlab users Histograms The NumPy histogram function applied to an array returns a pair of vectors: the histogram of the array and the vector of bins. Beware: matplotlib also has a function to build histograms (called hist, as in Matlab) that differs from the one in NumPy. The main difference is that pylab.hist plots the histogram automatically, while numpy.histogram only generates the data. Further reading The Python tutorial NumPy Reference SciPy Tutorial SciPy Lecture Notes A matlab, R, IDL, NumPy/SciPy dictionary Table Of Contents Quickstart tutorial Prerequisites The Basics An example Array Creation Printing Arrays Basic Operations Universal Functions Indexing, Slicing and Iterating Shape Manipulation Changing the shape of an array Stacking together different arrays Splitting one array into several smaller ones Copies and Views No Copy at All View or Shallow Copy Deep Copy Functions and Methods Overview Less Basic Broadcasting rules Fancy indexing and index tricks Indexing with Arrays of Indices Indexing with Boolean Arrays The ix_() function Indexing with strings Linear Algebra Simple Array Operations Tricks and Tips “Automatic” Reshaping Vector Stacking Histograms Further reading Previous topic Installing NumPy Next topic NumPy basics Quick search search [[ 1. , 0., 0.], [ 0. , 1., 2.]] >>> import numpy as np >>> a = np .arange( 15 ).reshape( 3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) >>> a .shape (3, 5) >>> a .ndim 2 >>> a .dtype .name 'int64' >>> a .itemsize 8 >>> a .size 15 >>> type (a) <type 'numpy.ndarray'> >>> b = np .array([ 6, 7, 8 ]) >>> b array([6, 7, 8]) >>> type (b) <type 'numpy.ndarray'> >>> >>> import numpy as np >>> a = np .array([ 2, 3,4]) >>> a array([2, 3, 4]) >>> a .dtype dtype('int64') >>> b = np .array([ 1.2 , 3.5 , 5.1 ]) >>> b .dtype dtype('float64') >>> >>> a = np .array( 1,2 ,3,4) # WRONG >>> a = np .array([ 1, 2,3,4 ]) # RIGHT >>> >>> b = np .array([( 1.5 ,2, 3), ( 4,5,6 )]) >>> b array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]]) >>> >>> c = np .array( [ [ 1,2], [ 3, 4] ], dtype =complex ) >>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]]) >>> >>> np .zeros( ( 3,4) ) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> np .ones( ( 2 ,3,4), dtype =np.int16 ) # dtype can also be specified array([[[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]], [[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]]], dtype=int16) >>> np .empty ( (2, 3) ) # uninitialized, output may vary array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]]) >>> >>> np .arange( 10, 30 , 5 ) array([10, 15, 20, 25]) >>> np .arange( 0, 2, 0.3 ) # it accepts float arguments array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) >>> >>> from numpy import pi >>> np .linspace( 0 , 2, 9 ) # 9 numbers from 0 to 2 array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ]) >>> x = np .linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points >>> f = np .sin(x)
Re: Data Science With Python Tutorial by amadiwati(m): 7:56pm On Feb 28, 2019
God bless your effort sir
Re: Data Science With Python Tutorial by dauddy97(m): 9:42pm On Feb 28, 2019
I am in. Plz, try to finish this topic to the end, make it short in a summary way else, people would be tired of it. Also, try to drop examples and exercise to carry your students along. And lastly don't create any mumu whatsApp group, Group will die one day and people will end up advertising or sending inrelevant topics there. But from here, many will come and it will even help the future data scientists.
Re: Data Science With Python Tutorial by olaide07(f): 1:05am On Mar 02, 2019
I am so in.God bless you for this.
lahp:
There are several ways to create arrays.
For example, you can create an array from a regular
Python list or tuple using the array function. The type of
the resulting array is deduced from the type of the
elements in the sequences.
A frequent error consists in calling array with multiple
numeric arguments, rather than providing a single list of
numbers as an argument.
array transforms sequences of sequences into two-
dimensional arrays, sequences of sequences of sequences
into three-dimensional arrays, and so on.
The type of the array can also be explicitly specified at
creation time:
Often, the elements of an array are originally unknown, but
its size is known. Hence, NumPy offers several functions
to create arrays with initial placeholder content. These
minimize the necessity of growing arrays, an expensive
operation.
The function zeros creates an array full of zeros, the
function ones creates an array full of ones, and the
function empty creates an array whose initial content is
random and depends on the state of the memory. By
default, the dtype of the created array is float64 .
To create sequences of numbers, NumPy provides a
function analogous to range that returns arrays instead of
lists.
When arange is used with floating point arguments, it is
generally not possible to predict the number of elements
obtained, due to the finite floating point precision. For this
reason, it is usually better to use the function linspace
that receives as an argument the number of elements that
we want, instead of the step:
See also:
array , zeros, zeros_like , ones , ones_like , empty ,
empty_like , arange, linspace, numpy.random.rand ,
numpy.random.randn , fromfunction, fromfile
Printing Arrays
When you print an array, NumPy displays it in a similar
way to nested lists, but with the following layout:
the last axis is printed from left to right,
the second-to-last is printed from top to bottom,
the rest are also printed from top to bottom, with each
slice separated from the next by an empty line.
One-dimensional arrays are then printed as rows,
bidimensionals as matrices and tridimensionals as lists of
matrices.
See below to get more details on reshape .
If an array is too large to be printed, NumPy automatically
skips the central part of the array and only prints the
corners:
To disable this behaviour and force NumPy to print the
entire array, you can change the printing options using
set_printoptions .
Basic Operations
Arithmetic operators on arrays apply elementwise . A new
array is created and filled with the result.
Unlike in many matrix languages, the product operator *
operates elementwise in NumPy arrays. The matrix
product can be performed using the @ operator (in python
>=3.5) or the dot function or method:
Some operations, such as += and *=, act in place to
modify an existing array rather than create a new one.
When operating with arrays of different types, the type of
the resulting array corresponds to the more general or
precise one (a behavior known as upcasting).
Many unary operations, such as computing the sum of all
the elements in the array, are implemented as methods of
the ndarray class.
By default, these operations apply to the array as though it
were a list of numbers, regardless of its shape. However,
by specifying the axis parameter you can apply an
operation along the specified axis of an array:
Universal Functions
NumPy provides familiar mathematical functions such as
sin, cos, and exp. In NumPy, these are called “universal
functions”( ufunc ). Within NumPy, these functions operate
elementwise on an array, producing an array as output.
See also:
all , any , apply_along_axis , argmax, argmin , argsort,
average , bincount , ceil, clip , conj , corrcoef , cov , cross,
cumprod , cumsum , diff, dot, floor, inner , inv, lexsort, max ,
maximum , mean , median , min, minimum, nonzero , outer ,
prod, re , round, sort , std, sum , trace, transpose , var, vdot ,
vectorize , where
Indexing, Slicing and Iterating
One-dimensional arrays can be indexed, sliced and
iterated over, much like lists and other Python sequences.
Multidimensional arrays can have one index per axis.
These indices are given in a tuple separated by commas:
When fewer indices are provided than the number of axes,
the missing indices are considered complete slices :
The expression within brackets in b[i] is treated as an i
followed by as many instances of : as needed to
represent the remaining axes. NumPy also allows you to
write this using dots as b[i,...] .
The dots ( ... ) represent as many colons as needed to
produce a complete indexing tuple. For example, if x is
an array with 5 axes, then
x[1,2,...] is equivalent to x[1,2,:,:,:] ,
x[...,3] to x[:,:,:,:,3] and
x[4,...,5,:] to x[4,:,:,5,:] .
Iterating over multidimensional arrays is done with respect
to the first axis:
However, if one wants to perform an operation on each
element in the array, one can use the flat attribute which
is an iterator over all the elements of the array:
See also:
Indexing , Indexing (reference), newaxis, ndenumerate ,
indices
Shape Manipulation
Changing the shape of an array
An array has a shape given by the number of elements
along each axis:
The shape of an array can be changed with various
commands. Note that the following three commands all
return a modified array, but do not change the original
array:
The order of the elements in the array resulting from ravel
() is normally “C-style”, that is, the rightmost index
“changes the fastest”, so the element after a[0,0] is a
[0,1]. If the array is reshaped to some other shape, again
the array is treated as “C-style”. NumPy normally creates
arrays stored in this order, so ravel() will usually not need
to copy its argument, but if the array was made by taking
slices of another array or created with unusual options, it
may need to be copied. The functions ravel() and reshape
() can also be instructed, using an optional argument, to
use FORTRAN-style arrays, in which the leftmost index
changes the fastest.
The reshape function returns its argument with a modified
shape, whereas the ndarray.resize method modifies the
array itself:
If a dimension is given as -1 in a reshaping operation, the
other dimensions are automatically calculated:
See also:
ndarray.shape , reshape , resize , ravel
Stacking together different arrays
Several arrays can be stacked together along different
axes:
The function column_stack stacks 1D arrays as columns
into a 2D array. It is equivalent to hstack only for 2D
arrays:
On the other hand, the function row_stack is equivalent to
vstack for any input arrays. In general, for arrays of with
more than two dimensions, hstack stacks along their
second axes, vstack stacks along their first axes, and
concatenate allows for an optional arguments giving the
number of the axis along which the concatenation should
happen.
Note
In complex cases, r_ and c_ are useful for creating arrays
by stacking numbers along one axis. They allow the use
of range literals (“:”)
When used with arrays as arguments, r_ and c_ are similar
to vstack and hstack in their default behavior, but allow
for an optional argument giving the number of the axis
along which to concatenate.
See also:
hstack , vstack, column_stack, concatenate, c_ , r_
Splitting one array into several smaller ones
Using hsplit, you can split an array along its horizontal
axis, either by specifying the number of equally shaped
arrays to return, or by specifying the columns after which
the division should occur:
vsplit splits along the vertical axis, and array_split allows
one to specify along which axis to split.
Copies and Views
When operating and manipulating arrays, their data is
sometimes copied into a new array and sometimes not.
This is often a source of confusion for beginners. There
are three cases:
No Copy at All
Simple assignments make no copy of array objects or of
their data.
Python passes mutable objects as references, so function
calls make no copy.
View or Shallow Copy
Different array objects can share the same data. The view
method creates a new array object that looks at the same
data.
Slicing an array returns a view of it:
Deep Copy
The copy method makes a complete copy of the array
and its data.
Functions and Methods Overview
Here is a list of some useful NumPy functions and
methods names ordered in categories. See Routines for
the full list.
Array Creation
arange, array, copy , empty , empty_like , eye, fromfile ,
fromfunction , identity, linspace, logspace , mgrid , ogrid ,
ones , ones_like , r , zeros, zeros_like
Conversions
ndarray.astype , atleast_1d , atleast_2d , atleast_3d , mat
Manipulations
array_split , column_stack, concatenate , diagonal , dsplit,
dstack , hsplit, hstack , ndarray.item , newaxis , ravel , repeat
, reshape , resize , squeeze, swapaxes , take , transpose,
vsplit , vstack
Questions
all , any , nonzero , where
Ordering
argmax, argmin , argsort , max , min, ptp , searchsorted , sort
Operations
choose , compress , cumprod , cumsum , inner , ndarray.fill ,
imag , prod, put , putmask , real, sum
Basic Statistics
cov , mean , std, var
Basic Linear Algebra
cross, dot , outer , linalg.svd , vdot
Less Basic
Broadcasting rules
Broadcasting allows universal functions to deal in a
meaningful way with inputs that do not have exactly the
same shape.
The first rule of broadcasting is that if all input arrays do
not have the same number of dimensions, a “1” will be
repeatedly prepended to the shapes of the smaller arrays
until all the arrays have the same number of dimensions.
The second rule of broadcasting ensures that arrays with a
size of 1 along a particular dimension act as if they had
the size of the array with the largest shape along that
dimension. The value of the array element is assumed to
be the same along that dimension for the “broadcast”
array.
After application of the broadcasting rules, the sizes of all
arrays must match. More details can be found in
Broadcasting.
Fancy indexing and index tricks
NumPy offers more indexing facilities than regular Python
sequences. In addition to indexing by integers and slices,
as we saw before, arrays can be indexed by arrays of
integers and arrays of booleans.
Indexing with Arrays of Indices
When the indexed array a is multidimensional, a single
array of indices refers to the first dimension of a. The
following example shows this behavior by converting an
image of labels into a color image using a palette.
We can also give indexes for more than one dimension.
The arrays of indices for each dimension must have the
same shape.
Naturally, we can put i and j in a sequence (say a list)
and then do the indexing with the list.
However, we can not do this by putting i and j into an
array, because this array will be interpreted as indexing the
first dimension of a.
Another common use of indexing with arrays is the search
of the maximum value of time-dependent series:
You can also use indexing with arrays as a target to assign
to:
However, when the list of indices contains repetitions, the
assignment is done several times, leaving behind the last
value:
This is reasonable enough, but watch out if you want to
use Python’s += construct, as it may not do what you
expect:
Even though 0 occurs twice in the list of indices, the 0th
element is only incremented once. This is because Python
requires “a+=1” to be equivalent to “a = a + 1”.
Indexing with Boolean Arrays
When we index arrays with arrays of (integer) indices we
are providing the list of indices to pick. With boolean
indices the approach is different; we explicitly choose
which items in the array we want and which ones we don’t.
The most natural way one can think of for boolean
indexing is to use boolean arrays that have the same
shape as the original array:
This property can be very useful in assignments:
You can look at the following example to see how to use
boolean indexing to generate an image of the Mandelbrot
set :
The second way of indexing with booleans is more similar
to integer indexing; for each dimension of the array we
give a 1D boolean array selecting the slices we want:
Note that the length of the 1D boolean array must coincide
with the length of the dimension (or axis) you want to
slice. In the previous example, b1 has length 3 (the
number of rows in a), and b2 (of length 4) is suitable to
index the 2nd axis (columns) of a.
The ix_() function
The ix_ function can be used to combine different vectors
so as to obtain the result for each n-uplet. For example, if
you want to compute all the a+b*c for all the triplets taken
from each of the vectors a, b and c:
You could also implement the reduce as follows:
and then use it as:
The advantage of this version of reduce compared to the
normal ufunc.reduce is that it makes use of the
Broadcasting Rules in order to avoid creating an argument
array the size of the output times the number of vectors.
Indexing with strings
See Structured arrays .
Linear Algebra
Work in progress. Basic linear algebra to be included here.
Simple Array Operations
See linalg.py in numpy folder for more.
Tricks and Tips
Here we give a list of short and useful tips.
“Automatic” Reshaping
To change the dimensions of an array, you can omit one
of the sizes which will then be deduced automatically:
Vector Stacking
How do we construct a 2D array from a list of equally-
sized row vectors? In MATLAB this is quite easy: if x and
y are two vectors of the same length you only need do
m=[x;y] . In NumPy this works via the functions
column_stack , dstack , hstack and vstack , depending
on the dimension in which the stacking is to be done. For
example:
The logic behind those functions in more than two
dimensions can be strange.
See also:
NumPy for Matlab users
Histograms
The NumPy histogram function applied to an array
returns a pair of vectors: the histogram of the array and
the vector of bins. Beware: matplotlib also has a
function to build histograms (called hist, as in Matlab)
that differs from the one in NumPy. The main difference is
that pylab.hist plots the histogram automatically, while
numpy.histogram only generates the data.
Further reading
The Python tutorial
NumPy Reference
SciPy Tutorial
SciPy Lecture Notes
A matlab, R, IDL, NumPy/SciPy dictionary
Table Of Contents
Quickstart tutorial
Prerequisites
The Basics
An example
Array Creation
Printing Arrays
Basic Operations
Universal Functions
Indexing, Slicing and Iterating
Shape Manipulation
Changing the shape of an array
Stacking together different arrays
Splitting one array into several smaller ones
Copies and Views
No Copy at All
View or Shallow Copy
Deep Copy
Functions and Methods Overview
Less Basic
Broadcasting rules
Fancy indexing and index tricks
Indexing with Arrays of Indices
Indexing with Boolean Arrays
The ix_() function
Indexing with strings
Linear Algebra
Simple Array Operations
Tricks and Tips
“Automatic” Reshaping
Vector Stacking
Histograms
Further reading
Previous topic
Installing NumPy
Next topic
NumPy basics
Quick search
search
[[ 1. , 0., 0.],
[ 0. , 1., 2.]]
>>> import numpy as np
>>> a = np .arange( 15 ).reshape( 3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> a .shape
(3, 5)
>>> a .ndim
2
>>> a .dtype .name
'int64'
>>> a .itemsize
8
>>> a .size
15
>>> type (a)
<type 'numpy.ndarray'>
>>> b = np .array([ 6, 7, 8 ])
>>> b
array([6, 7, 8])
>>> type (b)
<type 'numpy.ndarray'>
>>>
>>> import numpy as np
>>> a = np .array([ 2, 3,4])
>>> a
array([2, 3, 4])
>>> a .dtype
dtype('int64')
>>> b = np .array([ 1.2 , 3.5 , 5.1 ])
>>> b .dtype
dtype('float64')
>>>
>>> a = np .array( 1,2 ,3,4) # WRONG
>>> a = np .array([ 1, 2,3,4 ]) # RIGHT
>>>
>>> b = np .array([( 1.5 ,2, 3), ( 4,5,6 )])
>>> b
array([[ 1.5, 2. , 3. ],
[ 4. , 5. , 6. ]])
>>>
>>> c = np .array( [ [ 1,2], [ 3, 4] ],
dtype =complex )
>>> c
array([[ 1.+0.j, 2.+0.j],
[ 3.+0.j, 4.+0.j]])
>>>
>>> np .zeros( ( 3,4) )
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
>>> np .ones( ( 2 ,3,4),
dtype =np.int16 ) # dtype can
also be specified
array([[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]],
[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]]], dtype=int16)
>>> np .empty
( (2, 3) ) #
uninitialized, output may vary
array([[ 3.73603959e-262,
6.02658058e-154, 6.55490914e-260],
[ 5.30498948e-313,
3.14673309e-307, 1.00000000e+000]])
>>>
>>> np .arange( 10, 30 , 5 )
array([10, 15, 20, 25])
>>> np .arange( 0, 2, 0.3 ) #
it accepts float arguments
array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5,
1.8])
>>>
>>> from numpy import pi
>>> np .linspace( 0 , 2, 9 ) #
9 numbers from 0 to 2
array([ 0. , 0.25, 0.5 , 0.75, 1. ,
1.25, 1.5 , 1.75, 2. ])
>>> x = np .linspace( 0, 2*pi, 100 ) #
useful to evaluate function at lots of points
>>> f = np .sin(x)
Re: Data Science With Python Tutorial by lahp(m): 9:42pm On Mar 11, 2019
Hi all good evening

we have learnt the basics of creating a numpy array using : numpy.array[[1, 2,4,5]] after importing the numpy package

in this class we shall see how we can do basic computing on numpy arrays and we shall focus of universal functions
Re: Data Science With Python Tutorial by lahp(m): 10:03pm On Mar 11, 2019
ufuncs which i shall use as an alias for universal functions exist in two flavours : unary ufuncs which operates in a single input and binary ufuncs which operates in double outputs we shall talk about these two flavours

Array arithmetics:
ufuncs should feel natural to use if u are well absorbed with pythons native arithmetics
the standard addition subtraction and multipliaction can be used

import numpy as np

x = np.arange(4)
print('x = ' x )
print('x + 5 = ' x + 5 )

[out] x = [0, 1, 2, 3]
[out] x + 5 = [5, 6, 7, 8]

we also have operators for exponentiation and modulus which are : a** , a%

u can add this arithmetic operation together if u choose


Operator Equivalent ufunc Description
+ np.add Addition (e.g., 1 + 1 = 2)
- np.subtract Subtraction (e.g., 3 - 2 = 1)
- np.negative Unary negation (e.g., -2)
* np.multiply Multiplication (e.g., 2 * 3 = 6)
/ np.divide Division (e.g., 3 / 2 = 1.5)
// np.floor_ divide Floor division (e.g., 3 // 2 = 1)
** np.power Exponentiation (e.g., 2 ** 3 = cool
% np.mod Modulus/remainder (e.g., 9 % 4 = 1)

the above is a table of arithmetic operators implemented in numpy

(1) (2) (Reply)

How Can I Deploy Applications Create In Vb.net / Html & Css / Java Guru Help

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 120
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.