177 KiB
177 KiB
In [1]:
import numpy as npIn [2]:
n = 10
x = np.random.normal(size=n)
print(x)[ 0.90148776 -0.63784548 0.61907743 -0.95073282 0.3897131 0.88123172 0.68407077 1.02936353 -0.85538578 -1.44188631]
In [3]:
import numpy as np
x = np.array([1, 2, 3])
print(x)[1 2 3]
In [4]:
import numpy as np
x = np.log(np.array([4, 7, 8]))
print(x)[1.38629436 1.94591015 2.07944154]
In [5]:
import numpy as np
from math import log
x = np.array([4, 7, 8])
for i in range(0, len(x)):
x[i] = log(x[i])
print(x)[1 1 2]
In [6]:
import numpy as np
x = np.log(np.array([4, 7, 8], dtype = np.float64))
print(x)[1.38629436 1.94591015 2.07944154]
In [7]:
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0]))
print(x)[1.38629436 1.94591015 2.07944154]
In [8]:
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0]))
print(x.itemsize)8
In [9]:
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
print(A)[[1.38629436 1.94591015 2.07944154] [1.09861229 2.30258509 2.39789527] [1.38629436 1.60943791 1.94591015]]
In [10]:
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
print(A[:,0])[1.38629436 1.09861229 1.38629436]
In [11]:
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
print(A[1,:])[1.09861229 2.30258509 2.39789527]
In [12]:
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to zero
A = np.zeros( (n, n) )
print(A)[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
In [13]:
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to one
A = np.ones( (n, n) )
print(A)[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
In [14]:
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]
A = np.random.rand(n, n)
print(A)[[9.01367154e-01 4.99358923e-02 8.62083431e-02 4.87662496e-01 4.35268384e-01 4.95778501e-01 8.47259540e-01 8.19492614e-01 7.48029002e-01 6.23905282e-01] [1.27137716e-01 2.21803999e-01 7.73335363e-01 8.12636533e-01 9.98254070e-01 5.73014312e-01 2.20054803e-01 3.32763666e-01 9.06059481e-01 8.89711757e-01] [8.96925518e-02 7.63406517e-01 6.37309895e-01 7.34414668e-01 5.05136701e-01 8.15223640e-04 8.94612568e-01 4.58800064e-01 7.05353991e-01 1.57603741e-01] [8.12532842e-01 9.31595458e-01 3.53618097e-01 9.13032740e-02 7.90877850e-01 7.77836912e-01 2.21341535e-01 4.30426390e-01 3.37060503e-01 8.73565723e-01] [9.00991648e-01 5.91377887e-01 1.24999213e-01 8.88127749e-02 7.49811765e-01 2.74541956e-01 6.70054572e-01 6.20486376e-01 3.12715695e-01 9.93767143e-01] [1.07972608e-01 9.20716385e-01 8.20068837e-01 5.64069531e-02 1.06786283e-01 2.76784740e-01 7.16634263e-01 7.86140930e-01 4.32839766e-01 9.78922154e-01] [4.56496194e-02 9.39925712e-01 4.16339920e-01 8.51501448e-01 6.50094775e-01 2.69132527e-01 9.01148802e-01 3.82213353e-01 8.63686244e-02 4.94507958e-01] [4.44878750e-01 5.09959399e-01 8.90303141e-01 1.35897374e-01 2.21820632e-01 2.18969262e-01 6.93515781e-01 6.38299113e-01 2.08291478e-01 4.53609787e-01] [5.28044814e-01 3.39354674e-01 7.73240597e-02 3.69972328e-01 4.52291156e-01 2.34048640e-01 7.34023583e-01 2.86689196e-01 4.68794083e-01 9.14515024e-01] [2.35728987e-01 5.58997922e-01 1.81586331e-01 7.01875673e-02 4.86286714e-01 1.91892059e-02 1.99830074e-01 1.38493791e-01 5.80880777e-01 9.24801038e-01]]
In [15]:
# Importing various packages
import numpy as np
n = 100
x = np.random.normal(size=n)
print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
print(np.mean(y))
z = x**3+np.random.normal(size=n)
print(np.mean(z))
W = np.vstack((x, y, z))
Sigma = np.cov(W)
print(Sigma)
Eigvals, Eigvecs = np.linalg.eig(Sigma)
print(Eigvals)0.19672699443907488 4.695047732333614 0.5139271024913812 [[0.9118261 2.86708608 1.95090925] [2.86708608 9.97030515 6.26396287] [1.95090925 6.26396287 6.36590624]] [15.50830953 0.07327648 1.66645148]
In [16]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse
eye = np.eye(4)
print(eye)
sparse_mtx = sparse.csr_matrix(eye)
print(sparse_mtx)
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,marker='x')
plt.show()[0;31m---------------------------------------------------------------------------[0m [0;31mModuleNotFoundError[0m Traceback (most recent call last) Cell [0;32mIn[16], line 1[0m [0;32m----> 1[0m [43mget_ipython[49m[43m([49m[43m)[49m[38;5;241;43m.[39;49m[43mrun_line_magic[49m[43m([49m[38;5;124;43m'[39;49m[38;5;124;43mmatplotlib[39;49m[38;5;124;43m'[39;49m[43m,[49m[43m [49m[38;5;124;43m'[39;49m[38;5;124;43minline[39;49m[38;5;124;43m'[39;49m[43m)[49m [1;32m 3[0m [38;5;28;01mimport[39;00m [38;5;21;01mnumpy[39;00m [38;5;28;01mas[39;00m [38;5;21;01mnp[39;00m [1;32m 4[0m [38;5;28;01mimport[39;00m [38;5;21;01mmatplotlib[39;00m[38;5;21;01m.[39;00m[38;5;21;01mpyplot[39;00m [38;5;28;01mas[39;00m [38;5;21;01mplt[39;00m File [0;32m~/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2432[0m, in [0;36mInteractiveShell.run_line_magic[0;34m(self, magic_name, line, _stack_depth)[0m [1;32m 2430[0m kwargs[[38;5;124m'[39m[38;5;124mlocal_ns[39m[38;5;124m'[39m] [38;5;241m=[39m [38;5;28mself[39m[38;5;241m.[39mget_local_scope(stack_depth) [1;32m 2431[0m [38;5;28;01mwith[39;00m [38;5;28mself[39m[38;5;241m.[39mbuiltin_trap: [0;32m-> 2432[0m result [38;5;241m=[39m [43mfn[49m[43m([49m[38;5;241;43m*[39;49m[43margs[49m[43m,[49m[43m [49m[38;5;241;43m*[39;49m[38;5;241;43m*[39;49m[43mkwargs[49m[43m)[49m [1;32m 2434[0m [38;5;66;03m# The code below prevents the output from being displayed[39;00m [1;32m 2435[0m [38;5;66;03m# when using magics with decorator @output_can_be_silenced[39;00m [1;32m 2436[0m [38;5;66;03m# when the last Python token in the expression is a ';'.[39;00m [1;32m 2437[0m [38;5;28;01mif[39;00m [38;5;28mgetattr[39m(fn, magic[38;5;241m.[39mMAGIC_OUTPUT_CAN_BE_SILENCED, [38;5;28;01mFalse[39;00m): File [0;32m~/miniforge3/lib/python3.9/site-packages/IPython/core/magics/pylab.py:99[0m, in [0;36mPylabMagics.matplotlib[0;34m(self, line)[0m [1;32m 97[0m [38;5;28mprint[39m([38;5;124m"[39m[38;5;124mAvailable matplotlib backends: [39m[38;5;132;01m%s[39;00m[38;5;124m"[39m [38;5;241m%[39m backends_list) [1;32m 98[0m [38;5;28;01melse[39;00m: [0;32m---> 99[0m gui, backend [38;5;241m=[39m [38;5;28;43mself[39;49m[38;5;241;43m.[39;49m[43mshell[49m[38;5;241;43m.[39;49m[43menable_matplotlib[49m[43m([49m[43margs[49m[38;5;241;43m.[39;49m[43mgui[49m[38;5;241;43m.[39;49m[43mlower[49m[43m([49m[43m)[49m[43m [49m[38;5;28;43;01mif[39;49;00m[43m [49m[38;5;28;43misinstance[39;49m[43m([49m[43margs[49m[38;5;241;43m.[39;49m[43mgui[49m[43m,[49m[43m [49m[38;5;28;43mstr[39;49m[43m)[49m[43m [49m[38;5;28;43;01melse[39;49;00m[43m [49m[43margs[49m[38;5;241;43m.[39;49m[43mgui[49m[43m)[49m [1;32m 100[0m [38;5;28mself[39m[38;5;241m.[39m_show_matplotlib_backend(args[38;5;241m.[39mgui, backend) File [0;32m~/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3606[0m, in [0;36mInteractiveShell.enable_matplotlib[0;34m(self, gui)[0m [1;32m 3585[0m [38;5;28;01mdef[39;00m [38;5;21menable_matplotlib[39m([38;5;28mself[39m, gui[38;5;241m=[39m[38;5;28;01mNone[39;00m): [1;32m 3586[0m [38;5;250m [39m[38;5;124;03m"""Enable interactive matplotlib and inline figure support.[39;00m [1;32m 3587[0m [1;32m 3588[0m [38;5;124;03m This takes the following steps:[39;00m [0;32m (...)[0m [1;32m 3604[0m [38;5;124;03m display figures inline.[39;00m [1;32m 3605[0m [38;5;124;03m """[39;00m [0;32m-> 3606[0m [38;5;28;01mfrom[39;00m [38;5;21;01mmatplotlib_inline[39;00m[38;5;21;01m.[39;00m[38;5;21;01mbackend_inline[39;00m [38;5;28;01mimport[39;00m configure_inline_support [1;32m 3608[0m [38;5;28;01mfrom[39;00m [38;5;21;01mIPython[39;00m[38;5;21;01m.[39;00m[38;5;21;01mcore[39;00m [38;5;28;01mimport[39;00m pylabtools [38;5;28;01mas[39;00m pt [1;32m 3609[0m gui, backend [38;5;241m=[39m pt[38;5;241m.[39mfind_gui_and_backend(gui, [38;5;28mself[39m[38;5;241m.[39mpylab_gui_select) File [0;32m~/miniforge3/lib/python3.9/site-packages/matplotlib_inline/__init__.py:1[0m [0;32m----> 1[0m [38;5;28;01mfrom[39;00m [38;5;21;01m.[39;00m [38;5;28;01mimport[39;00m backend_inline, config [38;5;66;03m# noqa[39;00m [1;32m 2[0m __version__ [38;5;241m=[39m [38;5;124m"[39m[38;5;124m0.1.6[39m[38;5;124m"[39m [38;5;66;03m# noqa[39;00m File [0;32m~/miniforge3/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:6[0m [1;32m 1[0m [38;5;124;03m"""A matplotlib backend for publishing figures via display_data"""[39;00m [1;32m 3[0m [38;5;66;03m# Copyright (c) IPython Development Team.[39;00m [1;32m 4[0m [38;5;66;03m# Distributed under the terms of the BSD 3-Clause License.[39;00m [0;32m----> 6[0m [38;5;28;01mimport[39;00m [38;5;21;01mmatplotlib[39;00m [1;32m 7[0m [38;5;28;01mfrom[39;00m [38;5;21;01mmatplotlib[39;00m [38;5;28;01mimport[39;00m colors [1;32m 8[0m [38;5;28;01mfrom[39;00m [38;5;21;01mmatplotlib[39;00m[38;5;21;01m.[39;00m[38;5;21;01mbackends[39;00m [38;5;28;01mimport[39;00m backend_agg [0;31mModuleNotFoundError[0m: No module named 'matplotlib'
In [17]:
import pandas as pd
from IPython.display import display
data = {'First Name': ["Frodo", "Bilbo", "Aragorn II", "Samwise"],
'Last Name': ["Baggins", "Baggins","Elessar","Gamgee"],
'Place of birth': ["Shire", "Shire", "Eriador", "Shire"],
'Date of Birth T.A.': [2968, 2890, 2931, 2980]
}
data_pandas = pd.DataFrame(data)
display(data_pandas)In [18]:
data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
display(data_pandas)In [19]:
display(data_pandas.loc['Aragorn'])In [20]:
new_hobbit = {'First Name': ["Peregrin"],
'Last Name': ["Took"],
'Place of birth': ["Shire"],
'Date of Birth T.A.': [2990]
}
data_pandas=data_pandas.append(pd.DataFrame(new_hobbit, index=['Pippin']))
display(data_pandas)In [21]:
import numpy as np
import pandas as pd
from IPython.display import display
np.random.seed(100)
# setting up a 10 x 5 matrix
rows = 10
cols = 5
a = np.random.randn(rows,cols)
df = pd.DataFrame(a)
display(df)
print(df.mean())
print(df.std())
display(df**2)In [22]:
df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
df.index = np.arange(10)
display(df)
print(df['Second'].mean() )
print(df.info())
print(df.describe())
from pylab import plt, mpl
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
df.cumsum().plot(lw=2.0, figsize=(10,6))
plt.show()
df.plot.bar(figsize=(10,6), rot=15)
plt.show()In [23]:
b = np.arange(16).reshape((4,4))
print(b)
df1 = pd.DataFrame(b)
print(df1)In [24]:
# Importing various packages
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
x = np.random.rand(100,1)
y = 2*x+np.random.randn(100,1)
linreg = LinearRegression()
linreg.fit(x,y)
xnew = np.array([[0],[1]])
ypredict = linreg.predict(xnew)
plt.plot(xnew, ypredict, "r-")
plt.plot(x, y ,'ro')
plt.axis([0,1.0,0, 5.0])
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title(r'Simple Linear Regression')
plt.show()Warning:
Output truncated. This notebook contains too many cells to display efficiently.
