梯度下降方法和scikit-learn两种方法预测学生是否被录取,需要数据集的可留言
##方法1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def sigmoid(z):
return 1/(1+np.exp(-z))
def predict(theta,X):
prob = sigmoid(X*theta.T)
return [1 if a>=0.5 else 0 for a in prob]
def gradientDescent(X,y,theta,alpha,m,numIter):
XTrans = X.transpose
t = theta
for i in range(0,numIter):
gradient=0
theta = np.mat(t)
pred = np.array(predict(theta,X))
loss = pred - y
gradient = np.dot(XTrans,loss)
theta = theta-alpha*gradient ###参数更新
return theta
##开始预测,对学生是否被录取的逻辑回归预测
df = pd.read_csv('./data/5_logisitic_admit.csv')
df.insert(1,'Ones',1)
#print(df.head(10))
positive = df[df['admit']==1]
negative = df[df['admit']==0]
fig,ax = plt.subplots(figsize=(8,5))
ax.scatter(positive['gre'],positive['gpa'],s=30,c='b',marker='o',label='admit')
ax.scatter(negative['gre'],negative['gpa'],s=30,c='r',marker='x',label='not admit')
ax.legend()
ax.set_xlabel('gre')
ax.set_ylabel('gpa')
#plt.show()
x = df.iloc[:,1:4]
y = df['admit']
x = np.array(x.values)
y = np.array(y.values)
m,n = np.shape(x)
print(m,n)
t = np.ones(n)
numIter = 1000
alpha = 0.00001
theta = gradientDescent(x,y,t,alpha,m,numIter)
pred = Predict(theta,x)
##方法2
from sklearn.linear_model.logistic import LogisticRegression
lf = LogisticRegression()
lf.fit(x,y)
pred=lf.predict(x)
print(pred)