221 KiB
221 KiB
In [1]:
import numpy as npIn [2]:
n = 10
x = np.random.normal(size=n)
print(x)[ 2.05896588 0.43642532 -0.21096409 0.40779804 -1.13963886 -0.53677418 0.83572684 0.58831595 0.30882611 -2.07060477]
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)[[4.62790954e-01 3.05355909e-01 4.36307837e-01 5.75663043e-01 8.47455900e-01 8.88156953e-01 3.41214900e-01 4.80217592e-01 3.51294625e-01 4.55894547e-01] [9.36928932e-01 1.34614286e-01 1.68706337e-01 5.12168283e-01 1.82057187e-01 5.04384702e-01 3.90807328e-01 7.21325310e-01 6.82139912e-04 7.75656839e-01] [7.39975586e-01 7.28371709e-02 4.47008569e-01 1.48689896e-01 8.77801149e-01 8.77111122e-01 6.14302755e-01 3.05347541e-01 5.22304438e-01 1.19480182e-01] [3.75927527e-01 1.79526117e-01 1.10245050e-01 2.14621443e-02 2.18149821e-01 6.60754644e-01 7.14889592e-01 2.00365236e-01 1.87081411e-01 8.98532247e-01] [9.76246245e-01 7.27600064e-02 6.40428736e-01 2.44633057e-01 2.13315320e-01 9.04923596e-01 1.01278843e-01 1.60040487e-02 9.57338433e-01 4.54609970e-01] [9.70716394e-01 8.19560587e-01 8.00696646e-01 8.50286966e-01 6.01413231e-01 5.35401167e-01 5.09318595e-01 7.50871033e-01 1.77320278e-01 1.41563755e-01] [5.58999323e-01 8.27823274e-02 2.45162718e-01 9.35805174e-01 3.97967922e-01 6.41998971e-01 2.85999165e-01 9.42305845e-01 6.51639460e-01 2.40037335e-01] [2.07463601e-01 6.36551310e-01 3.74118364e-01 9.48754846e-01 6.84043753e-01 8.69630846e-01 2.90701292e-01 3.73794006e-01 9.56898405e-01 7.53969725e-01] [8.69421667e-01 9.16907984e-01 2.60498453e-01 1.05394015e-01 8.28209931e-01 2.18273724e-01 5.70725653e-01 5.90412777e-01 3.15706255e-01 1.00031589e-01] [3.13129360e-02 9.74280470e-01 1.56927535e-01 2.05412497e-01 3.29614274e-01 1.32235211e-01 9.66580589e-01 6.25369588e-01 5.91506268e-01 3.63969272e-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.022796599742659046 4.026047728952776 -0.08626455960524361 [[ 1.07037904 3.23910863 3.46675556] [ 3.23910863 10.63080397 10.39964939] [ 3.46675556 10.39964939 18.1421739 ]] [26.31853116 0.06730967 3.45751608]
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_23270/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.
