我决定使用一些实际示例来处理线性回归。为此,我下载了一个csv文件,该文件显示了1850年至2015年每个月的一些城市的温度数据。https://data.world/data-society/global-climate-change-data/workspace/file?filename=GlobalLandTemperatures%2FGlobalLandTemperaturesByMajorCity.csv读取该文件,计算年平均温度是正常的。但是,计算线性回归参数w存在问题。它等于nan。结果,无法建立回归线。你能告诉我错误是什么吗?预先感谢!
import matplotlib.pyplot as plt
import csv
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
def search_stat(start_year,finish_year):
csv_path="GlobalLandTemperatures_GlobalLandTemperaturesByMajorCity.csv"
with open(csv_path, encoding='utf-8', newline='') as csv_file:
csvreader = csv.reader(csv_file)
array= []
end_year= str(finish_year)
temperature=0
n=-1
#row[0]- column with dates in format YYYY-MM-DD
#row[1]- column with monthly average temperature
#row[3]- column with city names
for row in csvreader:
if (row[3]== 'Abidjan') and (start_year<=(int((row[0])[:4]))) and not
(row[0].startswith(end_year)):
if (row[1]!='') and (row[1]!=','):
temperature=float(row[1])
array.append(temperature)
n+=1
else:
array.append(array[n])
n+=1
if (row[0].startswith(end_year)):
return array
def avr_tem_array(start_year,finish_year):
array=[] #list with values in every months
array= search_stat(start_year,finish_year)
i=0
sum=0
k=0
result_array=[]
months=(finish_year-start_year)*12
while(i<months):
sum+=array[i]
if k==11:
result_array.append(sum/12) #average temperature for a year
sum=0
k=0
i+=1
continue
i+=1
k+=1
print(array)
return(result_array)
array= []
array= avr_tem_array(1965,1995)#average annual temperatures 1965-1995
print(array)
years=np.arange(1965.0,1995.0)
learning_rate=0.01
learning_epochs=100
X=tf.compat.v1.placeholder(tf.float32)
Y=tf.compat.v1.placeholder(tf.float32)
def model(X,w):
return tf.compat.v1.multiply(X,w)
w= tf.compat.v1.Variable(0.0, name='weights')# parameter of regression
y_model=model(X,w)
cost=tf.square(Y-y_model)
main_fun=tf.compat.v1.train.GradientDescentOptimizer(learning_rate)
.minimize(cost)
sess=tf.compat.v1.Session()
init=tf.compat.v1.global_variables_initializer()
sess.run(init)
for epoch in range(learning_epochs):
for (x,y) in zip(years,array):
sess.run(main_fun, feed_dict={X: x, Y: y})
w_learned=sess.run(w) #calculate final value of linear regression parametr
#it turns out to be nan
sess.close()
y_lerned=w_learned*years #all values of y_learned is nan. We can't build line of regression
print(years)
print(w_learned)
print(y_lerned)
plt.scatter(years,array)
plt.plot(years,y_lerned) #building a regression line
plt.show()