Qadamlar soni:
a=0.01
Natijam:
Bu ikkinchi dastur kodi darsda ishlaganimiz
import time
import numpy as np
import matplotlib.pyplot as plt
N=int(input("N sonini kiriting =>"))
x=np.zeros(N)
y=np.zeros(N-1)
r=np.zeros(N)
w=np.zeros(N)
w1=np.zeros(N-1)
x[0]=1
for i in range(1,N):
x[i]=x[i-1]+1
y[i-1]=3/2*x[i-1]
w[0] = 0
a=0.01
for i in range(1,N):
S=0
for j in range(0,3):
r[j]=pow(w[i-1]*x[j]-y[j],2)
S=S+r[j]
w1[i-1]=S/3
w[i] = w[i-1] + a
print(w1[i-1])
plt.plot(w1)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import math
N=int(input('N='))
X=int(input('X='))
w=np.zeros(N)
Y=np.zeros(X)
B=np.zeros(X)
S=np.zeros(X)
r=np.zeros(N)
x=[1,2,3]
y=[3/2,3,9/2]
a=0.001
w[0]=3.6
k=0
for i in range(1,N):
for j in range(0,3):
w[i]=w[i-1]-a*2*(w[i-1]*x[j]-y[j])*x[j]
r[i-1]=w[i-1]*x[j]-y[j]
if 0.0001< r[i-1] and r[i-1]<0.001:
k=w[i-1]
print(w[i-1])
break
for i in range(4,X):
Y[i]=k*i
print(Y[i])
e=2.71
for i in range(4,X):
B[i]=pow(e,Y[i])/(pow(e,Y[i])+1)
if B[i]>=0.5:
S[i]=1
else :
S[i]=0
print(S[i])
#print(B[i])
plt.plot(r)
plt.show()
2 topshiriq
# Y=3/2X+b
import numpy as np
import matplotlib.pyplot as plt
import math
N=int(input('N='))
X=int(input('X='))
w=np.zeros(N)
Y=np.zeros(X)
B=np.zeros(X)
S=np.zeros(X)
r=np.zeros(N)
x=[1,2,3]
y=[3/2,3,9/2]
a=0.001
w[0]=3.6
k=0
for i in range(1,N):
for j in range(0,3):
w[i]=w[i-1]-a*2*(w[i-1]*x[j]-y[j])*x[j]
r[i-1]=w[i-1]*x[j]-y[j]
if 0.0001< r[i-1] and r[i-1]<0.001:
k=w[i-1]
print(w[i-1])
break
#print(B[i])
plt.plot(r)
plt.show()
Mashinali o‘qitishda Logistik regressiya yordamida sinflashtirish masalasini yechish algoritmi va dasturini tuzish.
Lagistik regressiya
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# import tensorflow as tf
# from sklearn.preprocessing import OneHotEncoder
def sigmoid(z):
return 1 / (1 + np.exp(- z))
plt.plot(np.arange(-5, 5, 0.1), sigmoid(np.arange(-5, 5, 0.1)))
plt.title('Sigmoid funktsiyani vizualizatsiya qilish')
plt.show()
data = pd.read_csv('data.csv', header=None)
print("Ma'lumotlar shakli:", data.shape)
print(data.head())
# Matritsa xususiyati
x_orig = data.iloc[:, 1:-1].values
# Ma'lumotlar belgilari
y_orig = data.iloc[:, -1:].values
print("Matritsa shaklining xususiyatlari:", x_orig.shape)
print("Vektorning shakl yorlig'i :", y_orig.shape)
# Ijobiy ma'lumotlar nuqtalari
x_pos = np.array([x_orig[i] for i in range(len(x_orig))
if y_orig[i] == 1])
# Salbiy ma'lumotlar nuqtalari
x_neg = np.array([x_orig[i] for i in range(len(x_orig))
if y_orig[i] == 0])
# Salbiy ma'lumotlar nuqtalarini chizish
plt.scatter(x_neg[:, 0], x_neg[:, 1], color='red', label='Salbiy')
# Ijobiy ma'lumotlar nuqtalarini chizish
plt.scatter(x_pos[:, 0], x_pos[:, 1], color='green', label='Ijobiy')
plt.xlabel('1-xususiyat')
plt.ylabel('2-xususiyat')
plt.title("Berilgan ma'lumotlarning grafik ko'rinishi")
plt.legend()
plt.show()
Qo`shimcha C++ da
Logistik regressiya C++
#include
#include
#include
#include
#include
using namespace std;
int main() {
double x[100],y[100],y1[100],loss[100];
srand(time(0));
ofstream Logistik;
Logistik.open("Logistik_reg.csv");
for(int i=0;i<100;i++){
x[i]=rand()%10;
y[i]=rand()%10;
cout<<"\n x["< cout<<" y["< y1[i]=1/(1+exp(-y[i]));
cout<<" y1["< if(y[i]<=x[i]) {
cout<<" -> True "<<1;
}
else {
cout<<" -> False "<<0;
}
loss[i]=fabs((1-y[i])*log(1-y1[i])+y[i]*log(y1[i]));
} Logistik<<" Y1 "< for(int i=0;i<100;i++){
if(y1[i]<=0.5) y1[i]=0;
else y1[i]=1;
Logistik< } Logistik<<" Loss "< for(int i=0;i<100;i++){
Logistik< }
return 0;
}
|