220 KiB
220 KiB
In [1]:
import numpy as npIn [2]:
n = 10
x = np.random.normal(size=n)
print(x)[ 1.81737781 0.45847011 0.53332849 1.04937026 0.53235952 2.24033384 -0.05605892 -0.02193078 -0.37343957 0.17849836]
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)[[0.15586219 0.68891428 0.38675231 0.63546905 0.97286326 0.423102 0.42841423 0.3478095 0.66130478 0.63443371] [0.88685415 0.82561703 0.56766321 0.43258818 0.99057275 0.40058341 0.84581375 0.64630711 0.03266152 0.57599494] [0.10145028 0.9074157 0.39386718 0.12831838 0.46242424 0.09280524 0.88415819 0.26443391 0.55095316 0.85897711] [0.69243548 0.33745439 0.14452065 0.29030256 0.8063779 0.60720029 0.42914251 0.44895662 0.09310479 0.48442601] [0.95152814 0.83294718 0.11005335 0.8758851 0.19375828 0.73888203 0.83203197 0.69203997 0.65147818 0.35195241] [0.21506004 0.24874378 0.31370028 0.9525328 0.71672791 0.05879106 0.45007578 0.36388542 0.50937003 0.57854115] [0.80033616 0.45273617 0.18038547 0.49557088 0.36209091 0.44512218 0.84078641 0.28924386 0.99166852 0.22896144] [0.77579275 0.83519517 0.40640797 0.66272614 0.18234499 0.97628064 0.19808709 0.1280526 0.33700495 0.32114535] [0.95462534 0.72047148 0.24512233 0.18474924 0.69169665 0.68763036 0.8861811 0.54193001 0.87830277 0.79251831] [0.13092444 0.41452482 0.40447213 0.89714357 0.25360039 0.80373997 0.51279028 0.58161787 0.08742496 0.45104086]]
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.0458524213754298 3.614161296466206 -0.22985907723809532 [[0.72589774 2.09219464 1.64672839] [2.09219464 6.9187554 4.62198131] [1.64672839 4.62198131 6.70530438]] [12.05431945 0.07101262 2.22462544]
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()[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] (0, 0) 1.0 (1, 1) 1.0 (2, 2) 1.0 (3, 3) 1.0
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)| First Name | Last Name | Place of birth | Date of Birth T.A. | |
|---|---|---|---|---|
| 0 | Frodo | Baggins | Shire | 2968 |
| 1 | Bilbo | Baggins | Shire | 2890 |
| 2 | Aragorn II | Elessar | Eriador | 2931 |
| 3 | Samwise | Gamgee | Shire | 2980 |
In [18]:
data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
display(data_pandas)| First Name | Last Name | Place of birth | Date of Birth T.A. | |
|---|---|---|---|---|
| Frodo | Frodo | Baggins | Shire | 2968 |
| Bilbo | Bilbo | Baggins | Shire | 2890 |
| Aragorn | Aragorn II | Elessar | Eriador | 2931 |
| Sam | Samwise | Gamgee | Shire | 2980 |
In [19]:
display(data_pandas.loc['Aragorn'])First Name Aragorn II Last Name Elessar Place of birth Eriador Date of Birth T.A. 2931 Name: Aragorn, dtype: object
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)[0;31m---------------------------------------------------------------------------[0m
[0;31mAttributeError[0m Traceback (most recent call last)
[0;32m/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_57294/1326197715.py[0m in [0;36m?[0;34m()[0m
[0;32m----> 6[0;31m new_hobbit = {'First Name': ["Peregrin"],
[0m[1;32m 7[0m [0;34m'Last Name'[0m[0;34m:[0m [0;34m[[0m[0;34m"Took"[0m[0;34m][0m[0;34m,[0m[0;34m[0m[0;34m[0m[0m
[1;32m 8[0m [0;34m'Place of birth'[0m[0;34m:[0m [0;34m[[0m[0;34m"Shire"[0m[0;34m][0m[0;34m,[0m[0;34m[0m[0;34m[0m[0m
[1;32m 9[0m [0;34m'Date of Birth T.A.'[0m[0;34m:[0m [0;34m[[0m[0;36m2990[0m[0;34m][0m[0;34m[0m[0;34m[0m[0m
[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/pandas/core/generic.py[0m in [0;36m?[0;34m(self, name)[0m
[1;32m 6200[0m [0;32mand[0m [0mname[0m [0;32mnot[0m [0;32min[0m [0mself[0m[0;34m.[0m[0m_accessors[0m[0;34m[0m[0;34m[0m[0m
[1;32m 6201[0m [0;32mand[0m [0mself[0m[0;34m.[0m[0m_info_axis[0m[0;34m.[0m[0m_can_hold_identifiers_and_holds_name[0m[0;34m([0m[0mname[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[1;32m 6202[0m ):
[1;32m 6203[0m [0;32mreturn[0m [0mself[0m[0;34m[[0m[0mname[0m[0;34m][0m[0;34m[0m[0;34m[0m[0m
[0;32m-> 6204[0;31m [0;32mreturn[0m [0mobject[0m[0;34m.[0m[0m__getattribute__[0m[0;34m([0m[0mself[0m[0;34m,[0m [0mname[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0m
[0;31mAttributeError[0m: 'DataFrame' object has no attribute 'append'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.
