import numpy as np
data= [ 1 , 2 , 3 , 4 , 5 ]
n= np. array( data* 10 )
print ( n)
print ( data)
n. shape
n. dtype
arr= [ [ 1 , 2 , 3 , 4 , 5 ] , [ 1 , 3 , 45 , 6 , 7 ] ]
arr2= np. array( arr)
print ( arr2)
print ( arr2. ndim)
print ( arr2. shape)
arr= [ [ '1' , 2 , 3 , 4 ] , [ 1 , 2 , 3 , 4 ] ]
arr2= np. array( arr)
print ( arr2)
print ( arr2. dtype)
arr= [ [ 1.1 , 2 , 3 , 4 ] , [ 1 , 2 , 3 , 4 ] ]
arr2= np. array( arr)
print ( arr2)
print ( arr2. dtype)
np. zeros( 10 )
np. ones( ( 2 , 3 ) )
np. empty( ( 2 , 3 , 4 ) )
np. arange( 10 )
arr= np. array( [ 1 , 2 , 3.3 , - 4.3 , - 100.9 ] )
print ( arr. astype( np. int32) )
arr1= np. array( [ 1 , 2 , 3 , 4 ] )
arr2= np. array( [ 1 , 2 , 3 , 4 ] )
arr1+ arr2
arr3= np. array( [ [ 1 , 2 , 3 , 4 ] , [ 1 , 3 , 6 , 7 ] ] )
arr4= np. array( [ [ 1 , 2 , 3 , 4 ] , [ 1 , 3 , 6 , 7 ] ] )
arr3+ arr4
5 * arr3
5 / arr3
arr= np. arange( 10 )
print ( arr)
print ( arr[ 1 ] )
print ( arr[ 4 : ] )
arr[ 0 : 4 ] = 11
arr_copy= arr. copy( )
print ( arr_copy)
arr1= np. array( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] )
print ( arr1[ 0 ] [ 1 ] )
print ( arr1[ 0 , 1 ] )
names= np. array( [ 'Tony' , 'Jack' , 'Robin' ] )
print ( names== "Tony" )
print ( ( names== 'Tony' ) & ( names== 'Robin' ) )
arr= np. empty( ( 8 , 4 ) )
for i in range ( 8 ) :
arr[ i] = i
print ( arr)
print ( arr[ [ 4 , 3 , 0 , 6 ] ] )
arr1= np. arange( 32 ) . reshape( ( 8 , 4 ) )
arr= np. arange( 15 ) . reshape( ( 3 , 5 ) )
print ( arr)
print ( arr. transpose( ) )
print ( arr. T)
arr= np. arange( 24 ) . reshape( ( 2 , 3 , 4 ) )
print ( arr. transpose( ( 1 , 2 , 0 ) ) )
x_array= np. array( [ 1 , 2 , 3 , 4 , 5 ] )
y_array= np. array( [ 11 , 22 , 33 , 44 , 55 ] )
condition= np. array( [ True , True , False , True , False ] )
result= [ ( x if c else y) for x, y, c in zip ( x_array, y_array, condition) ]
print ( result)
r= np. where( condition, x_array, y_array)
print ( r)
arr= np. random. randn( 1 , 4 )
print ( arr)
arr1= np. where( arr> 0 , 2 , - 2 )
print ( arr1)
arr= np. random. randn( 4 , 4 )
arr. mean( )
print ( np. mean( arr) )
print ( arr. sum ( ) )
print ( arr. std( ) )
print ( arr. mean( axis= ( ) ) )
print ( arr. sum ( 0 ) )
arr= np. random. randn( 4 )
arr. sort( )
arr= np. random. randn( 4 , 4 )
arr. sort( 1 )
arr= np. arange( 10 )
print ( arr)
np. save( 'arr_array' , arr)
np. load( "arr_array.npy" )
np. savez( 'array_1' , a= arr)
np. load( 'array_1.npz' ) [ 'a' ]
np. savetxt( "array_txt.txt" , arr, delimiter= "," )
np. loadtxt( "array_txt.txt" , delimiter= "," )
x= np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
y= np. array( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] )
print ( x. dot( y) )
import os
import matplotlib. pyplot as plt
position= 0
walk= [ position]
steps= 1000
for i in range ( steps) :
steps= 1 if np. random. randint( 0 , 2 ) else - 1
position+= steps
walk. append( position)
plt. plot( walk)
print ( ( np. abs ( walk) > 10 ) . argmax( ) )