1.Import the required libraries, including pandas, for importing a CSV file:
1.导入所需的库,包括pandas,用于导入CSV文件。
```py
import pandas as pd
...
...
@@ -15,39 +15,39 @@
import matplotlib.pyplot as plt
```
2.Read the CSV file containing the dataset:
2.读取包含数据集的CSV文件。
```py
data = pd.read_csv("SomervilleHappinessSurvey2015.csv")
```
3.Separate the input features from the target. Note that the target is located in the first column of the CSV file. Convert the values into tensors, making sure the values are converted into floats:
3.将输入特征与目标分开。注意,目标位于CSV文件的第一列。将值转换为张量,确保值转换为浮点数。
```py
x = torch.tensor(data.iloc[:,1:].values).float()
y = torch.tensor(data.iloc[:,:1].values).float()
```
4.Define the architecture of the model and store it in a variable named **model**. Remember to create a single-layer model:
4.定义模型的架构,并将其存储在一个名为`model`的变量中。记住要创建一个单层模型。
```py
model = nn.Sequential(nn.Linear(6, 1),
nn.Sigmoid())
```
5.Define the loss function to be used. Use the MSE loss function:
5.定义要使用的损失函数。使用MSE损失函数。
```py
loss_function = torch.nn.MSELoss()
```
6.Define the optimizer of your model. Use the Adam optimizer and a learning rate of`0.01`:
5.Use a **for** loop to train the network for 3,000 iteration steps:
5.使用`for`循环来训练网络,迭代步数为3000步。
对于我的范围(3000):
...
...
@@ -279,7 +279,7 @@
打印(i,loss.item())
6.Test your model by performing a prediction on the first instance of the test set and comparing it with the ground truth:
6.通过对测试集的第一个实例进行预测,并与地面真相进行比较来测试你的模型。
之前=模型(x_test [0])
...
...
@@ -303,7 +303,7 @@
解:
1.Import the following libraries:
1.导入以下库:
将熊猫作为 pd 导入
...
...
@@ -325,7 +325,7 @@
torch.manual_seed(0)
2.Read the previously prepared dataset, which should have been named **dccc_prepared.csv**:
2.读取之前准备好的数据集,该数据集应该命名为`dccc_prepared.csv`。
数据= pd.read_csv(“ dccc_prepared.csv”)
...
...
@@ -337,13 +337,13 @@
图 3.14:dccc_prepared.csv
3.Separate the features from the target:
3.将特征与目标分开。
X = data.loc [:,:-1]
y =数据[“下个月的默认付款”]
4.Using scikit-learn's **train_test_split** function, split the dataset into training, validation, and testing sets. Use a 60:20:20 split ratio. Set **random_state** to 0:
5.Convert the validation and testing sets into tensors, bearing in mind that the features' matrices should be of the float type, while the target matrices should not. Leave the training sets unconverted for the moment as they will undergo further transformation: