<> Must do part :( It mainly refers to the document of Huang haiguang )
import numpy as np import matplotlib.pyplot as plt import pandas as pd df = pd.
read_csv('ex1data1.txt', names=['population', 'profit']) data = df #def
normalize_feature(df): #return df.apply(lambda column: (column - column.mean())
/ column.std())# Feature scaling def get_X(df):# Read feature ones = pd.DataFrame({'ones': np.ones(
len(df))})#ones yes m that 's ok 1 Column dataframe data = pd.concat([ones, df], axis=1) #
Consolidated data , Merge by column axis: Axis to merge links ,0 Yes ,1 It's a column return data.iloc[:, :-1] def linear_cost(theta
, X , y): m = X.shape[0] # Number of samples inner = X @ theta - y # The difference from the target is h(theta),inner Work it out in a row
square_sum= inner.T @ inner #h(theta) Square of cost = square_sum/(2*m) return cost
def gradient(theta, X, y): m = X.shape[0] inner = X.T @ (X@theta - y)
#X There is only one feature , HENGWEI 1 It doesn't count , That is, the statement is an update theta1 Time , Loss function pair theta1 Derivation of return inner/m def
batch_gradient_decent(theta, X, y, epoch, alpha=0.02): cost_data = [linear_cost(
theta, X, y)] for _ in range(epoch): #_ It's just a circular sign , It's not used in the loop theta = theta - alpha *
gradient(theta, X, y) cost_data.append(linear_cost(theta, X, y)) return theta,
cost_data X= get_X(df) y = df.values[:, 1] theta = np.zeros(df.shape[1]) epoch =
6000 final_theta, cost_data = batch_gradient_decent(theta, X, y, epoch) b =
final_theta[0] k = final_theta[1] plt.scatter(data.population, data.profit,
label="Training data") plt.plot(data.population, data.population*k + b, label=
"Prediction") plt.xlabel('population') plt.ylabel('profit') plt.legend(loc=2)
forecast= float(input('population')) predict_profit = forecast*k+b print(
predict_profit) plt.scatter(forecast, predict_profit, marker='+', c='red') plt.
show()

My prediction (forecast) Input 23, It's marked in red

<> Selected part
import numpy as np import matplotlib.pyplot as plt import pandas as pd ax = plt
.axes(projection='3d') df = pd.read_csv('ex1data2.txt', names=['square',
'bedrooms', 'price']) def normalize_feature(df): return df.apply(lambda column:
(column - column.mean()) / column.std()) def get_X(df):# Read feature ones = pd.DataFrame
({'ones': np.ones(len(df))})#ones yes m that 's ok 1 Column dataframe data = pd.concat([ones, df],
axis=1) # Consolidated data , Merge by column axis: Axis to merge links ,0 Yes ,1 It's a column return data.iloc[:, :-1] def lr_cost
(theta, X, y): m = X.shape[0]#m Is the number of samples inner = X @ theta - y # R(m*1),X @
theta Equivalent to X.dot(theta) square_sum = inner.T @ inner cost = square_sum / (2 * m)
return cost def gradient(theta, X, y): m = X.shape[0] # Number of samples inner = X.T @ (X @
theta- y) # (m,n).T @ (m, 1) -> (n, 1),X @ theta Equivalent to X.dot(theta) return inner / m
def batch_gradient_decent(theta, X, y, epoch, alpha=0.01): cost_data = [lr_cost(
theta, X, y)] for _ in range(epoch): theta = theta - alpha * gradient(theta, X,
y) cost_data.append(lr_cost(theta, X, y)) return theta, cost_data def normalEqn(
X, y): # Normal equation theta = np.linalg.inv(X.T@X)@X.T@y#X.T@X Equivalent to X.T.dot(X) return theta
data= normalize_feature(df) # Feature scaling y = data.values[:, 2] X = get_X(data) ax.
scatter(X['square'], X['bedrooms'], y, alpha=0.3) plt.xlabel('square') plt.
ylabel('bedrooms') ax.set_zlabel(r'$prices$') epoch = 500 alpha = 0.01 theta =
np.zeros(X.shape[1]) # In this issue X There are three characteristics (1,square,bedrooms), therefore theta It starts with three zeros final_theta,
cost_data= batch_gradient_decent(theta, X, y, epoch, alpha=alpha) D =
final_theta[0] A = final_theta[1] B = final_theta[2] Z = A*X['square'] + B*X[
'bedrooms'] + D ax.plot_trisurf(X['square'], X['bedrooms'], Z, linewidth=0,
antialiased=False) predict_square = float(input('square:')) predict_square = ((
predict_square- df.square.mean())/df.square.std()) predict_bedrooms = float(
input('bedrooms')) predict_bedrooms = ((predict_bedrooms - df.bedrooms.mean())/
df.bedrooms.std()) p = A * predict_square + B*predict_bedrooms + D ax.scatter(
predict_square, predict_bedrooms, marker='+', c='red') p = p * df.price.std() +
df.price.mean() print('I predict the prices is :') print(p) plt.show()

Enter as
square=1635
bedrooms=3
I predict the prices is :
292611.913236568
In theory, the predicted point should be on the drawn plane , But it's not , Maybe it's because I didn't draw the right plane . The prediction results can be used .

Technology