In [6]:
for i in range(10):
y = i*40 +i**2 + 2
print(y)
i = i+2
In [7]:
range(10)
Out[7]:
In [8]:
for i in ["a","b","tt"]:
print i
In [20]:
x = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[2, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0,5, 2, 3, 4, 5, 6, 7, 8, 9]]
print(x)
In [23]:
np.mean(x)
Out[23]:
In [24]:
x= np.linspace(-1,1,100)
In [25]:
x
Out[25]:
In [29]:
x[[34,67]]
Out[29]:
In [30]:
x[45:90]
Out[30]:
In [31]:
x[::3]
Out[31]:
In [32]:
x[10::3]
Out[32]:
In [33]:
x[::-1]
Out[33]:
In [34]:
x= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [35]:
x[::-1]
Out[35]:
In [37]:
y = np.linspace(0,10,100)
x= np.linspace(-1,1,100)
In [38]:
x
Out[38]:
careful with "=":
In [47]:
xx = x.copy()
In [48]:
xx+=2
In [49]:
xx
Out[49]:
In [50]:
x
Out[50]:
In [52]:
xlist = [3,4,5,6,7,8,9]
xarray = np.asarray([3,4,5,6,7,8,9]) # np.asarray(xlist)
In [53]:
xlist*2
Out[53]:
In [54]:
xarray*2
Out[54]:
In [56]:
strangelist = ["toto",3,{},[]]
In [60]:
np.asarray(strangelist)*2
In [64]:
x
Out[64]:
In [65]:
mask = x>2
In [66]:
mask
Out[66]:
In [68]:
x[mask] # x[x>2]
Out[68]:
In [76]:
x[ (x>2) & (x<2.5) ] # x[ (x>2) * (x>1.5) ] # both have to be true
Out[76]:
In [75]:
x[ (x>2) | (x>1.5) ] # x[ (x>2) + (x>1.5) ] # any have to be true
Out[75]:
In [78]:
iamnan = np.NaN
In [79]:
iamnan
Out[79]:
In [80]:
iamnan==iamnan
Out[80]:
In [82]:
np.inf==np.inf
Out[82]:
In [83]:
xwithnan = np.asarray([3,4,5,6,7,2,3,np.NaN,75,75])
In [84]:
xwithnan
Out[84]:
In [85]:
xwithnan*2
Out[85]:
In [86]:
4+np.NaN
Out[86]:
In [87]:
4/np.NaN
Out[87]:
In [88]:
4**np.NaN
Out[88]:
In [89]:
np.mean(xwithnan)
Out[89]:
In [90]:
np.nanmean(xwithnan)
Out[90]:
In [91]:
np.mean(xwithnan[xwithnan==xwithnan])
Out[91]:
In [96]:
~(xwithnan==xwithnan)
Out[96]:
In [93]:
xwithnan!=xwithnan
Out[93]:
In [97]:
np.isnan(xwithnan)
Out[97]:
In [98]:
xwithnan = [3,4,5,6,7,2,3,np.NaN,75,75]
In [100]:
xwithnan[xwithnan==xwithnan]
Out[100]:
In [105]:
0 == False
Out[105]:
In [ ]:
1 == True
In [117]:
a = np.random.rand(30)
b = np.random.rand(30)
In [118]:
# plot within the notebook
%matplotlib inline
import matplotlib.pyplot as mpl
In [119]:
pl = mpl.hist(a)
In [126]:
mpl.scatter(a,b,s=150, facecolors="None", edgecolors="b",lw=3)
Out[126]:
In [ ]: