In this blog post, we would take a look at NumPy ecosystem and see how to compute with the NumPy arrays.
NumPy is a python package that provides multidimensional arrays, tables, and matrices for python. Data is either stored in contiguous or straightened arrays and the types are always homogeneous. They can be consisting of integers, numbers, character data, arrays of records, or nested records.
Green boxes are python's core scientific packages, built on top of NumPy.
We would run a couple of commands involving NumPy arrays to get a feel of operation that can be done on NumPy arrays.
a) NumPy Arrays:
Suppose I declare a list, sales, as:
>> Sales = [1.0, 2.0, 3.5, 4.5]
Now if we want to double the individual entries inside the list, and for that if we can write a command like:
>> print sales*2
it won't double the individual values inside the list, but rather would double the size of the list itself, with the individual array values being repeated, as show below in command 5.
So for doubling the individual values in such a list, we would need to run a command as below:
>> print [2*s for s in sales]
But if we convert the list to a NumPy array, these operations can be done very easily and intuitively as shown below:
- First we need to import the NumPy array as:
>> import numpy as np
- Then we convert the list, sales, into a NumPy array as
>> sales = np.array(sales)
>> print sales
- To double the individual values inside the array:
>> print sales * 2
- To add a number to all the elements in the array:
>> print sales + 1
- To square all the individual elements in the array:
>> print np.power(sales, 2)
All NumPy arrays are single element data type and the data type can be seen using the dtype parameter.
We would talk about some more of operations with NumPy arrays as below:
- To create an array of size 5 of all zeros
>> np.zeros(5)
- To create an array of size 5 of all ones
>> np.ones(5)
- To multiply all elements of the ones array by 42
>> np.ones(5) * 42
- To create an array of random distribution
>> np.random.random(5)
- To create an array of diagonal matrix of 4
>> np.diag([1, 1, 1, 1])
- To create an array of 0-10
>> np.arrange(0, 10)
- To create an array of numbers between 10 and 30 (10 included), with a step of 2
>> np.arange(10,30,2)
- If we want to create a linear sample of a space, we use a linspace. The following command creates 100 sample points between 0 and 1 in a linear space.
>> np.linspace(0,1,100)
- To find the data type of the array
>> x.dtype
- To specify the data type while creating the array:
>> x = np.ones(5, dtype='int64')
- Define the shape of the array while we construct it:
>> np.ones(shape=(3,3), dtype='float64')
- To create a random, uniformly sampled array from 0 to 1, of size 5x5.
>> np.random.uniform(0,1,size=(5,5))
- To reshape a single row array in to a multidimensional one or vice versa, we would use the reshape command as:
>> data = np.arange(9)
>> data = data.reshape((3,3))
Note that the outer parenthesis is defining the function argument and the inner parenthesis is defining the tuple, (3,3) in this case.
- To access and change the first column:
>> data[0,0] = 12
>> data[1,0] = 13
>> data[2,0] = 14
- Selecting the complete first row
>> print data[0,:]
- Selecting the complete first column
>> print data[:,0]
- Assigning a whole column:
>> data[:,0] = 0
In the next post, we would be working with some real life data to show how to use these constructs in analyzing/modifying a real-life data set.
NumPy is a python package that provides multidimensional arrays, tables, and matrices for python. Data is either stored in contiguous or straightened arrays and the types are always homogeneous. They can be consisting of integers, numbers, character data, arrays of records, or nested records.
Green boxes are python's core scientific packages, built on top of NumPy.
We would run a couple of commands involving NumPy arrays to get a feel of operation that can be done on NumPy arrays.
a) NumPy Arrays:
Suppose I declare a list, sales, as:
>> Sales = [1.0, 2.0, 3.5, 4.5]
Now if we want to double the individual entries inside the list, and for that if we can write a command like:
>> print sales*2
it won't double the individual values inside the list, but rather would double the size of the list itself, with the individual array values being repeated, as show below in command 5.
So for doubling the individual values in such a list, we would need to run a command as below:
>> print [2*s for s in sales]
But if we convert the list to a NumPy array, these operations can be done very easily and intuitively as shown below:
- First we need to import the NumPy array as:
>> import numpy as np
- Then we convert the list, sales, into a NumPy array as
>> sales = np.array(sales)
>> print sales
- To double the individual values inside the array:
>> print sales * 2
- To add a number to all the elements in the array:
>> print sales + 1
- To square all the individual elements in the array:
>> print np.power(sales, 2)
All NumPy arrays are single element data type and the data type can be seen using the dtype parameter.
We would talk about some more of operations with NumPy arrays as below:
- To create an array of size 5 of all zeros
>> np.zeros(5)
- To create an array of size 5 of all ones
>> np.ones(5)
- To multiply all elements of the ones array by 42
>> np.ones(5) * 42
- To create an array of random distribution
>> np.random.random(5)
- To create an array of diagonal matrix of 4
>> np.diag([1, 1, 1, 1])
- To create an array of 0-10
>> np.arrange(0, 10)
- To create an array of numbers between 10 and 30 (10 included), with a step of 2
>> np.arange(10,30,2)
- If we want to create a linear sample of a space, we use a linspace. The following command creates 100 sample points between 0 and 1 in a linear space.
>> np.linspace(0,1,100)
- To find the data type of the array
>> x.dtype
- To specify the data type while creating the array:
>> x = np.ones(5, dtype='int64')
- Define the shape of the array while we construct it:
>> np.ones(shape=(3,3), dtype='float64')
- To create a random, uniformly sampled array from 0 to 1, of size 5x5.
>> np.random.uniform(0,1,size=(5,5))
- To reshape a single row array in to a multidimensional one or vice versa, we would use the reshape command as:
>> data = np.arange(9)
>> data = data.reshape((3,3))
Note that the outer parenthesis is defining the function argument and the inner parenthesis is defining the tuple, (3,3) in this case.
- To access and change the first column:
>> data[0,0] = 12
>> data[1,0] = 13
>> data[2,0] = 14
- Selecting the complete first row
>> print data[0,:]
- Selecting the complete first column
>> print data[:,0]
- Assigning a whole column:
>> data[:,0] = 0
In the next post, we would be working with some real life data to show how to use these constructs in analyzing/modifying a real-life data set.
No comments:
Post a Comment