进行基于文本的预测

我的代码可以进行数值预测,但如何进行基于文本的预测呢?

我尝试过编写一个Python脚本,将字符串转换为数字,但效率非常低。我正在寻找更好的方法来实现这一点。

import pandas as pdimport numpy as npimport sklearnfrom sklearn import linear_modelfrom sklearn.utils import shuffledata = pd.read_csv("data.csv", sep=";")data = data[["index1", "index2", "index3", "index4", "index5", "index6"]]predict = "index6"X = np.array(data.drop([predict], 1))y = np.array(data[predict])x_train, x_test, y_train, y_test =         sklearn.model_selection.train_test_split(X, y, test_size = 0.1)linear = linear_model.LinearRegression()linear.fit(x_train, y_train)acc = linear.score(x_test, y_test)print(acc)print('Coefficient: \n', linear.coef_)print('Intercept: \n', linear.intercept_)predictions = linear.predict(x_test)for x in range(len(predictions)):    print(predictions[x], x_test[x], y_test[x])

这是进行数值预测的代码。


回答:

你可以尝试使用TF-IDF方法对你的词语进行向量化,这种方法会将字符串转换为基于它们的频率和使用情况的浮点值数组。这是自然语言处理中将词语转换为数字的最常用方法,可能对你也有效。有很多关于TF-IDF向量化的教程可供参考。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注