当前位置:网站首页>Using neural network to predict the weather
Using neural network to predict the weather
2022-07-28 06:11:00 【Alan and fish】
1. A simple understanding of the principle of Neural Networks

As shown in the figure, this neural network is divided into :
- Input layer
Each node of the input layer represents one of the characteristics of an object , These features can be represented by a matrix x Express , Because this is what we humans can understand , So it should be converted into something that the computer can understand .
Using functions to calculate ,w Weight. ,b It's bias .
y=w1x+b1
We train this function constantly , Gradient descent through back propagation to the best w and b Be able to fit these data .
Among them, the transmission layer has 3 A node is a 1x3 Matrix , The corresponding hiding is a 1x4 Matrix , Then multiply by w1 It's a 3x4 Matrix ,b It's a 1x4 Matrix .
The neural network also needs an activation function , Some commonly used sigmoid,relu,tanh, Because the neural network corresponds to a linearized function , Sometimes we have to solve the problem of nonlinearity , So the activation function is introduced , Solve problems that cannot be solved by linear model .
Hidden layer
Output layer
To make a long story short , Neural network is to find the most suitable w and b So that its function graph can contain the sample points we need . As shown in the figure , The graph of our neural network is green , It just includes all positive examples , When I predict next time , That is to throw features in , He will enter a dot in the green image .
This is a little unclear , You can refer to other online Posts .
2. Use neural networks to predict weather cases
In order to better learn neural network , I learned an example .
The data set of this example is 348 Day weather , Predict according to the characteristics of these weather conditions .
Data sets :
link :https://pan.baidu.com/s/1NORkTP-OFOfsbRVvyt29sw
Extraction code :kibn
- Load data
import numpy as np
import pandas as pd
import datetime
import matplotlib.pyplot as plt
from sklearn import preprocessing
# Load data
def data_load(filepath):
''' In the data table
year,moth,day,week The specific time indicated respectively
temp_2: The highest temperature the day before yesterday
temp_1: The highest temperature yesterday
average: In history , The average maximum temperature on this day of the year
actual: This is our tag value , The real maximum temperature of the day
friend: This column may be busy , Your friend guessed the possible value , Let's just ignore it '''
features=pd.read_csv(filepath)
# Print data format
print(features.head())
print(features.shape) # Altogether 348 Data
return features
The data obtained is the current weather , The day before yesterday , Yesterday's weather , The data are as follows 
- Display data
In order to let everyone better understand the data , Made a visual display of all the data now
def showpicture(features):
# Processing time data
years = features['year']
months = features['month']
days = features['day']
# convert to datetime Format
dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in
zip(years, months, days)]
# take string Format is converted into time format as required
dates = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in dates]
# print(dates[:5])
# Hot coding alone
# Because the week in our data is in string format , So if you make a single hot code for the week, it will be converted into the corresponding data
features = pd.get_dummies(features)
# print(features.head(5))
# Draw the data into a picture
# Specify the default style
plt.style.use('fivethirtyeight')
# Setting up layout
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))
fig.autofmt_xdate(rotation=45) # Indicates that the x Axis progress 45 Degree reversal
# Label value ( actual value )
ax1.plot(dates, features['actual'])
ax1.set_xlabel('day');
ax1.set_ylabel('Temperature');
ax1.set_title('Max Temp')
# yesterday
ax2.plot(dates, features['temp_1'])
ax2.set_xlabel('day');
ax2.set_ylabel('Temperature');
ax2.set_title('Previous Max Temp')
# The day before yesterday
ax3.plot(dates, features['temp_2'])
ax3.set_xlabel('day');
ax3.set_ylabel('Temperature');
ax3.set_title('Two Days Prior Max Temp')
# My friend
ax4.plot(dates, features['friend'])
ax4.set_xlabel('day');
ax4.set_ylabel('Temperature');
ax4.set_title('Friend Estimate')
plt.show()
As shown in the figure , Corresponding to the current maximum temperature 、 The highest temperature yesterday 、 The highest temperature the day before yesterday , Among them, the weather predicted by friends can be ignored .
- Processing data
# Processing data
def data_handle(features):
# Hot coding alone
# Because the week in our data is in string format , So if you make a single hot code for the week, it will be converted into the corresponding data
features = pd.get_dummies(features)
# label
# In our data , Apart from actual It's our actual value y, The other values are x
# So when reading, extract him
labels=np.array(features['actual'])
# print(labels)
# Remove labels from features
features=features.drop('actual',axis=1)
# Keep the list separately , In case of future trouble
features_list=list(features.columns)
print(features_list)
# Convert the data into an appropriate format
features=np.array(features)
# print(features)
# print(features.shape)
# Because in our original data month and day They are all relatively small , So I can be right features Make a standardization , In this way, the training convergence will be faster
input_features=preprocessing.StandardScaler().fit_transform(features)
return input_features,labels
- The training model predicts the weather
Here is a beginner's neural network , So take the training set as the test set , Then display through images , See the fitting degree of the predicted data more intuitively .
import torch
from data_load.preprocessdata import data_load,data_handle,showpicture
from model.modeling import Neural
import torch.nn as nn
import numpy as np
import datetime
import pandas as pd
import matplotlib.pyplot as plt
if __name__=='__main__':
# Load data
features=data_load('./data/temps.csv')
# Generate statistical charts from the data
# showpicture(features)
# Separate the real value from the predicted data
input_features,labels=data_handle(features)
# Use torch.nn Building neural network
x = torch.tensor(input_features, dtype=float)
y = torch.tensor(labels, dtype=float)
# Set parameters
input_size=input_features.shape[1] # (348,14), The raw data is a 348 That's ok 14 Columns of the matrix
hidden_size=128
output_size=1
batch_size=16 # Training in batches , Each use 16 Data
my_nn=nn.Sequential(
nn.Linear(input_size,hidden_size), # The input layer has 14 Nodes , Hidden layer are 128 Nodes
nn.Sigmoid(),# The activation function uses sigmoid function
nn.Linear(hidden_size,output_size), # Output layer
)
# Define the loss function , The mean square loss function is used
cost=nn.MSELoss(reduction='mean')
# Construct an optimizer , It can reduce the noise of input data , Make the data more convenient for training
optimizer=torch.optim.Adam(my_nn.parameters(),lr=0.001)
# Training network
losses=[]
for i in range(1000):
batch_loss=[]
# Use MINI-Batch Methods to train
for start in range(0,len(input_features),batch_size):
end=start+batch_size if start+batch_size<len(input_features) else len(input_features)
xx=torch.tensor(input_features[start:end],dtype=torch.float,requires_grad=True)
yy=torch.tensor(labels[start:end],dtype=torch.float,requires_grad=True)
## Training
prediction=my_nn(xx)
loss=cost(prediction,yy)
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
batch_loss.append(loss.data.numpy())
# Every time 100 One loss per print
if i%100==0:
losses.append(np.mean(batch_loss))
print(i,np.mean(batch_loss))
# Test the training results , It was originally intended to use test sets , For a quick experiment , What we are experimenting with is the data of the test set
x=torch.tensor(input_features,dtype=torch.float)
predict=my_nn(x).data.numpy() # Need to put tensor Data to numpy Formatted data , Because in plt China only supports numpy Formatted data
# Visualize the predicted data and actual data
# format conversion
# convert to datetime Format
# Processing time data
years = features['year']
months = features['month']
days = features['day']
dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in
zip(years, months, days)]
# take string Format is converted into time format as required
dates = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in dates]
# Create a table to store the date and its corresponding label
true_date=pd.DataFrame(data={'date':dates,'actual':labels})
# Similarly, create a table to store the predicted values
prediction_data = pd.DataFrame(data={'date': dates, 'prediction': predict.reshape(-1)})
# Create an image
# True value
plt.plot(true_date['date'],true_date['actual'],'b-',label='actual')
# Predictive value
plt.plot(prediction_data['date'],prediction_data['prediction'],'ro',label='prediction')
plt.legend()
# Title
plt.xlabel('Date');
plt.ylabel('Maximum Temperature(F)');
plt.title('Actual and predicted Values');
plt.show()
Every time 100 Display once loss:
100 37.94807
200 35.65217
300 35.278557
400 35.112137
500 34.977985
600 34.857384
700 34.736656
800 34.610817
900 34.47966
Use matplotlib.pyplot Visualize the fitting between our predicted weather and the real weather :
3. summary
Through this little experiment , Let me simply understand that neural network is a mathematical function , Training this model is to find the most appropriate parameters , These functions can perfectly contain our sample points , The next time we predict , Just input the features , We can calculate which y, Is our prediction .
边栏推荐
- 分布式集群架构场景优化解决方案:分布式ID解决方案
- Micro service architecture cognition and service governance Eureka
- Distributed lock redis implementation
- Two methods of covering duplicate records in tables in MySQL
- 微信小程序开发语言一般有哪些?
- Regular verification rules of wechat applet mobile number
- 如何选择小程序开发企业
- Uview upload component upload upload auto upload mode image compression
- 【3】 Redis features and functions
- Sqlalchemy usage related
猜你喜欢
随机推荐
Flink CDC (MySQL as an example)
【一】redis简介
强化学习——价值学习中的SARSA
深度学习(增量学习)——ICCV2022:Contrastive Continual Learning
强化学习——连续控制
Self attention learning notes
What are the advantages of small program development system? Why choose it?
Centos7 installing MySQL
Tensorboard visualization
深度学习——Pay Attention to MLPs
微信团购小程序怎么做?一般要多少钱?
tf.keras搭建神经网络功能扩展
【1】 Introduction to redis
小程序开发
《AdaFace: Quality Adaptive Margin for Face Recognition》用于人脸识别的图像质量自适应边缘损失
Briefly understand MVC and three-tier architecture
What about the app store on wechat?
Deep learning (self supervised: Moco V3): An Empirical Study of training self supervised vision transformers
Sqlalchemy usage related
Deep learning (self supervision: CPC V2) -- data efficient image recognition with contractual predictive coding









