declare

Arrays in Python / Numpy



Sharing buttons:

we're going to be talking about arrays

in Python to do some numerical analysis

so we're going to talk about some

built-in arrays to Python and then also

numpy arrays so we're going to cover

arrays two types the first one is going

to be Python arrays and then also numpy

arrays how to define those how to work

with them in in Python so if we think

about a raise

you could have a vector or one

dimensional array so for example let's

say I just had some values okay and

these are going to be a collection of

numbers instead of an individual number

and then I might reference it for

example let's say I want to get the

second element of that I would do x1 and

that would equal this value right here

okay so I could also have a

multi-dimensional arrays as well let's

say I had a matrix M and it had

individual values at all of these rows

and in these columns and then I could

pick out a value and work with it in

different ways with this matrix so what

we'll do now is go ahead and just open

up Python will run okay so I'll just do

the ideally GUI and once that comes up

then we'll start working with some

sample arrays in in Python okay and then

let me go ahead and just create a new

file this is where I can put some of the

commands to create a script

okay so I'll have my script over here to

the side and then my command window over

here to the other side when I run it

I'll be able to see the results there

okay so save this first of all just for

syntax highlighting purposes to create a

new Python file I'll just call it my

script dot py okay and then when I run

it I'll see the results on the right

side okay so let's first of all just

create a new ray and I'll just say that

X equals 1 to 3 point 5 10 and 20 ok so

this is going to be a collection of

numbers defined as an array if I just

print X that's going to then show up

here as my array if I want to access a

certain element of that I can do this

from the command prompt or over here on

the script side if I access X 2 that's

going to be equal to 3 point 5 ok so

that's the middle one if I want to

access the last element of this I do X

negative 1 and that's going to be 20 if

I only do the second-to-last one

negative two okay so I can I can either

get it from the beginning or the end

part of it as well

also you can deal with ranges as well

okay so 0 to 2 it does not include this

last element there it'll just include 1

less than that so that would be 1 2 2 1

& 2 just the 0 & 1 elements of that

array okay so this is these are the

built in ray arrays - you know - Python

but a lot of times what we want to do is

we want to do some analysis on I find

that numpy is a lot easier to use then

the built-in arrays so I'm going to

import numpy as NP okay and then what I

want to do is define a new array and

I'll say NP dot now in this case I'll do

array and then have my parentheses and

within that I'll define my new array

okay so I'll just do the same one that I

did before okay so just go and copy that

and inside these parentheses go ahead

and put your

and then I'll print why okay so that's

going to be printing X and then Y after

it loads in the the numpy you can see it

treats all those numbers as

floating-point

verses Python the built-in arrays

treated those some of them as integers

and the middle one as a floating point

number okay so I can do things certain

types of analysis with this now that I

have this array Y that's in numpy so for

example I can do n P dot mean you know

take the mean value of this if I have a

multi-dimensional the second argument is

going to be which axis I choose to take

the mean over in this case I just have

you know one dimension for this array so

I'm just going to take the mean of Y

okay so it just took the average value I

can also do the max value for example or

the min value okay so those are all you

know operations that can do on this

numpy array so let's say I had let's say

I want to plot these values okay so

let's say I have Y and then I also have

T so T might be my time and let's say so

there's a couple different operations I

want to show you here you can define a

new array with the empty function okay

so the shape let's say that's going to

be five that'll just be it isn't going

to clear the memory is going to be

whatever the memory is they're just

going to say I have five elements to it

and it can be very large numbers very

small numbers if I print T for example

okay so let me print T and and then I'll

print Y after that okay so when you

print it you can see that it just came

out with you know just whatever bits

were there in the computer it didn't try

to initialize that at

anyway you can also initialize them with

values of 1 for example just with the

ones ok and there you can see all of

those ones you can do zeros as well okay

so okay so zeros ones another common one

is linspace okay so we're going to have

a linearly spaced values let's see

between zero this is going to be our

starting value and then the stopping

value might be I'll say 1 so it's going

to go between 0 and 1 and I want to have

5 elements to that okay so let's go

ahead and run this one ok so you can see

those five elements so I have two arrays

that are the same size now I might want

to plot those so I'll go ahead and just

import that plot lib pie plot and i'll

import that as PLT and then let me go

ahead and just plot t and y and then i

will show that as well ok so I'm just

going to show this data now this is an

example where you use these vectors

these arrays these fundamental arrays or

vectors to be able to plot some data and

I'll just make those as red circles okay

and then run it again and then it's

going to open up this plot though where

I can see the data so these five points

one down here and then it goes up

there's my last point okay so linearly

space between 0 and 1 and these are just

some numbers that I put in for the Y

vector okay one another thing to know is

that the Python arrays are just a little

bit more flexible I'm going to put

instead of three point five here I'm

going to put in car okay so one two car

ten and 20 and then if I print that I

have some integers and then also

okay also this car element that you can

see down there so one thing I can do is

also access the individual element of

that array so if I type in something

like X let's see that's going to be two

okay that's going to be car but if I

access X two and maybe the very first

element of that that's going to be C if

I want to take just a piece of that

string okay let's say I want to just get

see a there is a CA with the range the

range there okay I can also say let's

see I can also do things like X of 2 to

the end of it okay so there it takes car

10 and 20 okay and then so there's

different ways to slice it slice these

arrays just take different parts of them

but one final thing I wanted to cover in

here is it's really important with

dealing with arrays data types if you

have a string in there you can add a

number and a string together so let's

say I have a is going to be equal to 7

as a string B is going to equal 3 as an

integer and then C equals let's say a

floating-point number so python is

dynamically typed so I can change you

know even though C is a floating point I

can change that to a string and back and

forth so if I try to add a and B

together it's going to give me an error

because I can't says I can't add a

string and an integer object together so

what I need to do is I can either do

you'll convert a into an integer okay or

a float value plus B okay and then I get

the value of 10

but if

I do if I convert be instead to a string

then I'm going to get seven three which

is like putting two elements of the

sentence together concatenating them

okay so that's just one other thing to

know if you do have an array such as

this up above where it's 1 2 car 10 20

you got to be careful about adding or

you know some operations that you do on

those arrays okay so that that concludes

some of this information on arrays we

can create them we can manipulate them

deuced

you know basic statistics and also be

able to plot them as well