Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1 /15/2020 LAB no.3.

ipynb - Colaboratory

Muhammad Salman
ee171057

from google.colab import files

uploaded=files.upload()

Choose Files Gambiaa.csv


Gambiaa.csv (application/vnd.ms-excel) - 322413 bytes, last modified: 11/14/2020 - 100% done
Saving Gambiaa.csv to Gambiaa.csv

import pandas as pd
import io
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv(io.StringIO(uploaded['Gambiaa.csv'].decode('utf-8')))
print(df)

Region Country State City Month Day Year AvgTemperature


0 Africa Gambia NaN Banjul 1 1 1995 76.5
1 Africa Gambia NaN Banjul 1 2 1995 70.3
2 Africa Gambia NaN Banjul 1 3 1995 74.7
3 Africa Gambia NaN Banjul 1 4 1995 73.3
4 Africa Gambia NaN Banjul 1 5 1995 73.8
... ... ... ... ... ... ... ... ...
8531 Africa Gambia NaN Banjul 5 9 2020 77.6
8532 Africa Gambia NaN Banjul 5 10 2020 75.6
8533 Africa Gambia NaN Banjul 5 11 2020 76.3
8534 Africa Gambia NaN Banjul 5 12 2020 77.8
8535 Africa Gambia NaN Banjul 5 13 2020 72.9

[8536 rows x 8 columns]

Day = df[(df['City']=='Banjul') & (df['Year']==1995) & (df['Month']==4)]['Day']


YEAR=df[(df['City']=='Banjul') & (df['Month']==4)& (df['Day']==4)]['Year']
len(YEAR)

24

A = []
i=1
for year in df['Year'].unique():
A.append(df[(df['City']=='Banjul') & (df['Year']==year) & (df['Month']==4)]['AvgTemperature'])
#print(A)
i+=1

A= np.sum(A,axis=0)/len(df['Year'].unique())
print(A)
plt.ylim(60,80)

https://colab.research.google.com/drive/1iGI16qCbXJFqwzkw88NQJVDuUZs_PBGg#scrollTo=e5QhrYQXcNi7&printMode=true 1/4
1 /15/2020 LAB no.3.ipynb - Colaboratory

plt.scatter(Day,A)
plt.title("Gambia's April Temperature for the year 1995-2020")
plt.xlabel("Days")
plt.ylabel("Average Temperature (F)")

[77.25416667 77.175 76.775 76.825 76.92916667 76.8125


77.04583333 76.7125 76.78333333 77.10416667 77.65833333 78.58333333
77.5 77.35833333 77.7375 76.97916667 77.225 77.1125
76.3 76.50833333 76.725 77.05833333 77.24166667 77.33333333
76.82083333 77.56666667 77.97083333 77.4 77.6625 77.32083333]
Text(0, 0.5, 'Average Temperature (F)')

one=np.ones(Day.shape)
B=np.array([Day,one]).T
b=A.reshape(30,1)

bt=np.dot(B.T,B)
Binv=np.linalg.inv(bt)
Bn=np.dot(B.T,b)
xo=np.dot(Binv,Bn)
xo

array([[1.18845013e-02],
[7.69984291e+01]])

A_new=xo[0]*Day+xo[1]
plt.ylim((60,80))
plt.plot(Day,A,'o',Day,A_new)
plt.title("Gambia's April Temperature for the year 1995-2020")
plt.xlabel("Days")
plt.ylabel("Average Temperature (F)")

https://colab.research.google.com/drive/1iGI16qCbXJFqwzkw88NQJVDuUZs_PBGg#scrollTo=e5QhrYQXcNi7&printMode=true 2/4
1 /15/2020 LAB no.3.ipynb - Colaboratory

Text(0, 0.5, 'Average Temperature (F)')

A = []
i=1
R=[]
z=0
for year in df['Year'].unique():
while i!=25:
for day in df['Day'].unique():
R=np.sum(df[(df['City']=='Banjul') & (df['Year']==year) & (df['Month']==4)& (df['Day']==i)
#R=df[(df['City']=='Banjul') & (df['Year']==year) & (df['Month']==4)& (df['Day']==i)]['Avg
z+=R
#R=0
i+=1
o=z/len(Day)
A.append(o)
z=0
print(A)
plt.plot(YEAR,A)
plt.title("Gambia's April Temperature for the year 1995-2020")
plt.xlabel("YEARs")
plt.ylabel("Average Temperature (F)")

https://colab.research.google.com/drive/1iGI16qCbXJFqwzkw88NQJVDuUZs_PBGg#scrollTo=e5QhrYQXcNi7&printMode=true 3/4
1 /15/2020 LAB no.3.ipynb - Colaboratory

[75.22666666666666, 76.25999999999999, 76.98333333333333, 76.36333333333339, 74.50333333333


Text(0, 0.5, 'Average Temperature (F)')

https://colab.research.google.com/drive/1iGI16qCbXJFqwzkw88NQJVDuUZs_PBGg#scrollTo=e5QhrYQXcNi7&printMode=true 4/4

You might also like