import numpy
my_test = numpy.genfromtxt("mytest.csv", delimiter=",")
print(type(my_test))
<class 'numpy.ndarray'>
vector = numpy.array([1, 2, 3, 4])
print(vector)
matrix = numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(matrix)
[1 2 3 4]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
print(vector.shape)
(4,)
print(matrix.shape)
(3, 4)
print(my_test)
[[ nan nan nan nan
nan]
[ 1.98600000e+03 nan nan nan
0.00000000e+00]
[ 1.98600000e+03 nan nan nan
5.00000000e-01]
...,
[ 1.98700000e+03 nan nan nan
7.50000000e-01]
[ 1.98900000e+03 nan nan nan
1.50000000e+00]
[ 1.98500000e+03 nan nan nan
3.10000000e-01]]
my_test2 = numpy.genfromtxt("mytest.csv", delimiter=",", dtype="U75", skip_header=1)
print(my_test2)
[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
['1986' 'Americas' 'Uruguay' 'Other' '0.5']
['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62']
...,
['1987' 'Africa' 'Malawi' 'Other' '0.75']
['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
print(my_test2[1,1])
Americas
matrix = numpy.array([
[5, 10, 15, 20],
[20, 25, 30, 35],
[35, 40, 45, 50]
])
print(matrix[1:3,0:3])
[[20 25 30]
[35 40 45]]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix == 25
array([[False, False, False],
[False, True, False],
[False, False, False]], dtype=bool)
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
second_column_25 = (matrix[:,1] == 25)
print(second_column_25)
print(matrix[second_column_25, :])
[False True False]
[[20 25 30]]
vector = numpy.array([5, 10, 15, 20])
vector.sum()
50
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=0
)
array([60, 75, 90])
#replace nan value with 0
world_alcohol = numpy.genfromtxt("mytest.csv", delimiter=",")
#print world_alcohol
is_value_empty = numpy.isnan(world_alcohol[:,4])
#print is_value_empty
world_alcohol[is_value_empty, 4] = '0'
alcohol_consumption = world_alcohol[:,4]
alcohol_consumption = alcohol_consumption.astype(float)
total_alcohol = alcohol_consumption.sum()
average_alcohol = alcohol_consumption.mean()
print(total_alcohol)
print(average_alcohol)
1137.78
1.14006012024
import numpy as np
a = np.arange(16)
a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
a.reshape(4,4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
a.shape
(16,)
a.dtype
dtype('int64')
a.dtype.name
'int64'
np.ones( (2,3,4), dtype=np.int32 )
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=int32)
np.arange( 10, 30, 5 )
array([10, 15, 20, 25])
np.random.random((3,2))
array([[ 0.12534788, 0.3088895 ],
[ 0.88039175, 0.99165413],
[ 0.30894224, 0.07571642]])
from numpy import pi
np.linspace( 0, 2*pi, 100 )
array([ 0. , 0.06346652, 0.12693304, 0.19039955, 0.25386607,
0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866,
0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126,
0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385,
1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644,
1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903,
1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162,
2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421,
2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 ,
2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939,
3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,
3.4906585 , 3.55412502, 3.61759154, 3.68105806, 3.74452458,
3.8079911 , 3.87145761, 3.93492413, 3.99839065, 4.06185717,
4.12532369, 4.1887902 , 4.25225672, 4.31572324, 4.37918976,
4.44265628, 4.5061228 , 4.56958931, 4.63305583, 4.69652235,
4.75998887, 4.82345539, 4.88692191, 4.95038842, 5.01385494,
5.07732146, 5.14078798, 5.2042545 , 5.26772102, 5.33118753,
5.39465405, 5.45812057, 5.52158709, 5.58505361, 5.64852012,
5.71198664, 5.77545316, 5.83891968, 5.9023862 , 5.96585272,
6.02931923, 6.09278575, 6.15625227, 6.21971879, 6.28318531])
B = np.arange(3)
print(B)
print(np.sqrt(B))
[0 1 2]
[ 0. 1. 1.41421356]
A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0],
[3,4]] )
print(A.dot(B))
print(np.dot(A, B))
[[5 4]
[3 4]]
[[5 4]
[3 4]]