import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import time
path = "C:\\Users\\todgu\\Downloads\\삼각함수표1111.csv"
doc = pd.read_csv(path, encoding = "cp949")
doc = doc.dropna()
def sin(angle):
y = doc.iloc[:, 3].values
x = doc.iloc[:, 1].values
y = y.reshape(-1, 1)
x = x.reshape(-1, 1)
poly_reg = PolynomialFeatures(degree = 4)
x_poly = poly_reg.fit_transform(x)
poly_reg.get_feature_names_out()
len_reg = LinearRegression()
len_reg.fit(x_poly, y)
return len_reg.predict(poly_reg.fit_transform([[angle]]))
def cos(angle):
y = doc.iloc[:, 4].values
x = doc.iloc[:, 1].values
y = y.reshape(-1, 1)
x = x.reshape(-1, 1)
poly_reg = PolynomialFeatures(degree = 4)
x_poly = poly_reg.fit_transform(x)
poly_reg.get_feature_names_out()
len_reg = LinearRegression()
len_reg.fit(x_poly, y)
return len_reg.predict(poly_reg.fit_transform([[angle]]))
def tan(angle):
y = doc.iloc[:, 5].values
x = doc.iloc[:, 1].values
y = y.reshape(-1, 1)
x = x.reshape(-1, 1)
poly_reg = PolynomialFeatures(degree = 4)
x_poly = poly_reg.fit_transform(x)
poly_reg.get_feature_names_out()
len_reg = LinearRegression()
len_reg.fit(x_poly, y)
return len_reg.predict(poly_reg.fit_transform([[angle]]))
while True:
print("<삼각함수 잘하는 AI>입니다.")
print("이용하고자 하는 함수를 선택하세요.")
print("[1] sin | [2] cos | [3] tan")
func = input()
print("각도를 입력하여 주세요.(90°이하) [뒤로가기]")
ang = input()
if ang == "뒤로가기":
continue
elif func == "1" or func == "sin":
print(sin(ang))
elif func == "2" or func == "cos":
print(cos(ang))
elif func == "3" or func == "tan":
print(tan(ang))
else:
print("잘못된 입력값입니다.")
I am getting the following error:
Traceback (most recent call last):
File "c:\Users\todgu\coding\AIstudy\삼각함수.py", line 65, in <module>
print(sin(ang))
^^^^^^^^
File "c:\Users\todgu\coding\AIstudy\삼각함수.py", line 23, in sin
return len_reg.predict(poly_reg.fit_transform([[angle]]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\sklearn\utils\_set_output.py", line 140, in wrapped
data_to_wrap = f(self, X, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\sklearn\base.py", line 915, in fit_transform
return self.fit(X, **fit_params).transform(X)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\sklearn\base.py", line 1151, in wrapper
return fit_method(estimator, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\sklearn\preprocessing\_polynomial.py", line 322, in fit
_, n_features = self._validate_data(X, accept_sparse=True).shape
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\sklearn\base.py", line 604, in _validate_data
out = check_array(X, input_name="X", **check_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\sklearn\utils\validation.py", line 948, in check_array
raise ValueError(
ValueError: dtype='numeric' is not compatible with arrays of bytes/strings.Convert your data to numeric values explicitly instead.
I was making a program that outputs the result according to the input value by learning the value of each angle of the trigonometric function.
can anyone help?
I am so tired of this.
I’ve tried changing the datatype, but I’m still getting errors. I have no idea why I am getting the error… ㅠoㅠ
>Solution :
Probally the columns in the CSV file have some non-numerical (str) values. I can’t tell for sure, as I don’t have access to the file, but I imagine it contains data like ‘sin’ or ‘cos’, and it may not be calculating those values and reading them as numbers. You can use the pandas function ‘to_numeric’ to try solving that.
Link: https://pandas.pydata.org/docs/reference/api/pandas.to_numeric.html
Something like:
doc[‘column_name’] = pd.to_numeric(doc[‘column_name’])
I’m also learning data analysis, so not too sure if I could help, hope it does!