Re: Datenanalyse in Python
Verfasst: Sonntag 13. Dezember 2020, 21:09
Code: Alles auswählen
from datetime import datetime
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as pldates
import numpy as np
DATA_FILENAME = "SmartCountr.csv"
def main():
data = pd.read_csv(DATA_FILENAME, delimiter=";")
# Create linear regression object
regr = linear_model.LinearRegression()
x = [datetime.strptime(str(d), '%Y-%m-%d %H:%M:%S') for d in data["zeit"]]
counter_x = matplotlib.dates.date2num(x)
formatter = pldates.DateFormatter("%M")
ax = plt.subplot()
ax.xaxis.set_major_formatter(formatter)
# Load the dataset
counter_y = data['gate.1.in']
counter_x.shape, counter_y.shape
# Use only one feature
counter_x = counter_x[:, np.newaxis, 2]
counter_x.shape
# Train the model using the training sets
regr.fit(counter_x, counter_y)
# Make predictions using the testing set
counter_y_pred = regr.predict(counter_x)
# Plot outputs
plt.scatter(counter_x, data['gate.1.in'], color='black')
plt.plot(counter_x, counter_y_pred, color='blue', linewidth=3)
# plt.xticks(())
# plt.yticks(())
axis_label_arguments = {
"family": "serif",
"weight": "normal",
"size": 16,
"labelpad": 6,
}
plt.xlabel("Minutes", color="r", **axis_label_arguments)
plt.ylabel("Frequency", color="b", **axis_label_arguments)
plt.suptitle("Sensor measurement errors", fontsize=16)
plt.legend()
plt.show()
if __name__ == "__main__":
main()