liste = [4,5,6,7,82,3,1,5,28,2]
liste
[4, 5, 6, 7, 82, 3, 1, 5, 28, 2]
max(liste)
82
maxi = liste[0]
for i in range(len(liste)):
if(maxi < liste[i]):
maxi = liste[i]
maxi
82
def maximum(liste):
maxi = liste[0]
for i in range(len(liste)):
if(maxi < liste[i]):
maxi = liste[i]
return maxi
maximum([3,4,2,5,9])
9
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def Hata(w):
return ((w-3)**2)+5
def turev(w):
return 2*(w-3)
def gradyan_inis(w = 0, alpha = 0.05, dongu = 20):
W = np.zeros(dongu)
for i in range(dongu):
W[i] = w
w = w - alpha * turev(w)
return W
a = 0.95
W = gradyan_inis(w = 40, alpha = 0.95)
W
array([ 40. , -30.3 , 32.97 , -23.973 , 27.2757 , -18.84813 , 22.663317 , -14.6969853 , 18.92728677, -11.33455809, 15.90110228, -8.61099206, 13.44989285, -6.40490356, 11.46441321, -4.61797189, 9.8561747 , -3.17055723, 8.55350151, -1.99815136])
import matplotlib.pyplot as plt
%matplotlib inline
t = np.arange(-50,50,0.5)
plt.plot(W,Hata(W),'k')
plt.scatter(W,Hata(W), color = 'red')
plt.plot(t,Hata(t))
plt.xlabel('w'); plt.ylabel('Hata(w)'); plt.title("alpha = " + str(a))
plt.grid()
a = 0.05
W = gradyan_inis(w = 40, alpha = a)
t = np.arange(-50,50,0.5)
plt.plot(W,Hata(W),'k')
plt.scatter(W,Hata(W), color = 'red')
plt.plot(t,Hata(t))
plt.xlabel('w'); plt.ylabel('Hata(w)'); plt.title("alpha = " + str(a))
plt.grid()
W[-1]
7.9981513553900685
a = 0.05
W = gradyan_inis(w = 40, alpha = a, dongu=100000)
t = np.arange(-50,50,0.5)
plt.plot(W,Hata(W),'k')
plt.scatter(W,Hata(W), color = 'red')
plt.plot(t,Hata(t))
plt.xlabel('w'); plt.ylabel('Hata(w)'); plt.title("alpha = " + str(a))
plt.grid()
W[-1]
3.0000000000000018
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.arange(10, 100, step= 5)
y = 2 * x + 5 + 20 * np.random.randn(18)
plt.scatter(x,y)
plt.xlabel("x")
plt.ylabel("y")
Text(0,0.5,'y')
from sklearn.linear_model import LinearRegression
model = LinearRegression()
x = x.reshape(-1, 1)
model.fit(x,y)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
x
array([[10], [15], [20], [25], [30], [35], [40], [45], [50], [55], [60], [65], [70], [75], [80], [85], [90], [95]])
x_test = np.arange(100)
x_test = x_test.reshape(-1, 1)
x
array([[10], [15], [20], [25], [30], [35], [40], [45], [50], [55], [60], [65], [70], [75], [80], [85], [90], [95]])
y_pred = model.predict(x_test)
plt.plot(x_test, y_pred, 'r')
plt.scatter(x,y)
plt.xlabel("x")
plt.ylabel("x")
Text(0,0.5,'x')
# import libraries
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.linear_model import LinearRegression
# Collect data
x = np.arange(10, 100, step= 5)
y = 2 * x + 5 + 20 * np.random.randn(18)
x = x.reshape(-1, 1)
# train test split
x_test = np.arange(100)
x_test = x_test.reshape(-1, 1)
# Apply Machine learning algorithm
model = LinearRegression()
# learn parameters with fit
model.fit(x,y)
# prediction
y_pred = model.predict(x_test)
plt.plot(x_test, y_pred, 'r')
plt.scatter(x,y)
plt.xlabel("x")
plt.ylabel("x")
Text(0,0.5,'x')
#Evaluation
# print the R-squared value for the model
model.score(x, y)
0.8970703805274145
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
from sklearn.linear_model import LogisticRegression
# Collect data
x0 = np.random.randn(100) + 2
x1 = np.random.randn(100) + 2
x0_ = np.random.randn(100) + 3
x1_ = np.random.randn(100) + 3
xx0 = np.concatenate((x0,x0_))
xx1 = np.concatenate((x1,x1_))
y = np.concatenate((np.zeros(100) ,np.ones(100)))
d = {'x0': xx0, "x1":xx1, "y":y}
data = pd.DataFrame(d)
data.head(3)
x0 | x1 | y | |
---|---|---|---|
0 | 1.332974 | 0.563395 | 0.0 |
1 | 2.360866 | 0.673493 | 0.0 |
2 | 1.704928 | 2.181285 | 0.0 |
c=['b','r']
mycolors = [c[0] if i==0 else c[1] for i in y]
data.plot.scatter('x0', 'x1' ,c=mycolors)
<matplotlib.axes._subplots.AxesSubplot at 0x1a1c414780>
# train test split
from sklearn.cross_validation import train_test_split
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# Split dataset into train ab=nd test sets
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 1/4, random_state = 0)
len(X_train), len(y_test)
(150, 50)
# Apply Machine learning algorithm
model = LogisticRegression()
# learn parameters with fit
model.fit(X_train, y_train)
# prediction
y_pred = model.predict(X_test)
#Evaluation
from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)
0.88
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)
array([[20, 3], [ 3, 24]])