0% found this document useful (0 votes)
78 views2 pages

Practical 2.ipynb - Colaboratory

This document contains code to import libraries, load weather data from a CSV file, examine the data, and attempt to build a linear regression model to predict temperature. However, the date column contains string values that cannot be converted to floats, causing the model fitting to fail with a ValueError. The data is explored through methods like shape, dtypes, describe, and checking for null values.

Uploaded by

Vatsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views2 pages

Practical 2.ipynb - Colaboratory

This document contains code to import libraries, load weather data from a CSV file, examine the data, and attempt to build a linear regression model to predict temperature. However, the date column contains string values that cannot be converted to floats, causing the model fitting to fail with a ValueError. The data is explored through methods like shape, dtypes, describe, and checking for null values.

Uploaded by

Vatsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import 

pandas as pd
import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
import seaborn as sea
import statistics as st
import sklearn

df = pd.read_csv('/content/weatherHistory.csv.zip')

df.head()

Apparent Wind Win


Formatted Precip Temperature
Summary Temperature Humidity Speed Bearin
Date Type (C)
(C) (km/h) (degrees

2006-04-01
Partly
0 00:00:00.000 rain 9.472222 7.388889 0.89 14.1197 251
Cloudy
+0200

2006-04-01
Partly
1 01:00:00.000 rain 9.355556 7.227778 0.86 14.2646 259
Cloudy
+0200

2006-04-01
Mostly
2 02:00:00.000 rain 9.377778 9.377778 0.89 3.9284 204
Cloudy
+0200

2006-04-01
Partly
3 03:00:00.000 rain 8.288889 5.944444 0.83 14.1036 269
Cloudy
+0200

df.shape

(96453, 12)

df.dtypes

Formatted Date object


Summary object
Precip Type object
Temperature (C) float64
Apparent Temperature (C) float64
Humidity float64
Wind Speed (km/h) float64
Wind Bearing (degrees) float64
Visibility (km) float64
Loud Cover float64
Pressure (millibars) float64
Daily Summary object
dtype: object

df.describe()

Apparent Wind
Temperature Wind Speed Visibi
Temperature Humidity Bearing
(C) (km/h)
(C) (degrees)

count 96453.000000 96453.000000 96453.000000 96453.000000 96453.000000 96453.00

mean 11.932678 10.855029 0.734899 10.810640 187.509232 10.34

std 9.551546 10.696847 0.195473 6.913571 107.383428 4.19

min -21.822222 -27.716667 0.000000 0.000000 0.000000 0.00

25% 4.688889 2.311111 0.600000 5.828200 116.000000 8.33

50% 12.000000 12.000000 0.780000 9.965900 180.000000 10.04

75% 18.838889 18.838889 0.890000 14.135800 290.000000 14.81

df.isnull().sum()

Formatted Date 0
Summary 0
Precip Type 517
Temperature (C) 0
Apparent Temperature (C) 0
Humidity 0
Wind Speed (km/h) 0
Wind Bearing (degrees) 0
Visibility (km) 0
Loud Cover 0
Pressure (millibars) 0
Daily Summary 0
dtype: int64

Predictive data

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(df.drop('Temperature (C)', axis=1), df['Temperature (C)'], test_size=0.2)

# Train a linear regression model on the training set
model = LinearRegression()
model.fit(X_train, y_train)

# Generate predictions on the test set
predictions = model.predict(X_test)

# Print the predicted values
print(predictions)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-904760d17b7a> in <cell line: 10>()
8 # Train a linear regression model on the training set
9 model = LinearRegression()
---> 10 model.fit(X_train, y_train)
11
12 # Generate predictions on the test set

5 frames
/usr/local/lib/python3.9/dist-packages/pandas/core/generic.py in __array__(self,
dtype)
2068
2069 def __array__(self, dtype: npt.DTypeLike | None = None) ->
np.ndarray:
-> 2070 return np.asarray(self._values, dtype=dtype)
2071
2072 def __array_wrap__(

ValueError: could not convert string to float: '2009-07-21 08:00:00.000 +0200'

Colab paid products - Cancel contracts here

You might also like