<jupyter_start><jupyter_text>Importando dados<jupyter_code>import pandas
import numpy

def read_labels():
    labels = []
    file = open('../data/features_names.txt', 'r')
    for line in file:
        line = line[:-1]
        labels.append(line)
    return labels

print(str(read_labels()))
NUMBER_SAMPLES = 1000
NUMBER_FEATURES = 6

def create_mock_data():

    samples = numpy.around(1 * numpy.random.rand(NUMBER_SAMPLES, NUMBER_FEATURES), decimals=3)

    data = pandas.DataFrame(data=samples)
    print(str(data))
    data.to_csv('../data/biodigester.csv', index=False, encoding='utf-8')
import pandas

def read_data():
    labels = read_labels()
    biodigester_data = pandas.read_csv('../data/biodigester.csv', sep=",", names=labels)
    return biodigester_data

#print(str(read_data()))
bio_dataframe = read_data()
#print(bio_dataframe)

target = bio_dataframe['volume']
data = bio_dataframe.drop(['volume'], axis=1)<jupyter_output><empty_output><jupyter_text>Separando slice para validação<jupyter_code># particionar dados em treino e teste
from sklearn.cross_validation import train_test_split
data_train, data_test, target_train, target_test = train_test_split(data, target, test_size = .1) # .5 metade dos dados para teste
print(len(data_train))
print(len(data_test))
print(len(target_train))
print(len(target_test))<jupyter_output>900
100
900
100<jupyter_text>Gerando o modelo de regressão linear<jupyter_code>from sklearn import linear_model
regressor = linear_model.LinearRegression()

# Train the model using the training sets
regressor.fit(data_train, target_train)
X_toPredict = numpy.array([[0.817,0.065,0.774,0.72,0.104], [0.153,0.577,0.347,0.74,0.073]])

y_prediction = regressor.predict(X_toPredict)
print(y_prediction)

reg2 = regressor
y2 = reg2.predict(X_toPredict)
print(y2)<jupyter_output>[0.49242922 0.47034935]
[0.49242922 0.47034935]<jupyter_text>Validando o modelo<jupyter_code>from sklearn.cross_validation import cross_val_predict

predicted = cross_val_predict(regressor, data, target, cv=10)
print(y_pred.mean())
plt.scatter(data_test, target_test,  color='black')
plt.plot(data_test, predicted, color='blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()
import matplotlib.pyplot as plt
# cross_val_predict returns an array of the same size as `y` where each entry
# is a prediction obtained by cross validation:

fig, ax = plt.subplots()
ax.scatter(target_test, predicted, edgecolors=(0, 0, 0))
ax.plot([target_test.min(), target_test.max()], [target_test.min(), target_test.max()], 'k--', lw=1)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()<jupyter_output><empty_output><jupyter_text>Fazendo predições<jupyter_code>X_toPredict = [[0.817,0.065,0.774,0.72,0.104], [0.153,0.577,0.347,0.74,0.073]]

y_prediction = regressor.predict(X_toPredict)
print(y_prediction)<jupyter_output>[0.49242922 0.47034935]<jupyter_text>Fazendo testes<jupyter_code>X = data
y = target

# particionar dados em treino e teste
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .1) # .5 metade dos dados para teste

print(len(X_train))
print(len(X_test))
print(len(y_train))
print(len(y_test))
import matplotlib.pyplot as plt
# cross_val_predict returns an array of the same size as `y` where each entry
# is a prediction obtained by cross validation:
predicted = cross_val_predict(regressor, data, target, cv=10)

fig, ax = plt.subplots()
ax.scatter(target, predicted, edgecolors=(0, 0, 0))
ax.plot([target.min(), target.max()], [target.min(), target.max()], 'k--', lw=1)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()

import matplotlib.pyplot as plt
# Plot outputs
# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets
regr.fit(X_train, y_train)

# Make predictions using the testing set
y_pred = regr.predict(X_test)

plt.scatter(X_test, y_test,  color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()<jupyter_output><empty_output><jupyter_text>Persistencia do modelo<jupyter_code>import pickle
from sklearn.externals import joblib

joblib.dump(regressor, 'modelo.pkl')
classificador = joblib.load('./modelo.pkl')
classificador.predict(X_toPredict)
#[0.49242922 0.47034935]<jupyter_output><empty_output><jupyter_text>Multi output<jupyter_code>from sklearn.multioutput import MultiOutputRegressor
from sklearn import linear_model

regressor = linear_model.LinearRegression()

regr_multirf = MultiOutputRegressor(regressor)
regr_multirf.fit(data_train, target_train)
data_2 = bio_dataframe.drop(['volume', 'gas_production'], axis=1)
target_2 = bio_dataframe[['volume', 'gas_production']]
#print(targets)
#print(data_2)

regressor = linear_model.LinearRegression()

regr_multirf = MultiOutputRegressor(regressor)
regr_multirf.fit(data_2, target_2)
to = [[0.439, 0.394, 0.957, 0.182], [0.535, 0.049, 0.842, 0.894]]
print(regr_multirf.predict(to))
from sklearn.metrics import r2_score

r = linear_model.LinearRegression()
r.fit(data_train, target_train)
y_pred = r.predict(data_test)
print(r2_score(target_test,y_pred))
print(pandas.DataFrame(data=y_pred, names=['bosta']))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13431129738997905, 'fraction_numerical': 0.04419889502762431, 'mean_word_length': 6.942511346444781, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '451214', 'license': 'MIT', 'n_tokens_mistral': 2130, 'n_tokens_neox': 1972, 'n_words': 378, 'path': 'digester/regression.ipynb', 'repo_name': 'bio-digester/data-digester', 'size': '5249'}