TensorFlow-线性回归

TensorFlow-线性回归 (TensorFlow - Linear Regression)

In this chapter, we will focus on the basic example of linear regression implementation using TensorFlow. Logistic regression or linear regression is a supervised machine learning approach for the classification of order discrete categories. Our goal in this chapter is to build a model by which a user can predict the relationship between predictor variables and one or more independent variables.

在本章中,我们将重点介绍使用TensorFlow进行线性回归的基本示例。 Logistic回归或线性回归是一种有监督的机器学习方法,用于对离散量类别进行分类。 本章的目标是建立一个模型,用户可以通过该模型来预测预测变量和一个或多个自变量之间的关系。

The relationship between these two variables is cons −idered linear. If y is the dependent variable and x is considered as the independent variable, then the linear regression relationship of two variables will look like the following equation −

这两个变量之间的关系被认为是线性的。 如果y是因变量且x被视为自变量,则两个变量的线性回归关系将类似于以下方程式-


Y = Ax+b

We will design an algorithm for linear regression. This will allow us to understand the following two important concepts −

我们将设计用于线性回归的算法。 这将使我们能够理解以下两个重要概念-

  • Cost Function

    成本函数
  • Gradient descent algorithms

    梯度下降算法

The schematic representation of linear regression is mentioned below −

线性回归的示意图表示如下-

Schematic Representation Linear Regression

The graphical view of the equation of linear regression is mentioned below −

下面提到线性回归方程的图形视图-

Graphical Schematic Representation

设计线性回归算法的步骤 (Steps to design an algorithm for linear regression)

We will now learn about the steps that help in designing an algorithm for linear regression.

现在,我们将学习有助于设计线性回归算法的步骤。

第1步 (Step 1)

It is important to import the necessary modules for plotting the linear regression module. We start importing the Python library NumPy and Matplotlib.

导入用于绘制线性回归模块的必要模块很重要。 我们开始导入Python库NumPy和Matplotlib。


import numpy as np 
import matplotlib.pyplot as plt

第2步 (Step 2)

Define the number of coefficients necessary for logistic regression.

定义逻辑回归所需的系数数。


number_of_points = 500 
x_point = [] 
y_point = [] 
a = 0.22 
b = 0.78

第三步 (Step 3)

Iterate the variables for generating 300 random points around the regression equation −

迭代变量以围绕回归方程生成300个随机点-

Y = 0.22x+0.78

Y = 0.22x + 0.78


for i in range(number_of_points): 
   x = np.random.normal(0.0,0.5) 
   y = a*x + b +np.random.normal(0.0,0.1) x_point.append([x]) 
   y_point.append([y])

第4步 (Step 4)

View the generated points using Matplotlib.

使用Matplotlib查看生成的点。


fplt.plot(x_point,y_point, 'o', label = 'Input Data') plt.legend() plt.show()

The complete code for logistic regression is as follows −

逻辑回归的完整代码如下-


import numpy as np 
import matplotlib.pyplot as plt 

number_of_points = 500 
x_point = [] 
y_point = [] 
a = 0.22 
b = 0.78 

for i in range(number_of_points): 
   x = np.random.normal(0.0,0.5) 
   y = a*x + b +np.random.normal(0.0,0.1) x_point.append([x]) 
   y_point.append([y]) 
   
plt.plot(x_point,y_point, 'o', label = 'Input Data') plt.legend() 
plt.show()

The number of points which is taken as input is considered as input data.

被视为输入的点数被视为输入数据。

Code For Logistic Regression

翻译自: https://www.tutorialspoint.com/tensorflow/tensorflow_linear_regression.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow是一个开源的机器学习框架,它可以用于各种各样的任务,包括多元线性回归。多元线性回归是一种用于预测因变量和多个自变量之间关系的统计学方法。在TensorFlow中,我们可以使用线性回归模型来实现多元线性回归。 在TensorFlow中,我们可以使用tf.estimator API来构建线性回归模型。首先,我们需要定义特征列,这些特征列将用于训练模型。然后,我们可以使用tf.estimator.LinearRegressor类来定义模型,并使用train()方法来训练模型。最后,我们可以使用evaluate()方法来评估模型的性能,并使用predict()方法来进行预测。 下面是一个简单的TensorFlow多元线性回归的代码示例: ``` import tensorflow as tf # 定义特征列 feature_columns = [ tf.feature_column.numeric_column('feature1'), tf.feature_column.numeric_column('feature2'), tf.feature_column.numeric_column('feature3') ] # 定义模型 model = tf.estimator.LinearRegressor(feature_columns=feature_columns) # 定义训练数据 train_input_fn = tf.estimator.inputs.numpy_input_fn( x={'feature1': [1., 2., 3., 4.], 'feature2': [2., 4., 6., 8.], 'feature3': [3., 6., 9., 12.]}, y=[6., 12., 18., 24.], batch_size=2, num_epochs=None, shuffle=True ) # 训练模型 model.train(input_fn=train_input_fn, steps=1000) # 定义测试数据 test_input_fn = tf.estimator.inputs.numpy_input_fn( x={'feature1': [5., 6.], 'feature2': [10., 12.], 'feature3': [15., 18.]}, y=[30., 36.], batch_size=2, num_epochs=1, shuffle=False ) # 评估模型 eval_result = model.evaluate(input_fn=test_input_fn) print(eval_result) # 进行预测 predict_input_fn = tf.estimator.inputs.numpy_input_fn( x={'feature1': [7., 8.], 'feature2': [14., 16.], 'feature3': [21., 24.]}, num_epochs=1, shuffle=False ) predictions = model.predict(input_fn=predict_input_fn) for prediction in predictions: print(prediction['predictions']) ``` 这个代码示例中,我们定义了三个特征列(feature1, feature2, feature3),并使用这些特征列来训练模型。我们使用训练数据来训练模型,并使用测试数据来评估模型的性能。最后,我们使用预测数据来进行预测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值