HOw do I use the index as a reference instead of the date

Grantx

Working with dates is an absolute nightmare. How do I select the index column instead of the dates column so that I can plot a linear regression line? The index column is numbered so I would like to reference the index against the price (instead of the date column)

I am getting this error message when trying to convert this column of strings so I choose to ignore it and use the index instead:

ValueError: could not convert string to float: '28/07/2017'

Here is the csv data:

           Date       Time     Open     High      Low     Last   Volume 
0    28/07/2017   00:00:00  1.12670  1.14067  1.12626  1.13833   245861   
1    31/07/2017   00:00:00  1.13892  1.14552  1.13356  1.14511   179706   
2    01/08/2017   00:00:00  1.14457  1.14514  1.13869  1.13973   162943 

Here is the code:

    #import libraries
    import pandas as pd
    import matplotlib.pyplot as plt
    from scipy import stats
    import numpy as np

    data = pd.read_csv('EURCHF_Daily.csv')  # load data set
    x = data.iloc[:, 0].values.reshape(-1, 1)  # values converts it into a numpy array
    x = x.astype(np.float)
    Y = data.iloc[:, 1].values.reshape(-1, 1)  # -1 means that calculate the dimension of rows, but have 1 column
    linear_regressor = LinearRegression()  # create object for the class
    linear_regressor.fit(X, Y)  # perform linear regression
    Y_pred = linear_regressor.predict(X)  # make predictions

plt.scatter(X, Y)
plt.plot(X, Y_pred, color='red')
plt.show()
abhilb

Try:

I am assuming you want Y to be the column Open

X = df.index.to_numpy().reshape(-1, 1)
Y = df.iloc[:, 2].values.reshape(-1, 1)

linear_regressor = LinearRegression()  # create object for the class
linear_regressor.fit(X, Y)
Y_pred = linear_regressor.predict(X)
plt.scatter(X, Y)
plt.plot(X, Y_pred, color='red')
plt.show()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I set a Series index to be DatetimeIndex instead of Index?

how do I use current node instead of this?

Java: Why is the Date constructor deprecated, and what do I use instead?

How do I use a cell reference as a column in a table reference?

How can I use `index_mut` to get a mutable reference?

How do i use force index with QueryDSL?

How do i use variable in array index?

How do I reference a unique index that uses a function in ON CONFLICT?

How do I use a date in an Expect script?

How do I reference a date array in multiple CTE's?

How can I index a Date column that doesn't reference itself by index value or column name?

How do I use the index tag? "Index is not defined"

"Member cannot be accessed with an instance reference qualify it with a type name instead" and I DO use type name

How do I forward fill a date index with duplicate period values?

How do I change the date format for a Pandas Index?

How do I reference a bootstrap scss variable using @use?

How do I reference an arbitrary id<MTLTexture> for use in a SCNTechnique?

What is the <views:...> command in Xaml and how do I use it/find reference for it?

How do I re-use a variable with a different reference type?

How do I use a Match function and reference the row number it returns?

How do I reference a category and use its methods in a class?

How do i use a parameter input for an object reference?

SwiftUI how do I use .textFieldStyle(.myTextFieldStyle) instead of textFieldStyle(MyTextFieldStyle())

How do I make Alien use an existing tarball instead of downloading?

How do I change Eclipse to use spaces instead of tabs?

How do I use reduce function instead of recurring function?

How do I force compiler to use aggregate init instead of constructor

How do I configure Notepad++ to use spaces instead of tabs?

How do I uninstall PIL for python 2.7 and use Pillow instead?