<filename>Machine Learning Scientist with Python Track/9. Machine Learning for Time Series Data in Python/ch1_exercises.py<gh_stars>1-10
# Exercise_1 
#1
## Plotting a time series (I)



# Print the first 5 rows of data

print(data.head())



# Print the first 5 rows of data2

print(data2.head())





# Plot the time series in each dataset

fig, axs = plt.subplots(2, 1, figsize=(5, 10))

data.iloc[:1000].plot(y='data_values', ax=axs[0])

data2.iloc[:1000].plot(y='data_values', ax=axs[1])

plt.show()
#2
# Print the first 5 rows of data2

print(data2.head())
#3
fig, axs = plt.subplots(2, 1, figsize=(5, 10))

data.iloc[:1000].plot(y='data_values', ax=axs[0])

data2.iloc[:1000].plot(y='data_values', ax=axs[1])

plt.show()


--------------------------------------------------
# Exercise_2 
fig, axs = plt.subplots(2, 1, figsize=(5, 10))

data.iloc[:1000].plot(x='time', y='data_values', ax=axs[0])

data2.iloc[:1000].plot(x='time', y='data_values', ax=axs[1])

plt.show()

--------------------------------------------------
# Exercise_3 
#1
# Print the first 5 rows for inspection
print(data.head())
#2
from sklearn.svm import LinearSVC

# Construct data for the model
X = data[["petal length (cm)", "petal width (cm)"]]
y = data[['target']]

# Fit the model
model = LinearSVC()
model.fit(X, y)


--------------------------------------------------
# Exercise_4 
## Predicting using a classification model



# Create input array

X_predict = targets[['petal length (cm)', 'petal width (cm)']]



# Predict with the model

predictions = model.predict(X_predict)

print(predictions)



# Visualize predictions and actual values

plt.scatter(X_predict['petal length (cm)'], X_predict['petal width (cm)'],

            c=predictions, cmap=plt.cm.coolwarm)

plt.title("Predicted class values")

plt.show()

--------------------------------------------------
# Exercise_5 
from sklearn import linear_model



# Prepare input and output DataFrames

X = boston[['AGE']]

y = boston[['RM']]



# Fit the model

model = linear_model.LinearRegression()

model.fit(X, y)

--------------------------------------------------
# Exercise_6 
# Generate predictions with the model using those inputs
predictions = model.predict(new_inputs.reshape(-1,1))

# Visualize the inputs and predicted values
plt.scatter(new_inputs, predictions, color='r', s=3)
plt.xlabel('inputs')
plt.ylabel('predictions')
plt.show()

--------------------------------------------------
# Exercise_7 
import librosa as lr

from glob import glob



# List all the wav files in the folder

audio_files = glob(data_dir + '/*.wav')



# Read in the first audio file, create the time array

audio, sfreq = lr.load(audio_files[0])

time = np.arange(0, len(audio)) / sfreq



# Plot audio over time

fig, ax = plt.subplots()

ax.plot(time, audio)

ax.set(xlabel='Time (s)', ylabel='Sound Amplitude')

plt.show()

--------------------------------------------------
# Exercise_8 
data = pd.read_csv('prices.csv', index_col=0)



# Convert the index of the DataFrame to datetime

data.index = pd.to_datetime(data.index)

print(data.head())



# Loop through each column, plot its values over time

fig, ax = plt.subplots()

for column in data.columns:

    data[column].plot(ax=ax, label=column)

ax.legend()

plt.show()

--------------------------------------------------

<filename>Machine Learning Scientist with Python Track/9. Machine Learning for Time Series Data in Python/ch1_exercises.py<gh_stars>1-10
# Exercise_1 
#1
## Plotting a time series (I)



# Print the first 5 rows of data

print(data.head())



# Print the first 5 rows of data2

print(data2.head())





# Plot the time series in each dataset

fig, axs = plt.subplots(2, 1, figsize=(5, 10))

