Sei sulla pagina 1di 3

Python

Rahul Sharma
December 2018

1 Reading CSV File and Linear Regression


Use pandas for it. The commands are:
import pandas as pd
import numpy as np
Data=pd.readcsv(”File.csv”)
Data[”price”] = Data[”price”].str.replace(”,”,””).astype(float) # remove com-
mas and convert string to float
X=pd.concat([rr.cityMPG,rr.weight,rr.hrspwr],axis=1) # Attaching coulmns
in dataframe
A=numpy.ones((len(rr.cityMPG),2)) print(A[100]) C=pd.DataFrame(”C1”:A[:,0])
# numpy array to dataframe
var= (numpy.linalg.inv((Xf inal.T ).dot(Xf inal)))#F ormulaf or(XT X)−1 in
python
import statsmodels
import statsmodels.api as sm
model = sm.OLS(y, Xf inal).f it()
model.summary()

2 For Loop While Loop, If else


• While loop
i=0
while i≤6:
print(i)
i=i+1
sum1=0
• For Loop
for x in range(10):
sum1=sum1+x
print(sum1)

1
• if-else statement
i=2
if i≥1 and i≤3:
print(i)
elif i≥3 and i≤6:
print(i)

3 Functions in Python and Vectors


def absolutev alue(num) :
if num¿=0:
return num
else:
return -num
print(absolute-value(-2))
x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])
x0**2 # Computes the square power of element in the vector
x0[:-1] # produces array([1, 2, 3])

4 Optimization in Python
from scipy.optimize import minimize
def rosen(x):
”””The Rosenbrock function”””
return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)
x0 = numpy.array([1.3, 0.7, 0.8, 1.9, 1.2])
res = minimize(rosen, x0, method=’BFGS’,options=’disp’: True)
print(res.x)

5 2-d and 3-d plots in python


• 2D plot
import matplotlib.pyplot as plt
plt.plot(X.hrspwr, y)
• 3D plot
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56] # put 0s on the y-axis, and put the y axis on the
z-axis

2
Y=rr.price
ax.scatter(xs=X.hrspwr, ys=X.cityMPG, zs=Y, zdir=’z’, label=’ys=0, zdir=z’)
plt.show()

6 Matrix Manipulation
import numpy as np
A = np.array([[2, 4], [5, -6]]) Defining a matrix
B=np.array([[3, 5],[6, 6]])
C=A+B
print(C)
print(C**2) # Calculate the power
print(C[:,0]) # Accessing first row
D=np.linalg.inv(C) # inverse
D.dot(C) # Matrix product
C*C # Hadmard product
E=np.zeros((2,3)) # Array of zeros
print(E)

7 Using libraries for LDA, NN, and VAE


Link for using edward 2 https://github.com/tensorflow/probability/blob/
master/tensorflow_probability/python/edward2/Upgrading_From_Edward_
To_Edward2.md
Some ubuntu commands:
$cdanaconda3/
/anaconda3$./bin/ipython

Potrebbero piacerti anche