229 KiB
229 KiB
In [1]:
import numpy as npIn [3]:
n = 100
x = np.random.normal(size=n)
print(x)[ 0.22889528 0.91634536 0.58026357 -0.20121369 1.63697022 -0.16620476 -1.88888726 -2.65872949 -0.00563363 1.25894439 -0.07825328 1.43521368 0.97253438 -1.64819989 0.98672233 -1.73087706 -0.99593933 -1.34926433 -1.26715086 -0.95021858 -0.33244987 0.69501728 -0.48914472 1.87699249 -0.91130987 -0.2889529 -0.74183199 0.74935175 1.34457198 -1.70306077 -0.21142106 2.10793224 -0.40622194 2.34515157 -0.40761485 1.69110258 0.72886914 -0.43700299 -1.14714793 0.15694733 0.66461823 0.89304652 1.4543903 -0.37398506 -0.14684297 -0.57722604 -0.44036741 -0.40055163 0.21510519 -0.53120826 -0.43941984 0.68555857 0.85074613 -0.8371588 1.19968839 0.59476652 1.09647082 0.67324848 0.5365493 -0.11823534 0.52454391 1.43031082 0.22692571 -0.87295854 -2.82106202 -0.27436094 0.51321315 0.51606513 -1.56397437 -1.23442784 0.43472146 0.74179877 0.73562128 1.00883303 -0.05851042 -1.19276526 1.16715641 -0.6401151 0.81572064 1.0670422 -0.38749302 -1.67392967 -2.76996862 0.34870472 1.3352494 -1.23489158 -1.70233983 0.62477383 0.14297718 0.23248733 0.26787549 1.75420536 -1.22660411 -0.27597454 0.28073813 0.42879538 1.64568937 -0.19205253 1.9311968 -0.26983672]
In [5]:
import numpy as np
x = np.array([1, 2, 4])
print(x)[1 2 4]
In [4]:
import numpy as np
x = np.log(np.array([4, 7, 8]))
print(x)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)In [6]:
import numpy as np
x = np.log(np.array([4, 7, 8], dtype = np.float64))
print(x)In [7]:
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0]))
print(x)In [8]:
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0]))
print(x.itemsize)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)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])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,:])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)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)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)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)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()In [1]:
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)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 [2]:
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)| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | -1.749765 | 0.342680 | 1.153036 | -0.252436 | 0.981321 |
| 1 | 0.514219 | 0.221180 | -1.070043 | -0.189496 | 0.255001 |
| 2 | -0.458027 | 0.435163 | -0.583595 | 0.816847 | 0.672721 |
| 3 | -0.104411 | -0.531280 | 1.029733 | -0.438136 | -1.118318 |
| 4 | 1.618982 | 1.541605 | -0.251879 | -0.842436 | 0.184519 |
| 5 | 0.937082 | 0.731000 | 1.361556 | -0.326238 | 0.055676 |
| 6 | 0.222400 | -1.443217 | -0.756352 | 0.816454 | 0.750445 |
| 7 | -0.455947 | 1.189622 | -1.690617 | -1.356399 | -1.232435 |
| 8 | -0.544439 | -0.668172 | 0.007315 | -0.612939 | 1.299748 |
| 9 | -1.733096 | -0.983310 | 0.357508 | -1.613579 | 1.470714 |
0 -0.175300 1 0.083527 2 -0.044334 3 -0.399836 4 0.331939 dtype: float64 0 1.069584 1 0.965548 2 1.018232 3 0.793167 4 0.918992 dtype: float64
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | 3.061679 | 0.117430 | 1.329492 | 0.063724 | 0.962990 |
| 1 | 0.264421 | 0.048920 | 1.144993 | 0.035909 | 0.065026 |
| 2 | 0.209789 | 0.189367 | 0.340583 | 0.667239 | 0.452553 |
| 3 | 0.010902 | 0.282259 | 1.060349 | 0.191963 | 1.250636 |
| 4 | 2.621102 | 2.376547 | 0.063443 | 0.709698 | 0.034047 |
| 5 | 0.878123 | 0.534362 | 1.853835 | 0.106431 | 0.003100 |
| 6 | 0.049462 | 2.082875 | 0.572069 | 0.666597 | 0.563167 |
| 7 | 0.207888 | 1.415201 | 2.858185 | 1.839818 | 1.518895 |
| 8 | 0.296414 | 0.446453 | 0.000054 | 0.375694 | 1.689345 |
| 9 | 3.003620 | 0.966899 | 0.127812 | 2.603636 | 2.162999 |
In [6]:
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()[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) Cell [0;32mIn[6], line 1[0m [0;32m----> 1[0m [43mdf[49m[38;5;241m.[39mcolumns [38;5;241m=[39m [[38;5;124m'[39m[38;5;124mFirst[39m[38;5;124m'[39m, [38;5;124m'[39m[38;5;124mSecond[39m[38;5;124m'[39m, [38;5;124m'[39m[38;5;124mThird[39m[38;5;124m'[39m, [38;5;124m'[39m[38;5;124mFourth[39m[38;5;124m'[39m, [38;5;124m'[39m[38;5;124mFifth[39m[38;5;124m'[39m] [1;32m 2[0m df[38;5;241m.[39mindex [38;5;241m=[39m np[38;5;241m.[39marange([38;5;241m10[39m) [1;32m 4[0m display(df) [0;31mNameError[0m: name 'df' is not defined
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.