data.iloc[:1000].plot(y='data_values', ax=axs[0])

data2.iloc[:1000].plot(y='data_values', ax=axs[1])

plt.show()
#2
# Print the first 5 rows of data2

print(data2.head())
#3
fig, axs = plt.subplots(2, 1, figsize=(5, 10))

data.iloc[:1000].plot(y='data_values', ax=axs[0])

data2.iloc[:1000].plot(y='data_values', ax=axs[1])

plt.show()


--------------------------------------------------
# Exercise_2 
fig, axs = plt.subplots(2, 1, figsize=(5, 10))

data.iloc[:1000].plot(x='time', y='data_values', ax=axs[0])

data2.iloc[:1000].plot(x='time', y='data_values', ax=axs[1])

plt.show()

--------------------------------------------------
# Exercise_3 
#1
# Print the first 5 rows for inspection
print(data.head())
#2
from sklearn.svm import LinearSVC

# Construct data for the model
X = data[["petal length (cm)", "petal width (cm)"]]
y = data[['target']]

# Fit the model
model = LinearSVC()
model.fit(X, y)


--------------------------------------------------
# Exercise_4 
## Predicting using a classification model



# Create input array

X_predict = targets[['petal length (cm)', 'petal width (cm)']]



# Predict with the model

predictions = model.predict(X_predict)

print(predictions)



# Visualize predictions and actual values

plt.scatter(X_predict['petal length (cm)'], X_predict['petal width (cm)'],

            c=predictions, cmap=plt.cm.coolwarm)

plt.title("Predicted class values")

plt.show()

--------------------------------------------------
# Exercise_5 
from sklearn import linear_model



# Prepare input and output DataFrames

X = boston[['AGE']]

y = boston[['RM']]



# Fit the model

model = linear_model.LinearRegression()

model.fit(X, y)

--------------------------------------------------
# Exercise_6 
# Generate predictions with the model using those inputs
predictions = model.predict(new_inputs.reshape(-1,1))

# Visualize the inputs and predicted values
plt.scatter(new_inputs, predictions, color='r', s=3)
plt.xlabel('inputs')
plt.ylabel('predictions')
plt.show()

--------------------------------------------------
# Exercise_7 
import librosa as lr

from glob import glob



# List all the wav files in the folder

audio_files = glob(data_dir + '/*.wav')



# Read in the first audio file, create the time array

audio, sfreq = lr.load(audio_files[0])

time = np.arange(0, len(audio)) / sfreq



# Plot audio over time

fig, ax = plt.subplots()

ax.plot(time, audio)

ax.set(xlabel='Time (s)', ylabel='Sound Amplitude')

plt.show()

--------------------------------------------------
# Exercise_8 
data = pd.read_csv('prices.csv', index_col=0)



# Convert the index of the DataFrame to datetime

data.index = pd.to_datetime(data.index)

print(data.head())



# Loop through each column, plot its values over time

fig, ax = plt.subplots()

for column in data.columns:

    data[column].plot(ax=ax, label=column)

ax.legend()

plt.show()

--------------------------------------------------

# Exercise_1 #1 ## Plotting a time series (I) # Print the first 5 rows of data # Print the first 5 rows of data2 # Plot the time series in each dataset #2 # Print the first 5 rows of data2 #3 # Exercise_2 # Exercise_3 #1 # Print the first 5 rows for inspection #2 # Construct data for the model # Fit the model # Exercise_4 ## Predicting using a classification model # Create input array # Predict with the model # Visualize predictions and actual values # Exercise_5 # Prepare input and output DataFrames # Fit the model # Exercise_6 # Generate predictions with the model using those inputs # Visualize the inputs and predicted values # Exercise_7 # List all the wav files in the folder # Read in the first audio file, create the time array # Plot audio over time # Exercise_8 # Convert the index of the DataFrame to datetime # Loop through each column, plot its values over time