D
D
dmbgdnwtch2021-10-07 00:43:16
Python
dmbgdnwtch, 2021-10-07 00:43:16

I don't understand how to fix the error. Why does it return type str?

Error on this line X_grid = np.arange(min(x),max(x),0.1)
"TypeError: unsupported operand type(s) for -: 'str' and 'str'"
source :

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

dataset = pd.read_csv('google.csv')
dataset.shape
dataset

dataset.plot(x = "Open", y = "High", alpha = 0.4)
plt.title(' Gogle Stock Trading')
plt.xlabel("Start Price")
plt.ylabel("Highest Price")
plt.xlim(60,100)
plt.ylim(60,100)
dataset.plot(x = "Open", y = " High", alpha = 0.9)
plt.title('Google Stock Trading')
plt.xlabel("Price at the start of the day")
plt.ylabel("Highest price")

x = pd.DataFrame(dataset.Open)
y = pd.DataFrame(dataset.High)

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x,y)

plt.scatter(x,y, color='red')
plt.plot(x, model.predict(x),color='green')
plt.title("Результат линейной регрессии")
plt.xlim(60,100)
plt.ylim(60,100)

from sklearn.preprocessing import PolynomialFeatures
poly_reg= PolynomialFeatures(degree=2)
x_poly = poly_reg.fit_transform(x)

lin_reg2 = LinearRegression()
lin_reg2.fit(x_poly,y)

X_grid = np.arange(min(x),max(x),0.1)
X_grid = X_grid.reshape(len(X_grid),1)
plt.scatter(x,y, color='red')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-10-08
@o5a

The problem is using min(x) on
x = pd.DataFrame(dataset.Open)
In this case, it will return the name of the column, i.e. line. To find the minimum for the data, you must explicitly specify which column to search for, i.e. min(x.Open)
Or initially do x not DataFrame, but Series
x = pd.Series(dataset.Open)
Then you can use min(x)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question